This commit is contained in:
Clément Lacau 2023-06-04 23:12:47 +02:00
parent 3a50f1d83d
commit 839b6f79ec

View file

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
from random import random from random import random
from math import floor, sqrt, factorial from math import floor, sqrt, factorial,exp
from statistics import mean, variance from statistics import mean, variance
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from pylab import * 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... 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. 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 P = R * N # Total number of items
print("## Running {} NFBP simulations with {} items".format(R, N)) print("## Running {} NFBP simulations with {} items".format(R, N))
# number of bins # number of bins
@ -106,17 +110,26 @@ def stats_NFBP_iter(R, N):
print("Mean number of bins : {} (variance {})".format(I, IVariance), "\n") print("Mean number of bins : {} (variance {})".format(I, IVariance), "\n")
# TODO clarify line below # TODO clarify line below
print(" {} * {} iterations of T".format(R, N), "\n") print(" {} * {} iterations of T".format(R, N), "\n")
for n in range(N):
for n in range(min(N, 10)):
Hn = HSum[n] / R # moyenne Hn = HSum[n] / R # moyenne
HVariance = sqrt(HSumVariance[n] / (R - 1) - Hn**2) # Variance HVariance = sqrt(HSumVariance[n] / (R - 1) - Hn**2) # Variance
Var.append(HVariance)
H.append(Hn)
print( print(
"Index of bin containing the {}th item (H_{}) : {} (variance {})".format( "Index of bin containing the {}th item (H_{}) : {} (variance {})".format(
n, n, Hn, HVariance 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] HSum = [x / R for x in HSum]
# print(HSum) HSumVariance = [x / R for x in HSumVariance]
print(HSumVariance)
# Plotting # Plotting
fig = plt.figure() fig = plt.figure()
# T plot # T plot
@ -176,13 +189,17 @@ def stats_NFBP_iter(R, N):
cx.set_xlabel("n-item (1-{})".format(N)) cx.set_xlabel("n-item (1-{})".format(N))
cx.set_title("H histogram for {} items".format(P)) cx.set_title("H histogram for {} items".format(P))
xb = linspace(0, N, 10) xb = linspace(0, N, 10)
yb = Hn * xb / 10 xc=linspace(0,N,50)
wb = HVariance * xb / 10 yb = [Hmean for n in range(N)]
cx.plot(xb, yb, label="Theoretical E(Hn)", color="brown") db =(( HSum[30] - HSum[1])/30)*xc
cx.plot(xb, wb, label="Theoretical V(Hn)", color="purple") 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") cx.legend(loc="upper left", title="Legend")
plt.show()
plt.show()
def simulate_NFDBP(N): def simulate_NFDBP(N):
""" """
@ -219,6 +236,7 @@ def stats_NFDBP(R, N, t_i):
""" """
print("## Running {} NFDBP simulations with {} items".format(R, N)) print("## Running {} NFDBP simulations with {} items".format(R, N))
# TODO comment this function # TODO comment this function
T1=[]
P = N * R # Total number of items P = N * R # Total number of items
I = [] I = []
H = [[] for _ in range(N)] # List of empty lists 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): for k in range(N):
T.append(0) T.append(0)
T = sim["T"] T = sim["T"]
T1.append(sim["T"][0])
for n in range(N): for n in range(N):
H[n].append(sim["H"][n]) H[n].append(sim["H"][n])
Tk[n].append(sim["T"][n]) Tk[n].append(sim["T"][n])
@ -244,7 +263,7 @@ def stats_NFDBP(R, N, t_i):
Sum_T = [ Sum_T = [
x * 100 / (sum(Sum_T)) for x in Sum_T x * 100 / (sum(Sum_T)) for x in Sum_T
] # Pourcentage de la repartition des items ] # Pourcentage de la repartition des items
T1=[x/100 for x in T1]
print("Mean number of bins : {} (variance {})".format(mean(I), variance(I))) print("Mean number of bins : {} (variance {})".format(mean(I), variance(I)))
for n in range(N): for n in range(N):
@ -258,6 +277,7 @@ def stats_NFDBP(R, N, t_i):
E = 0 E = 0
sigma2 = 0 sigma2 = 0
# print(T_maths) # print(T_maths)
T_maths = [x * 100 for x in T_maths]
for p in range(len(T_maths)): for p in range(len(T_maths)):
E = E + (p + 1) * T_maths[p] E = E + (p + 1) * T_maths[p]
sigma2 = ((T_maths[p] - E) ** 2) / (len(T_maths) - 1) 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_i, E, sqrt(sigma2)
) )
) )
T_maths = [x * 100 for x in T_maths] # T_maths = [x * 100 for x in T_maths]
# Plotting # Plotting
fig = plt.figure() fig = plt.figure()
# T plot # T plot
@ -322,8 +342,9 @@ def stats_NFDBP(R, N, t_i):
bx.legend(loc="upper right", title="Legend") bx.legend(loc="upper right", title="Legend")
# Loi mathematique # Loi mathematique
print("ici")
print(T_maths) print(T_maths)
cx = fig.add_subplot(224) cx = fig.add_subplot(223)
cx.bar( cx.bar(
x, x,
T_maths, T_maths,
@ -343,6 +364,30 @@ def stats_NFDBP(R, N, t_i):
cx.set_xlabel("Bins i=(1-{})".format(N)) cx.set_xlabel("Bins i=(1-{})".format(N))
cx.set_title("Theoretical T{} values in %".format(t_i)) cx.set_title("Theoretical T{} values in %".format(t_i))
cx.legend(loc="upper right", title="Legend") 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() plt.show()