In [2]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
In [4]:
sample_size = 1000
In [8]:
xi = np.random.normal(0,1, sample_size)**2 + np.random.uniform(-1,1, sample_size)
In [11]:
interval = {'low' : 1, 'high' : 2}
plt.hist(xi, normed=True, bins=sample_size // 10);
In [12]:
%%time
convergence_list = []
for sample_size in [10** x for x in range(1, 6)] :
    first_seq = np.random.normal(0,1, sample_size)
    second_seq = np.random.uniform(-1,1, sample_size)
    sum_seq = norm_seq**2 + uniform_seq
    est_proba = np.count_nonzero((sum_seq >= interval['low']) & (sum_seq <= interval['high'])) / sample_size
    convergence_list.append(est_proba)
    print('sample size:           {}'.format(sample_size))
    print('estimated probability: {}'.format(est_proba))
    if len(convergence_list) >= 2:
        print('estimations difference : {}\n'.format(np.abs(convergence_list[-1] - convergence_list[-2])))
sample size:           10
estimated probability: 0.3
sample size:           100
estimated probability: 0.19
estimations difference : 0.10999999999999999

sample size:           1000
estimated probability: 0.204
estimations difference : 0.013999999999999985

sample size:           10000
estimated probability: 0.2053
estimations difference : 0.0013000000000000234

sample size:           100000
estimated probability: 0.20141
estimations difference : 0.0038900000000000046

CPU times: user 16 ms, sys: 0 ns, total: 16 ms
Wall time: 10.3 ms
In [14]:
xi = np.random.normal(1,2, sample_size) + np.random.exponential(2, sample_size)
interval = {'low' : 0, 'high' : 1}
plt.hist(xi, normed=True, bins=sample_size // 10);
In [15]:
%%time
convergence_list = []
for sample_size in [10** x for x in range(1, 6)] :
    first_seq = np.random.normal(1,2, sample_size)
    second_seq = np.random.exponential(2, sample_size)
    sum_seq = first_seq + second_seq
    est_proba = np.count_nonzero((sum_seq >= interval['low']) & (sum_seq <= interval['high'])) / sample_size
    convergence_list.append(est_proba)
    print('sample size:           {}'.format(sample_size))
    print('estimated probability: {}'.format(est_proba))
    if len(convergence_list) >= 2:
        print('estimations difference : {}\n'.format(np.abs(convergence_list[-1] - convergence_list[-2])))
sample size:           10
estimated probability: 0.2
sample size:           100
estimated probability: 0.06
estimations difference : 0.14

sample size:           1000
estimated probability: 0.145
estimations difference : 0.08499999999999999

sample size:           10000
estimated probability: 0.1464
estimations difference : 0.0014000000000000123

sample size:           100000
estimated probability: 0.14244
estimations difference : 0.003959999999999991

CPU times: user 16 ms, sys: 0 ns, total: 16 ms
Wall time: 14.3 ms
In [ ]: