Added many thing, check the code Paul

This commit is contained in:
Clément Lacau 2023-06-04 00:28:24 +02:00
parent 7804bbfc43
commit 8284d7bf03

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 from math import floor, sqrt,factorial
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 *
@ -185,55 +185,77 @@ def stats_NFDBP(R, N,t_i):
T=[] T=[]
Tk=[[] for _ in range(N)] Tk=[[] for _ in range(N)]
Ti=[] Ti=[]
T_maths=[]
#First iteration to use zip after #First iteration to use zip after
sim=simulate_NFDBP(N) sim=simulate_NFDBP(N)
Sum_T=sim["T"] Sum_T=[0 for _ in range(N)]
for i in range(R): for i in range(R):
sim = simulate_NFDBP(N) sim = simulate_NFDBP(N)
I.append(sim["i"]) I.append(sim["i"])
for k in range(N):
T.append(0)
T=sim["T"]
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])
T=sim["T"]
Ti.append(sim["T"]) Ti.append(sim["T"])
for k in range(N):
Sum_T.append(0)
T.append(0)
Sum_T=[x+y for x,y in zip(Sum_T,T)] Sum_T=[x+y for x,y in zip(Sum_T,T)]
Sum_T=[x/R for x in Sum_T] #Experimental [Ti=k] Sum_T=[x/R for x in Sum_T] #Experimental [Ti=k]
Sum_T=[x*100/(sum(Sum_T)) for x in Sum_T] #Pourcentage de la repartition des items Sum_T=[x*100/(sum(Sum_T)) for x in Sum_T] #Pourcentage de la repartition des items
print(Tk)
print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I))) print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I)))
for n in range(N): for n in range(N):
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n]))) print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
print("Mean T_{} : {} (variance {})".format(k, mean(Sum_T), variance(Sum_T))) print("Mean T_{} : {} (variance {})".format(k, mean(Sum_T), variance(Sum_T)))
#Loi math
for u in range(N):
u=u+2
T_maths.append(1/(factorial(u-1))-1/factorial(u))
E=0
sigma2=0
print("hep")
print(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)
print("Mathematical values : Empiric mean T_{} : {} Variance {})".format(t_i, E, sqrt(sigma2)))
T_maths=[x*100 for x in T_maths]
#Plotting #Plotting
fig = plt.figure() fig = plt.figure()
#T plot #T plot
x = np.arange(N) x = np.arange(N)
print(x) print(x)
ax = fig.add_subplot(121) print(Sum_T)
ax = fig.add_subplot(221)
ax.bar(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red') ax.bar(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,3), yticks=np.linspace(0, 3, 5)) ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,20), yticks=np.linspace(0, 20, 2))
ax.set_ylabel('Items') ax.set_ylabel('Items(n) in %')
ax.set_xlabel('Boxes (1-{})'.format(N)) ax.set_xlabel('Boxes (1-{})'.format(N))
ax.set_title('T histogram for {} packages (Number of packages in each box)'.format(P)) ax.set_title('Items percentage for each box and {} packages (Number of packages in each box)'.format(P))
ax.legend(loc='upper left',title='Legend') ax.legend(loc='upper left',title='Legend')
#Mathematical P(Ti=k) plot. It shows the Ti(t_i) law with the probability of each number of items.
print(len(Tk[t_i]))
bx = fig.add_subplot(222)
bx.hist(Tk[t_i],bins=10, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
bx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,len(Tk[t_i])), yticks=np.linspace(0, 1, 1))
bx.set_ylabel('P(T{}=i)'.format(t_i))
bx.set_xlabel('Boxes i=(1-{}) in %'.format(N))
bx.set_title('T{} histogram for {} packages (Number of packages in each box)'.format(t_i,P))
bx.legend(loc='upper left',title='Legend')
#Loi mathematique
print(T_maths)
cx = fig.add_subplot(224)
cx.bar(x,T_maths, width=1,label='Theoretical values', edgecolor="blue", linewidth=0.7,color='red')
cx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,100), yticks=np.linspace(0, 100, 10))
cx.set_ylabel('P(T{}=i)'.format(t_i))
cx.set_xlabel('Boxes i=(1-{})'.format(N))
cx.set_title('Theoretical T{} values in %'.format(t_i))
cx.legend(loc='upper left',title='Legend')
plt.show() plt.show()
#Mathematical P(Ti=k) plot
x = np.arange(N)
print(x)
ax = fig.add_subplot(122)
ax.hist(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,3), yticks=np.linspace(0, 3, 5))
ax.set_ylabel('Items')
ax.set_xlabel('Boxes (1-{})'.format(N))
ax.set_title('T histogram for {} packages (Number of packages in each box)'.format(P))
ax.legend(loc='upper left',title='Legend')
plt.show()
N = 10 ** 1 N = 10 ** 1
sim = simulate_NFBP(N) sim = simulate_NFBP(N)
@ -257,10 +279,4 @@ for j in range(sim["i"] + 1):
print() print()
stats_NFBP_iter(10**3, 10) stats_NFBP_iter(10**3, 10)
#stats_NFDBP(10 ** 3, 10) stats_NFDBP(10 ** 3, 10,1)
#
#pyplot.plot([1, 2, 4, 4, 2, 1], color = 'red', linestyle = 'dashed', linewidth = 2,
#markerfacecolor = 'blue', markersize = 5)
#pyplot.ylim(0, 5)
#pyplot.title('Un exemple')
#show()