You are on page 1of 5

Série N°3

For revision purposes kindly don't use in exams.


M.T.T.M.

#Exercice1
1.
mk<-function(x,k)
{
n<-length(x)
s<-sum((x-mean(x))^k))/n
return(s)
}
2.
skew1<-function(x)
{
s1<-mk(x,3)/(sd(x)^3)
return(s1)
}
kurt1<-unction(x)
{
k1<-mk(x,4)/(sd(x)^4)
return(k1)
}
3.
swek2<-function(x)
{
n<-length(x)
s2<-(n/((n-1)*(n-2))*((sum((x-mean(x))^3)/(sd(x)^3))
return(s2)
}
kurt2<-function(x)
{
n<-length(x)
k2<-((n*(n-1))/((n-1)*(n-2)*(n-3)))*((sum(x-mean(x))^4)/(sd(x)^4))
return(k2)
}
4.
x<-rnorm(100);x
y<-rexp(100,2);y
mk(x,2);mk(y,2)
skew1(x);skew2(x)
skew1(y);skew2(y)
kurt1(y);kurt2(y)
kurt1(x);kurt2(x)

#Exercice2
1.
In<-function(n)
{
x<-runif(n,0,2)
s<-sum(x^2)/n
return(s)
Série N°3
For revision purposes kindly don't use in exams.
M.T.T.M.

x<-seq(100,10000,100);x
y<-NULL
for(i in 1:100)
{
y[i]<-In(x[i]) #In(i*100)
};y
plot(x,y, type="l", main = "Representation de l'evolution")
#Exercice3
1.
sp<-function(alpha, N)
{
s<-cumsum(1/((1:N)^alpha))
return(s)
}
2.
sp(0.5,100)
sp(1,100)
sp(2,100)
sp(5,100)
plot(sp(0.5,100), type="l")
lines(sp(1,100), col = 2)
lines(sp(2,100), col = 3)
lines(sp(5,100), col = 4)
legend("topleft", c("alpha=0.5","alpha=1", "alpha=2", "alpha=5"), col =1:4, lty=1)

#Exercice4
lv<-function(x,a,b)
{
s<-sum(dweibull(x,a,b,log = TRUE))*(-1)
return(s)
}
#Ou bien
lv1<-function(x,a,b)
{
s<-sum(log(prod(dweibull(x,a,b)))*(-1)
return(s)
}

#Exercice5
1. #Code
x<-rnorm(1,0,1)
i=1
while(x<=1)
{
x<-rnorm(1,0,1)
i<-i+1
} print(c(x,i))
Série N°3
For revision purposes kindly don't use in exams.
M.T.T.M.

2. #n premiers simulations
sim<-function(n,m,sigma)
{
y<-rep(0,n) #ou bien y<-NULL
for(i in 1:n)
{
x<-rnorm(1,m,sigma)
while(x<=1)
{
x<-rnorm(1,m,sigma)
}
y[i]<-x
}
return(y)
}

#Exercice8
1. X v.a de fct de rép. F, F est continue [car: exp(-x) est continue] et strictement croissant=>F
est bijective.
En calculons F^(-1)[x=-ln(sqrt((1/y))-1)], alors la methode de l'inverse est applicanle.
2.
sim<-function(n)
{
set.seed(100) #Pour avoir même point de depart
u<-runif(n) #Ou bien runif(n,0,1)
x<--log(sqrt(1/u)-1)
return(x)
}

Y<-function(n)
{
X<-sim(n)
s<-log(1+exp(-X))
return(s)
}
#Exemple
n=10
sim(n)
Y(n)

#Exercice9
1.
#Intergrale d'une densité = 1
#c=4/3
# X est un v.a à support bornée de densité de f.
# f(x) admet un maximum en x= 1/2 donc X simulable par methode de reject
2.
sim<-function(n)
{
Série N°3
For revision purposes kindly don't use in exams.
M.T.T.M.

v<-rep(0,n)
for(i in 1:n)
{
u1<-runif(1,0,1) #ici a=0,b=1
u2<-runif(1,0,4/3) #Max = M= f(1/2)
while(u2>=4/3*(1-3*(u1-1/2)^2))
{
u1<-runif(1,0,1)
u2<-runif(1,0,4/3)
}
v[i]<-u1
}
return(v)
}
3.
hist(sim(1000), prob= TRUE)
curve(4/3*(1-3*(x-1/2)^2), add = TRUE, col =2)

#Exercice12
sim<-function(n)
{
v<-rep(0,n) #ou bien v<-NULL
c<-sqrt((2*pi)/exp(1))
for(i in 1:n)
{
x<-rcauchy(1)
u<-runif(1,0,c*dcauchy(x))
while(u>=dnorm(x))
{
x<-rcauchy(1)
u<-runif(1,0,c*dcauchy(x))
}
v[i]<-x
}
return(x)
}

#Exercice 13
1.
#On a les bornee finie [0,2] en pour utiliser loi uniforme
#E(2x^2) tq X~>U[0,2]

#Code R (Exemple)
x<-runif(100,0,2)
sum(2*(x^2))/100 #ou bien direct mean(2*(x^2))
mean(2*(x^2))
2.
#[0,+inf] en pour utiliser loi exponentielle
#E[sin(x)/x*f(x)/exp(-x)] tq exp(1) remaque dans cas f(x) est la densité d'une loi Student.
Série N°3
For revision purposes kindly don't use in exams.
M.T.T.M.

#Code R (Exemple)
x<-rexp(100,1)
I<-mean((sin(x)/x)*(dt(x,1)/exp(-1)));I

Related Interests