#################################################### # ESERCITAZIONE 1 #################################################### ##################################################### # ESERCIZIO 1 # Calcolo di quantili ##################################################### ## Normale media=0 varianza=1 help(qnorm) # P(X<x)=0.95 qnorm(0.95, mean = 0, sd = 1) qnorm(0.95, 0, 1, lower.tail = TRUE) qnorm(0.95) # P(X>x)=0.07 qnorm(1-0.07,0,1) qnorm(0.07,0,1, lower.tail = FALSE) ## Normale media=5 varianza=9 # P(X<x)=0.95 qnorm(0.95, mean = 5, sd = sqrt(9)) # P(X>x)=0.07 qnorm(0.07,5,sqrt(9),FALSE) ## t di student df=9 help(qt) # P(X<x)=0.95 qt(0.95, df = 9) # P(X>x)=0.07 qt(0.07,9, , lower.tail = FALSE) #---------------------------------------## Chi quadro df=12 help(qchisq) # P(X<x)=0.95 qchisq(0.95, df=12) # P(X>x)=0.07 qchisq(0.07, 12, , lower.tail = FALSE) ## F df1 = 4 df2 = 7 help(qf) # P(X<x)=0.95 qf(0.95, df1 = 4, df2 = 7) # P(X>x)=0.07 qf(0.07, df1 = 4, df2 = 7, , FALSE) ##################################################### # ESERCIZIO 2 # Generazione di numeri casuali ##################################################### ## Normale media=0 varianza=1 rnorm(n = 1, mean = 0, sd = 1) rand_Nst <- rnorm(n = 1000, mean = 0, sd = 1) mean(rand_Nst) sd(rand_Nst) ## Binomiale n.prove=20, p=0.3 help(rbinom) rand_B <- rbinom(n = 1000, size = 20, prob = 0.30) head(rand_B) #----------------------------------------## Normale media=5 varianza=9 rand_N <- rnorm(n = 1000, mean = 5, sd = sqrt(9)) mean(rand_N) sd(rand_N) ## t di student df=9 rand_t <- rt(n = 1000, df = 9) mean(rand_t) ## Chi quadro df=12 rand_chi <- rchisq(n = 1000, df = 12) mean(rand_chi) var(rand_chi) ## F df1=4 df2=7 rand_F <- rf(n = 1000, df1 = 4, df2 = 7) head(rand_F) ##################################################### # ESERCIZIO 3 # Calcolo delle funzioni di densità e # di probabilità cumulate ##################################################### ## Normale media=0 varianza=1 # f(1.64) dnorm(x=1.64, mean=0, sd=1) # P(X<2.3) pnorm(q = 2.3, mean = 0, sd = 1, lower.tail = TRUE) # P(X>1.5) pnorm(q = 1.5, mean = 0, sd = 1, lower.tail = FALSE) # P(-2.4<X<3.2) a <- pnorm(q = 3.2, mean = 0, sd = 1) b <- pnorm(q = -2.4, mean = 0, sd = 1) c <- a-b c ## Binomiale n.prove=20 e p=0.30 # P(X=6) dbinom(x = 6, size = 20, prob = 0.30) # P(X<=5) pbinom(q = 5, size = 20, prob = 0.30, TRUE) # P(X>=13) pbinom(q = 13, size = 20, prob = 0.30, FALSE) # P(X>13) pbinom(q = 14, size = 20, prob = 0.30, FALSE) #----------------------------------------# Normale media=0 varianza=1 # P(X>1.5) 1-pnorm(q = 1.5, mean = 0, sd = 1) # P(X<-2.7) pnorm(q = -2.7, mean = 0, sd = 1) # P(X>-1) pnorm(q = -1.0, mean = 0, sd = 1, FALSE) ## Binomiale n.prove=20 e p=0.30 # P(2<=X<=7) d <- pbinom(q = 7, size = 20, prob = 0.30) e <- pbinom(q = 2, size = 20, prob = 0.30) f <- d - e f ## Altre distribuzioni # t di student df=9 dt(x = 2, df = 9) pt(q=2, df=9) # Chi quadro df=12 dchisq(x = 3, df = 12) pchisq(q = 3, df = 12) # F df1 = 4, df2 = 7 df(x = 5, df1 = 4, df2 = 7) pf(q = 5, df1 = 4, df2 = 7) ##################################################### # ESERCIZIO 4 # Rappresentazioni grafiche ##################################################### ## Normale media=0 varianza=1 # funzione di densità di probabilità yNst <- dnorm(x = rand_Nst, mean = 0, sd = 1) plot(rand_Nst, yNst, type = 'p') plot hist(rand_Nst) # funzione di ripartizione yNst_c <- pnorm(q = rand_Nst, mean = 0, sd = 1) plot(rand_Nst, yNst_c, type = 'p') ## Binomiale n.prove=20, p=0.30 # funzione di probabilità yBin <- dbinom(x = rand_B, size = 20, prob = 0.30) hist(rand_B) plot(rand_B, yBin, type = 'p') # funzione di ripartizione yBin_c <- pbinom(q = rand_B, size = 20, prob = 0.30, TRUE) plot(rand_B, yBin_c, type = 'p') # -------------------------------------------yChi <- dchisq(x = rand_chi, df = 12) yChi_c <- pchisq(q = rand_chi, df = 12) plot(rand_chi, yChi, type = 'p') hist(rand_chi) plot(rand_chi, yChi_c, type = 'p') ##################################################### # ESERCIZIO 5 # Combinazione lineare di variabili casuali ##################################################### # V.C. Normale(5, 2) rand_X <- rnorm(n = 1000, mean = 5, sd = sqrt(2)) # V.C. Normale(7, 4) rand_Y <- rnorm(n = 1000, mean = 7, sd = sqrt(4)) # Z come combinazione lineare di X e Y Z <- 2*rand_X + 3*rand_Y mean(Z) var(Z) ##################################################### # ESERCIZIO 6 # Calcolo probabilità marginali e condizionate #################################################### # Matrice di frequenze assolute osservate: X = matrix(round(runif(12, min=0, max=100)), nrow = 3, ncol = 4) # Distribuzione delle freq. marginali assolute di colonna rowSums(X) # Distribuzione delle freq. marginali assolute di riga colSums(X) # Matrice delle prob. condizionate di riga prop.table(X,1) rowSums(prop.table(X,1)) # Ok # matrice delle prob. condizionate di colonna prop.table(X,2) colSums(prop.table(X,2)) # Ok # Matrice delle prob. congiunte prop.table(X) ###################################################### # ESERCIZIO 7 # Normale multivariata ###################################################### # Istallare il pacchetto mvtnorm install.packages("mvtnorm") require(mvtnorm) # Fissiamo media e varianza mu = c(1,-1) Si = matrix(c(4,1,1,1),2,2) # Calcoliamo funzione di densità in un punto f = dmvnorm(c(0,0),mu,Si) # Rappresentiamo grafico densità x = seq(-6,9,0.25); nx = length(x) y = seq(-6,9,0.25); ny = length(y) Z = matrix(0,nx,ny) for(i in 1:nx) for(j in 1:ny) Z[i,j] = dmvnorm(c(x[i],y[j]),mu,Si) persp(x,y,Z) # phi, theta persp(x,y,Z,phi=20) persp(x,y,Z,phi=-30) persp(x,y,Z,theta=10) persp(x,y,Z,phi=20,theta=10) persp(x,y,Z,phi=20,theta=40) persp(x,y,Z,phi=20,theta=40, col = "lightblue") contour(x,y,Z) #----------------------------------------------------------------# Provare altra varianza Si2 = matrix(c(4,0,0,1),2,2) Z = matrix(0,nx,ny) for(i in 1:nx) for(j in 1:ny) Z[i,j] = dmvnorm(c(x[i],y[j]),mu,Si2) contour(x,y,Z) #-----------------------------------------------------------------# Estrazione numeri casuali da una normale doppia Si2 = matrix(c(4,0,0,1),2,2) X2 = rmvnorm(1000,mu,Si2) colMeans(X2) cov(X2) cor(X2) ###################################################### # ESERCIZIO 8 # Verosimiglianza ###################################################### ## Rappresentazione verosimiglianza Bernoulli # Estraiamo 100 osservazioni y = 1*(runif(100)>0.5) p = 0.45 f = dbinom(y,1,p) L = prod(f) # Rappresentiamo likelihood pv = seq(0,1,0.001); np = length(pv) Lv = rep(0,np) for(i in 1:np) Lv[i] = prod(dbinom(y,1,pv[i])) plot(pv,Lv,type="l") # Rappresentiamo log-likelihood lv = log(Lv) plot(pv,lv,type="l") # massimo vicino a 0.5 phat = sum(y)/100 ## Rappresentazione verosimiglianza Normale # Estraiamo 100 osservazioni y = rnorm(50) mu = -0.5 f = dnorm(y,mu) L = prod(f) # Rappresentiamo likelihood muv = seq(-1,1,0.01); np = length(muv) Lv = rep(0,np) for(i in 1:np) Lv[i] = prod(dnorm(y,muv[i])) plot(muv,Lv,type="l") # Rappresentiamo log-likelihood lv = log(Lv) plot(muv,lv,type="l") # Massimo vicino a 0 muhat = sum(y)/50 si2hat = sum((y-muhat)^2)/50