-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab8.py
More file actions
53 lines (42 loc) · 1.71 KB
/
lab8.py
File metadata and controls
53 lines (42 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import scipy.stats as stats
gamma_confidence = 0.95
def _m_confidence_interval(distr):
m = np.mean(distr)
s = np.std(distr)
n = len(distr)
interval = s * stats.t.ppf((1 + gamma_confidence) / 2, n - 1) / (n - 1) ** 0.5
return np.around(m - interval, decimals=2), np.around(m + interval, decimals=2)
def _var_confidence_interval(distr):
s = np.std(distr)
n = len(distr)
low_b = s * (n / stats.chi2.ppf((1 + gamma_confidence) / 2, n - 1)) ** 0.5
high_b = s * (n / stats.chi2.ppf((1 - gamma_confidence) / 2, n - 1)) ** 0.5
return np.around(low_b, decimals=2), np.around(high_b, decimals=2)
def _m_confidence_asimpt(distr):
m = np.mean(distr)
s = np.std(distr)
n = len(distr)
u = stats.norm.ppf((1 + gamma_confidence) / 2)
interval = s * u / (n ** 0.5)
return np.around(m - interval, decimals=2), np.around(m + interval, decimals=2)
def _var_confidence_asimpt(distr):
m = np.mean(distr)
s = np.std(distr)
n = len(distr)
m_4 = stats.moment(distr, 4)
e_ = m_4 / s**4 - 3
u = stats.norm.ppf((1 + gamma_confidence) / 2)
U = u * (((e_ + 2) / n) ** 0.5)
low_b = s * (1 + 0.5 * U) ** (-0.5)
high_b = s * (1 - 0.5 * U) ** (-0.5)
return np.around(low_b, decimals=2), np.around(high_b, decimals=2)
if __name__ == '__main__':
size = [20, 100]
for s in size:
distr = np.random.normal(0, 1, size=s)
print('size = ' + str(s))
print('mean', _m_confidence_interval(distr))
print('variance', _var_confidence_interval(distr))
print('asimpt_mean', _m_confidence_asimpt(distr))
print('asimpt_variance', _var_confidence_asimpt(distr))