diff --git a/Chegodaiev.py b/Chegodaiev.py new file mode 100644 index 0000000000000000000000000000000000000000..064c4e165ba90eafdf02d76d30dd3357dc8cfcdb --- /dev/null +++ b/Chegodaiev.py @@ -0,0 +1,144 @@ +""" +CPoulard Tchégodaiev / Hazen +""" + + +from matplotlib import pyplot as plt +from matplotlib.widgets import Slider, CheckButtons +import numpy as np + +a = 0.5 +b = 0.5 +N = 10 +echantillon = sorted(range(1, N+1), reverse=True) +en_periode_de_retour = True + +def T_emp_Tchego(n_annees): + + plotting_positions_Tchego = [((n_annees + 0.4) / ((indice + 1) - 0.3)) for indice in range(n_annees)] + print("Tchégo ", plotting_positions_Tchego) + return plotting_positions_Tchego + +def T_emp_parametre(n_annees, a , b): + + plotting_positions_param = [((n_annees + b) / ((indice + 1) - a)) for indice in range(n_annees)] + print("Paramétré par curseur ", plotting_positions_param) + return plotting_positions_param + + +def retracer_Tchego(echantillon): + if en_periode_de_retour: + courbe_estim_Tchego.set_data(T_emp_Tchego(len(echantillon)), echantillon) + else: + courbe_estim_Tchego.set_data([ 1 - (1 / T) for T in T_emp_Tchego(len(echantillon))], echantillon) + + +def retracer_parametree(echantillon, a, b): + + T_emp = T_emp_parametre(len(echantillon), a,b) + + if en_periode_de_retour: + courbe_estim_param.set_data(T_emp, echantillon) + ax.set_title(f"Estimations de T empirique\n de Tchegodayev et paramétrée a={a:.2f} et b={b:.2f} ") + ax.set_xlim(xmin=min(T_emp), xmax=max(T_emp) * 1.1) + else: + freq = [ 1 - (1 / T) for T in T_emp] + courbe_estim_param.set_data(freq, echantillon) + ax.set_title(f"Estimations de la fréquence empirique\n de Tchegodayev et paramétrée a={a:.2f} et b={b:.2f} ") + ax.set_xlim(xmin=min(freq), xmax=max(freq) * 1.1) + #ax.set_xbounds(lower=min(T_emp), upper=max(T_emp)) + + fig_pp.canvas.draw_idle() + +# https://matplotlib.org/stable/gallery/widgets/slider_demo.html +# https://stackoverflow.com/questions/13656387/can-i-make-matplotlib-sliders-more-discrete +def update_a(val): + global a + a = val + retracer_parametree(echantillon, a, b) + +def update_b(val): + global b + b = val + retracer_parametree(echantillon, a, b) + +def update_N(val): + global N, echantillon + N = int( val) + echantillon = sorted(range(1, N+1), reverse = True) + retracer_parametree(echantillon, a, b) + retracer_Tchego(echantillon) + ax.set_ylim(bottom=0, top=N*1.1) + fig_pp.canvas.draw_idle() + +def switch_freq(label): + global en_periode_de_retour + + en_periode_de_retour = not en_periode_de_retour + retracer_parametree(echantillon, a, b) + retracer_Tchego(echantillon) + #ax.set_ylim(bottom=0, top=N * 1.1) + + fig_pp.canvas.draw_idle() + +#plt.ion() + +print(echantillon) +print(a, b) + + +fig_pp, (ax, ax_slide_a, ax_slide_b, ax_slide_n, ax_chb) = plt.subplots(nrows=5, ncols=1, gridspec_kw={'height_ratios': [10, 1, 1, 1,2]}) +plt.subplots_adjust(wspace=1, hspace=0.5,left=0.1,top=0.85,right=0.9,bottom=0.1) +fig_pp.canvas.set_window_title("Démo périodes de retour 'empiriques'") + +fig_pp.suptitle(f"les valeurs importent peu, seul leur rang est utilisé ! ") + +ax.set_xlabel("Période de retour (années)") + +courbe_estim_Tchego, = ax.plot(T_emp_Tchego(N), echantillon, color="red", alpha=0.7, linewidth=3, solid_capstyle='round', + zorder=2, label= "Tchegodaiev", marker="o" , ls="--", markersize=7) +courbe_estim_param, = ax.plot(T_emp_parametre(N, a, b), echantillon, color='blue', alpha=0.7, linewidth=5, solid_capstyle='round', + zorder=10, label = "paramétrée", marker="x" , ls=':', markersize=7) + + +for ax_s in [ax_slide_a, ax_slide_b, ax_slide_n, ax_chb]: + ax_s.xaxis.set_visible(False) + ax_s.yaxis.set_visible(False) + #for pos in ['right', 'top', 'bottom', 'left']: + # ax_s.spines[pos].set_visible(False) + +print("ici") + +# nom connu même hors de la fonction pour éviter le GC ? +# a +slider_a = Slider( + ax_slide_a, "paramètre a ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=0.5, color="green") + +slider_a.on_changed(update_a) + +# b +slider_b = Slider( + ax_slide_b, "paramètre b ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=0.5, color="blue") + +slider_b.on_changed(update_b) + +# N +valeurs_possibles_b = np.linspace(0, 100, 1) +slider_n = Slider( + ax_slide_n, "paramètre N ", valmin=5, valmax = 200, valfmt='%0.0f', valinit=N, valstep=valeurs_possibles_b, color="purple") + +slider_n.on_changed(update_N) + +# freq +valeurs_possibles_b = np.linspace(0, 100, 1) +chb_enT = CheckButtons(ax_chb, [ "En période de retour (années)"], actives=[0]) + +chb_enT.on_clicked(switch_freq) + + +ax.legend(title="formules", bbox_to_anchor=(0.85, 0.10), loc='lower right') +#plt.tight_layout() +#fig_pp.savefig("ma_figure.png") +#fig_pp.canvas.draw() +plt.show() +#plt.ioff()