import pandas as pan #pour les donnÃ©es :gestion des data.frame
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats.mstats as ms
import scipy.stats as st


###### Exo 1
df=pan.read_csv("/home/Enseignement/Cours/StatInfo/tpnote2024/Data68935.csv",sep="\t")


df=pan.read_csv("http://tinyurl.com/y39an7ef/Data68935.csv",sep="\t")
for nom in df.keys():
    globals()[nom] = df[nom]
    
df.keys()

#1 (1 point)  Date est qualitative, toutes les autres sont quantitatives continues (1 point)

#2 (1 point)
Bool2017=[((df["Date"])[i])[6:10]=="2017" for i in range(len(df))]
#liste avec Vrai si la ième observation est en 2017

len(df[Bool2017])#347 jours en 2017

#3 (2 points)


def Seuil(x):
    if x> 5: 
        return(2)
    elif x>2:
        return(1)
    else:
        return(0)
        

SeuilEthane=[Seuil(Ethane[i]) for i in range(len(Benzene)) ]
sum(SeuilEthane)#618

table=pan.crosstab(SeuilEthane, columns="SeuilEthane",normalize=True)
print(table)
#col_0  SeuilBenzene
#row_0              
#0               0.291
#1               0.545
#2               0.164

#4  (1 point) 

fig,ax=plt.subplots()
ax.bar(['m<2 µg','5  µg>m>2 µg','m>5 µg'],height=table['SeuilEthane'])
fig.suptitle("Diagramme en Tuyau d'orgue Des dépassements des Seuils d'Éthane à Vernaison")


#5  (1 point) 
Ethane.describe()

np.mean(Ethane)#3.469
np.var(Ethane)#biaisé 5.746
np.var(Ethane,ddof=1)#non-biaisé 5.754

np.quantile(Ethane,0.25,interpolation='lower')#1.8


#6 (2 points)
x=np.log(Ethane)

st.t.interval(0.90,df=len(x)-1,loc=np.mean(x),scale=st.sem(x))
#(1.032, 1.104)

#7 (2 points)
y=np.log(Propane)
np.corrcoef(x,y)#0.809 significativement non nulle, O
#st.pearsonr(x,y)  (0.8086133448734292, 8.456198861616579e-165) la régression est très significative

a,b=ms.linregress(x,y)[0:2]
print("y=",a,"*x+",b)
#y= 1.121 *x+ -0.3115888239914747

#8 (1 point) 

fig,ax=plt.subplots()
ax.plot(x,y,'.b')
ax.axline(xy1=(0,b),slope=a,color="red")
fig.suptitle("Nuage de points du Propane en fonction de l'Ethane et droite de régression")



###### Exo 2

#1 (1 point)
N=500
X=st.expon.rvs(loc=0,scale=1,size=N)




#2 (2 points)
Y=np.cumsum(X)/np.arange(1,N+1)
fig,ax=plt.subplots()
ax.plot(np.arange(1,N+1),Y)
fig.suptitle("Illustration de la LGN pour variables E(1)")
#3 (2 points) La limite est l'espérance 1 d'une loi E(1), on illustre la loi des grands nombres. 



#4 (1 point)

Z=np.ceil(X)

#5  (2 points)
table=pan.crosstab(Z, columns="Z",normalize=True)
table.plot.bar()

#Autre solution
fig,ax=plt.subplots()
z=np.arange(int(np.min(Z)),int(np.max(Z)+1))
ax.bar(z,np.reshape(table.values,len(table)),width=0.1)
fig.suptitle("Diagramme en bâton de Z")
       
#6 (2 points)
m=np.mean(Z);print(m)#proche de 1.5, on trace une loi géométrique de paramètre 1/np.mean(X)=0.67


fig,ax=plt.subplots()
z=np.arange(int(np.min(Z)),int(np.max(Z)+1))
ax.bar(z,np.reshape(table.values,len(table)),width=0.1)
fig.suptitle("Diagramme en bâton de Z")
ax.plot(z,st.geom.pmf(z,p=1/m),'or')
fig.suptitle("Diagramme en bâton de X comparé à loi G(0.67)")

