#!/usr/bin/env python # coding: utf-8 # # [Homework Week 9](https://tsoo-math.github.io/ucl/QHW4.html) # # For complete written solutions, please [see](https://tsoo-math.github.io/ucl/QHW4-sol.html). We will mainly be concerned with the Python code here. # ## Random deletion # In[1]: import numpy as np number=10000 inter = np.random.exponential(1/2,number) #notice it is parametrized by scale rather than rate arr = np.cumsum(inter) L = len(arr) coin = np.random.binomial(1,0.5, L) coinindex=-99 for i in range(L): if coin[i]==0: coinindex = np.append(coinindex, i) coinindex = np.delete(coinindex, 0) arrthin = np.delete(arr,coinindex) interthin = arrthin[0] for i in range(len(arrthin)-1): interthin = np.append(interthin, arrthin[i+1] - arrthin[i]) import matplotlib.pyplot as plt z=interthin plt.hist(z, bins = 50, density=True, label='Proability Histogram') t=np.linspace(0,5,num=2000) plt.plot(t,1*np.exp(-1*t), label='Exact Density') plt.legend(loc='upper left') plt.show() # ## Poisson process on a disc # In[2]: def point(): x=1 y=1 while(x**2 + y**2 >=1): x= 2*np.random.uniform() -1 y= 2*np.random.uniform() -1 return np.array([x,y]) pois = [point() for _ in range(np.random.poisson(100*np.math.pi) ) ] L= len(pois) x=100 y=100 for i in range(L): x = np.append(x, pois[i][0]) y = np.append(y, pois[i][1]) x = np.delete(x, 0) y = np.delete(y, 0) import matplotlib.pyplot as plt t=np.linspace(-1,1,num=500) plt.figure(figsize = (10,10)) plt.plot(x,y, 'r*') plt.plot(t, np.sqrt(1-t**2)) plt.plot(t, -np.sqrt(1-t**2)) plt.title("Poisson process on a disc", fontsize=10) plt.show() # ## Endnotes # # Use the ipynb [source](https://tsoo-math.github.io/ucl2/2021-hw-week9.ipynb) for the most update version. # In[3]: from datetime import datetime print(datetime.now()) # In[ ]: # In[ ]: # In[ ]: