import random
from scipy import special
n= 1000
p = 0.25
n_A = 240
N = 10000
count = 0
for i in range(n_A, n+1):
count += (special.comb(n, i)*(p**i)*((1-p)**(n-i)))
print("Analytical Solution: ",count)
Analytical Solution: 0.7776521016980777
count = 0
for i in range(N):
temp = [random.random() for j in range(n)]
cnt = sum(x<=p for x in temp)
if cnt >= n_A:
count += 1
print("Simulated Solution: " , count/N)
Simulated Solution: 0.7709
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def delta(x,y):
return 0 if x == y else 1
def M(seq1,seq2,i,j,k):
return sum(delta(x,y) for x,y in zip(seq1[i:i+k],seq2[j:j+k]))
def makeMatrix(seq1,seq2,k):
n = len(seq1)
m = len(seq2)
return [[M(seq1,seq2,i,j,k) for j in range(m-k+1)] for i in range(n-k+1)]
seqx = "TGGCACACTCACACCACACAGACAGTTA"
seqy = "TGGCACACTCACACCACACAGACAGTTA"
## pad sequence names at the left and top
arr = np.array(makeMatrix(seqx,seqy,1))
arr1 = np.full((arr.shape[0]+1,arr.shape[1]+1)," ",dtype=object)
for i in range(1,len(seqx)+1):
arr1[0][i] = seqx[i-1]
arr1[i][0] =seqx[i-1]
for i in range(0,arr.shape[0]):
for j in range(0, arr.shape[1]):
if arr[i][j] == 0:
arr1[i+1][j+1] = 'X'
## convert your array into a dataframe
df = pd.DataFrame (arr1)
filepath = 'q5.xlsx'
df.to_excel(filepath, index=False)
## The highlighting part is done in Excel and screenshot is shared below