-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_distribution_project.py
46 lines (38 loc) · 1.16 KB
/
stats_distribution_project.py
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
import itertools as it
import statistics
from typing import TypeVar
import matplotlib.pyplot as plt
_T = TypeVar("_T")
def list_count(items: list[_T]) -> dict[_T, int]:
counts = dict()
for i in items:
counts[i] = counts.get(i, 0) + 1
return counts
def round_dict(d: dict, n=3) -> dict:
res = dict()
for key in d:
res[key] = round(d[key], n)
return res
def combine_dict(key_: list[_T], item_: list[_T]) -> dict[_T, _T]:
d = {}
assert len(key_)==len(item_)
for i in range(len(key_)):
if d.get(key_[i], None) is None:
d[key_[i]] = item_[i]
else:
d[key_[i]] += item_[i]
return round_dict(d)
def product(items: list[float]) -> float:
cnt = 1
for i in items:
cnt *= i
return cnt
value = [15, 16, 17, 18]
prob = [0.1, 0.2, 0.3, 0.4]
mean_val, median_val, prob_dis = [], [], []
for v, p in zip(it.product(value, repeat=3), it.product(prob, repeat=3)):
mean_val.append(round(sum(v) / 3, 3))
prob_dis.append(round(product(p), 3))
median_val.append(round(statistics.median(v), 3))
print(combine_dict(mean_val, prob_dis))
print(combine_dict(median_val, prob_dis))