File: plot.py

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (47 lines) | stat: -rw-r--r-- 1,169 bytes parent folder | download
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
from collections import Counter
from statistics import fmean, pstdev

from matplotlib import pyplot as plt
from bitarray.util import random_k, sum_indices

from sample import SampleMeanDist

# The code will also work, if you change these parameters here:
SMD = SampleMeanDist(n=1_000, k=600)
M = 100_000  # number of trails
DX = 2.5  # width used for counting trail outcomes

def plot_pdf(plt, xmin, xmax):
    X = []
    Y = []
    n = 2_000
    for i in range(n + 1):
        x = xmin + i * (xmax - xmin) / n
        X.append(x)
        Y.append(SMD.pdf(x) * DX)
    plt.plot(X, Y)

def plot_count(plt, C):
    X = []
    Y = []
    for i in range(min(C), max(C) + 1):
        x = i * DX
        X.append(x)
        Y.append(C[i] / M)
    plt.scatter(X, Y, color='red')

if __name__ == '__main__':
    SMD.print()
    C = Counter()
    values = []
    for _ in range(M):
        x = sum_indices(random_k(SMD.n, SMD.k)) / SMD.k
        C[round(x / DX)] += 1
        values.append(x)
    assert C.total() == M
    print("mean", fmean(values))
    print("stdev", pstdev(values, SMD.mu))

    plot_count(plt, C)
    plot_pdf(plt, min(C) * DX, max(C) * DX)
    plt.show()