Esercitazione2.R

lorenzo — Oct 14, 2013, 8:18 AM



# definisco la directory di lavoro
setwd("/Users/lorenzo/Documents/Lavori guidati e corsi matematica/ST410 a.a. 2013-2014/Esercitazione2")

## Creazione del grafico della slide no. 1
#definisco il database reddito
income<-data.frame(nome=c("Marta", "Alessandro", "Emanuela", "Alfonso", "Jacopo", "Francesca", "Marco", "Aureliana"),reddito=c(40,22,17,19,90,100,45,4))
#lo ordino secondo i valori di reddito con la funzione order()
income<-income[order(income$reddito),]
#creo il grafico
plot(income$reddito, xaxt="s", yaxt="n",ylim=c(-5,125), xlab="", ylab="", bty="n", type="b")
abline(v=6.74,col="red", lty=1)
text(x=(1:8)-0.1,y=income$reddito+13, labels=income$reddito)
text(x=6.74,y=120,labels="p=0.82", col="blue")
text(x=6.74,y=0, labels="6.74", col="blue")
text(x=6.74,y=78.3, labels="Q(p)=78.3", col="blue")

plot of chunk unnamed-chunk-1

# definisco una matrice 2 x 5
A<-matrix(c(1:10), nrow=2, ncol=5)
# se voglio che i numeri siano disposti per riga utilizzo l'opzione byrow=TRUE
A<-matrix(c(1:10), nrow=2, ncol=5, byrow=T)

#il comando diag()
diag(10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1
diag(1:10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    2    0    0    0    0    0    0    0     0
 [3,]    0    0    3    0    0    0    0    0    0     0
 [4,]    0    0    0    4    0    0    0    0    0     0
 [5,]    0    0    0    0    5    0    0    0    0     0
 [6,]    0    0    0    0    0    6    0    0    0     0
 [7,]    0    0    0    0    0    0    7    0    0     0
 [8,]    0    0    0    0    0    0    0    8    0     0
 [9,]    0    0    0    0    0    0    0    0    9     0
[10,]    0    0    0    0    0    0    0    0    0    10
diag(matrix(1:9,3,3))
[1] 1 5 9

#Esercizio 1.1
# notare il messaggio di warning
matrix(1:5,3,3)[2,3]==3
Warning: data length [5] is not a sub-multiple or multiple of the number
of rows [3]
[1] TRUE

#Esercizio 1.2
A<-matrix(c(3,1,0,2,7,5,1,0,-3),3,3)
b<-matrix(c(8,12,3),1,3)
det(A)
[1] -52
sol<-solve(A)%*%t(b)

#Esercizio 1.3
A<-diag(cos(3*pi/5), nrow=2)
B<-matrix(c(0,sin(3*pi/5),-sin(3*pi/5),0),2,2)
A<-A+B
t(A)%*%A == diag(2)
     [,1] [,2]
[1,] TRUE TRUE
[2,] TRUE TRUE

#esempio di calcoli con la distribuzione normale
dnorm(0)
[1] 0.3989
dnorm(0,mean=1,sd=1)
[1] 0.242
dnorm(1,mean=1,sd=0)
[1] Inf
pnorm(5)
[1] 1
pnorm(-5)
[1] 2.867e-07
qnorm(0.95)
[1] 1.645
rnorm(10,mean=1,sd=1)
 [1] -0.1408  0.5887  0.9610  1.2082  1.1278 -0.2666  0.4422  1.6717
 [9]  0.4002  2.0625
rnorm(10)
 [1] -0.22957 -1.70033 -0.87690  0.87587  0.32916 -0.14855  0.33343
 [8] -0.86968 -1.43818 -0.06955


#Esercizio 2.1
binomiale<-matrix(0,4,11)
binomiale[1,]<-dbinom(0:10,10,1/100)
binomiale[2,]<-dbinom(0:10,10,1/10)
binomiale[3,]<-dbinom(0:10,10,1/2)
binomiale[4,]<-dbinom(0:10,10,99/100)
par(mfrow=c(2,2))
for (i in 1:4) plot(0:10,binomiale[i,],type="h",main=paste("Binomiale #",i), xlab="k", ylab="p")

plot of chunk unnamed-chunk-1

par(mfrow=c(1,1))
# Esercizio 2.2
plot(0:10,binomiale[1,],type="p", main="Distribuzioni binomiali", xlab="k", ylab="p")
lines(0:10,binomiale[2,],type="p", col="red")
lines(0:10,binomiale[3,],type="p", col="green")
lines(0:10,binomiale[4,],type="p", col="blue")
legend(0.2,0.9,legend=c("Binomiale 1", "Binomiale 2", "Binomiale 3", "Binomiale 4"), lty=c(1,1,1,1), ncol=2, col=c("black", "red", "green", "blue"))

plot of chunk unnamed-chunk-1


#Alcuni esempi di distribuzioni normali
curve(dnorm(x,mean=0,sd=0.5),-4,4, xlab="x", ylab="p",main="Densità di probabilità - variabili al. normali di media 0")
curve(dnorm(x,mean=0, sd=1),-4,4, add=T, col="red", lty=2)
curve(dnorm(x,mean=0, sd=2),-4,4, add=T, col="blue", lty=3)
legend(2,0.5,legend=c("sd=0.5", "sd=1", "sd=2"), lty=c(1,2,3), col=c("black", "red", "blue"))

plot of chunk unnamed-chunk-1


#Normale di media 0 e varianza 1: coda destra, coda sinistra, area centrale
curve(dnorm(x,mean=0,sd=1),-4,4,col="black", xlab="", ylab="", yaxt="n", bty="n")
curve(dnorm(x,mean=0,sd=1),-4,qnorm(0.025),type="h",n=1000,col="lightblue", add=T)
curve(dnorm(x,mean=0,sd=1),qnorm(0.975),4,type="h",n=1000,col="lightblue", add=T)
#curve(dnorm(x,mean=0,sd=1),qnorm(0.025),qnorm(0.975),type="h",n=1000,col="lightblue4", add=T)
text(-3,0.02,"2.5%")
text(3,0.02,"2.5%")
text(0,0.2,"95%")

plot of chunk unnamed-chunk-1


# legge debole dei grandi numeri
# n: numero simulazioni per ogni media campionaria
# m: numero di variabili sommate per la media campionaria
n<-1000
m<-300
a<-matrix(0,n,m)
for (i in 1:n) {for (j in 1:m) {a[i,j]<-mean(runif(j,-1,1))}}
plot(NULL,xlim=c(0,m), ylim=c(-1,1), xlab="n", ylab="Realizzazioni campionarie di M(n)", main="Simulazione: legge debole dei grandi numeri")
for (j in 1:m) {points(rep(j,n),a[,j], cex=0.1)}
abline(h=seq(-1,1,0.1),col="lightgrey", lty=2)

plot of chunk unnamed-chunk-1


# teorema limite centrale 

x<--2
y<-2
numcurve<-100
curve(pnorm(x),x,y,col="black",lwd=2,ylab="",xlab="")
#for (k in seq(1,numcurve,10)) curve(ppois((sqrt(k)*x+k),lambda=k),x,y, add=T, col=heat.colors(numcurve)[numcurve+1-k])
k<-1
curve(ppois((sqrt(k)*x+k),lambda=k),x,y, add=T, col="red")
k<-5
curve(ppois((sqrt(k)*x+k),lambda=k),x,y, add=T, col="orange")
k<-10
curve(ppois((sqrt(k)*x+k),lambda=k),x,y, add=T, col="green")
k<-200
curve(ppois((sqrt(k)*x+k),lambda=k),x,y, add=T, col="blue")

plot of chunk unnamed-chunk-1