<jupyter_start><jupyter_text>Simulating Lung Cancer Growth*Newton et al. (2012) use simulation to estimate the mean first passage time—the number of steps, on average, it takes for cancer to pass from the lung to each other location in the body, “something a static autopsy data set cannot give us directly.” The authors conclude that their study gives “important baseline quantitative insight into the structure of lung cancer progression networks.”*<jupyter_code>import numpy as np

# Get transition matrix
data = np.genfromtxt('lungcancer.csv', dtype=float, delimiter=',', names=True)
P = []
for i in range(50):
    P.append(list(data[i]))
# Correct distribution that does not sum up to 1
P = np.array(P)
for i in range(50):
    P[i] = P[i]/np.sum(P[i])
def markov(init, mat, num):
    # init: initial distribution to generate a start on the graph
    # mat: transition matrix
    # num: number of nodes on graph needs to visit
    # 
    nodelst = list(range(50))
    curr = np.random.choice(nodelst, p=init)
    visited = [curr]
    passage_time=0
    while len(visited)<num:
        curr = np.random.choice(nodelst, p=mat[curr])
        if curr not in visited:
            visited.append(curr)
        passage_time+=1
        
    return passage_time
# Prepare initial distribution
# Always starts from node #23
p0 = np.zeros(50)
p0[22] = 1
# Check mean passage-time for cancer to cover 10-24 nodes
pt = np.zeros(15)
rep = 1000
for n in range(10, 25):
    temp = 0
    for i in range(rep): #repeat each n-passage time for 50 times
        temp+= markov(p0, P, n)
    pt[n-10] = temp/rep
%matplotlib inline
import matplotlib.pyplot as plt

plt.plot(list(range(10,25)), pt, 'o-')
plt.xlabel('# of nodes')
plt.ylabel('mean passage time')
plt.title('Lung Cancer Evolution')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10581717451523545, 'fraction_numerical': 0.02548476454293629, 'mean_word_length': 3.7277486910994764, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '360502', 'license': 'MIT', 'n_tokens_mistral': 611, 'n_tokens_neox': 572, 'n_words': 216, 'path': 'Lung_cancer_growth.ipynb', 'repo_name': 'Lonitch/stochastic-process', 'size': '1805'}