jjj
This commit is contained in:
parent
3a50f1d83d
commit
839b6f79ec
1 changed files with 57 additions and 12 deletions
69
Probas.py
69
Probas.py
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
from random import random
|
||||
from math import floor, sqrt, factorial
|
||||
from math import floor, sqrt, factorial,exp
|
||||
from statistics import mean, variance
|
||||
from matplotlib import pyplot as plt
|
||||
from pylab import *
|
||||
|
@ -61,6 +61,10 @@ def stats_NFBP_iter(R, N):
|
|||
Runs R runs of NFBP (for N items) and studies distribution, variance, mean...
|
||||
Calculates stats during runtime instead of after to avoid excessive memory usage.
|
||||
"""
|
||||
Hmean=0
|
||||
Var=[]
|
||||
H=[]
|
||||
Exp=0
|
||||
P = R * N # Total number of items
|
||||
print("## Running {} NFBP simulations with {} items".format(R, N))
|
||||
# number of bins
|
||||
|
@ -106,17 +110,26 @@ def stats_NFBP_iter(R, N):
|
|||
print("Mean number of bins : {} (variance {})".format(I, IVariance), "\n")
|
||||
# TODO clarify line below
|
||||
print(" {} * {} iterations of T".format(R, N), "\n")
|
||||
|
||||
for n in range(min(N, 10)):
|
||||
for n in range(N):
|
||||
Hn = HSum[n] / R # moyenne
|
||||
HVariance = sqrt(HSumVariance[n] / (R - 1) - Hn**2) # Variance
|
||||
Var.append(HVariance)
|
||||
H.append(Hn)
|
||||
print(
|
||||
"Index of bin containing the {}th item (H_{}) : {} (variance {})".format(
|
||||
n, n, Hn, HVariance
|
||||
)
|
||||
)
|
||||
print(HSum)
|
||||
print(len(HSum))
|
||||
for x in range(len(HSum)):
|
||||
Hmean+=HSum[x]
|
||||
Hmean=Hmean/P
|
||||
print("Hmean is : {}".format(Hmean))
|
||||
Exp=np.exp(1)
|
||||
HSum = [x / R for x in HSum]
|
||||
# print(HSum)
|
||||
HSumVariance = [x / R for x in HSumVariance]
|
||||
print(HSumVariance)
|
||||
# Plotting
|
||||
fig = plt.figure()
|
||||
# T plot
|
||||
|
@ -176,13 +189,17 @@ def stats_NFBP_iter(R, N):
|
|||
cx.set_xlabel("n-item (1-{})".format(N))
|
||||
cx.set_title("H histogram for {} items".format(P))
|
||||
xb = linspace(0, N, 10)
|
||||
yb = Hn * xb / 10
|
||||
wb = HVariance * xb / 10
|
||||
cx.plot(xb, yb, label="Theoretical E(Hn)", color="brown")
|
||||
cx.plot(xb, wb, label="Theoretical V(Hn)", color="purple")
|
||||
xc=linspace(0,N,50)
|
||||
yb = [Hmean for n in range(N)]
|
||||
db =(( HSum[30] - HSum[1])/30)*xc
|
||||
wb =(( HSumVariance[30] - HSumVariance[1])/30)*xc
|
||||
cx.plot(xc, yb, label="Experimental Hn_Mean", color="brown")
|
||||
cx.plot(xc, H, label="Experimental E(Hn)", color="red")
|
||||
cx.plot(xc, Var, label="Experimental V(Hn)", color="purple")
|
||||
cx.legend(loc="upper left", title="Legend")
|
||||
plt.show()
|
||||
|
||||
|
||||
plt.show()
|
||||
|
||||
def simulate_NFDBP(N):
|
||||
"""
|
||||
|
@ -219,6 +236,7 @@ def stats_NFDBP(R, N, t_i):
|
|||
"""
|
||||
print("## Running {} NFDBP simulations with {} items".format(R, N))
|
||||
# TODO comment this function
|
||||
T1=[]
|
||||
P = N * R # Total number of items
|
||||
I = []
|
||||
H = [[] for _ in range(N)] # List of empty lists
|
||||
|
@ -235,6 +253,7 @@ def stats_NFDBP(R, N, t_i):
|
|||
for k in range(N):
|
||||
T.append(0)
|
||||
T = sim["T"]
|
||||
T1.append(sim["T"][0])
|
||||
for n in range(N):
|
||||
H[n].append(sim["H"][n])
|
||||
Tk[n].append(sim["T"][n])
|
||||
|
@ -244,7 +263,7 @@ def stats_NFDBP(R, N, t_i):
|
|||
Sum_T = [
|
||||
x * 100 / (sum(Sum_T)) for x in Sum_T
|
||||
] # Pourcentage de la repartition des items
|
||||
|
||||
T1=[x/100 for x in T1]
|
||||
print("Mean number of bins : {} (variance {})".format(mean(I), variance(I)))
|
||||
|
||||
for n in range(N):
|
||||
|
@ -258,6 +277,7 @@ def stats_NFDBP(R, N, t_i):
|
|||
E = 0
|
||||
sigma2 = 0
|
||||
# print(T_maths)
|
||||
T_maths = [x * 100 for x in T_maths]
|
||||
for p in range(len(T_maths)):
|
||||
E = E + (p + 1) * T_maths[p]
|
||||
sigma2 = ((T_maths[p] - E) ** 2) / (len(T_maths) - 1)
|
||||
|
@ -266,7 +286,7 @@ def stats_NFDBP(R, N, t_i):
|
|||
t_i, E, sqrt(sigma2)
|
||||
)
|
||||
)
|
||||
T_maths = [x * 100 for x in T_maths]
|
||||
# T_maths = [x * 100 for x in T_maths]
|
||||
# Plotting
|
||||
fig = plt.figure()
|
||||
# T plot
|
||||
|
@ -322,8 +342,9 @@ def stats_NFDBP(R, N, t_i):
|
|||
bx.legend(loc="upper right", title="Legend")
|
||||
|
||||
# Loi mathematique
|
||||
print("ici")
|
||||
print(T_maths)
|
||||
cx = fig.add_subplot(224)
|
||||
cx = fig.add_subplot(223)
|
||||
cx.bar(
|
||||
x,
|
||||
T_maths,
|
||||
|
@ -343,6 +364,30 @@ def stats_NFDBP(R, N, t_i):
|
|||
cx.set_xlabel("Bins i=(1-{})".format(N))
|
||||
cx.set_title("Theoretical T{} values in %".format(t_i))
|
||||
cx.legend(loc="upper right", title="Legend")
|
||||
|
||||
dx = fig.add_subplot(224)
|
||||
dx.hist(
|
||||
T1,
|
||||
bins=10,
|
||||
width=1,
|
||||
label="Empirical values",
|
||||
edgecolor="blue",
|
||||
linewidth=0.7,
|
||||
color="black",
|
||||
)
|
||||
dx.set(
|
||||
xlim=(0, 10),
|
||||
xticks=np.arange(0, 10,1),
|
||||
ylim=(0, 100),
|
||||
yticks=np.linspace(0, 100, 10),
|
||||
)
|
||||
dx.set_ylabel("Number of items in T1 for {} iterations")
|
||||
dx.set_xlabel("{} iterations for T{}".format(R,1))
|
||||
dx.set_title(
|
||||
"T{} items repartition {} items (Number of items in each bin)".format(1, P)
|
||||
)
|
||||
dx.legend(loc="upper right", title="Legend")
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue