######## jalaseye aval ######### 
################################
#### azmayeshe partabe seke ####
################################
coin <- sample(0:1, 1000000, replace = TRUE)
sum(coin)
p <- sum(coin)/length(coin)
p
################################
######### jalaseye 2 ##########
###############################
###### tamrine jalaseye 1 #####
dice_toss1 = sample(1:6, size = 100000, replace = TRUE)
#dice_toss1

x<-dice_toss1==6
#x 
#sum(x)
sum(x)/length(x)
#####nahveye mohasebeye ehtemal ba dasture table#####
table(dice_toss1)
table(dice_toss1)/ length(dice_toss1)
1/6

num_odd_dice1 = table(dice_toss1)[1]+ table(dice_toss1)[3]+ table(dice_toss1)[5]
num_even_dice1 = table(dice_toss1)[2] + table(dice_toss1)[4] + table(dice_toss1)[6]
num_odd_dice1
num_even_dice1


p_odd1 = num_odd_toss1 / lengh(dice_toss1)
p_odd1
p_even1 = num_even_toss1 / length(dice_toss1) 
p_even1


dice_toss2 = sample(1:6, size = 1000000, replace = TRUE, prob = c(1, 2, 1, 2, 1, 2))
#dice_toss2

x<-dice_toss2==6
sum(x)/length(x)
2/9






table(dice_toss2)

num_odd_dice2 = table(dice_toss2)[1]+ table(dice_toss2)[3]+ table(dice_toss2)[5]
num_even_dice2 = table(dice_toss2)[2] + table(dice_toss2)[4] + table(dice_toss2)[6]

num_odd_dice2
num_even_dice2

p_odd2 = num_odd_toss2 / lengh(dice_toss2)
p_odd2
p_even2 = num_even_toss2 / length(dice_toss2) 
p_even2
####### jalaseye 3 #######
##########################
####### mesale 2.1 #######
x<-sample(1:365, 25, replace = TRUE)
x
length(x)
y<-unique(x)
length(y)
z<-length(x)-length(y)
z
###### 
z <- 0
for(i in 1:1000000){
x<-sample(1:365, 70, replace = TRUE)
y<-unique(x)
z[i]<-length(x)-length(y)
}
#z
m<-z==0
#m
#sum(m)
p=sum(m)/length(m)
1-p 
########## tavalode nimeye aval nesbate 2 be 1 ba nime dovom darad #####
z <- 0
for(i in 1:100000){
x<-sample(1:365, 25, replace = TRUE, prob=rep(c(2,1), c(6*31, 365-6*31)))
y<-unique(x)
z[i]<-length(x)-length(y)
}
#z
m<-z==0
#m
#sum(m)
p=sum(m)/length(m)
p
1-p 
hist(z)
############# mesale 4.1 ###########
x=sample(1:6,30,replace=TRUE)
#############################################
### jalase 4
#############################################
x<-sample(1:365, 25, replace=TRUE)
x
length(x)
y<-unique(x)
length(y)
z<-length(x)-length(y)
z
#################
z<-0
for(i in 1:100000){
x<-sample(1:365, 25, replace=TRUE, prob=rep(c(2,1),c(6*31, 365-6*31)))
y<-unique(x)
z[i]<-length(x)-length(y)
}
#z
m<-z==0
#m
sum(m)
p=sum(m)/length(m)
1-p
hist(x)
############ mesal 4.1 #############
n=30
x=sample(1:6, n, replace=TRUE)
x
x<-sum(x==6)
x
phat<-x/n
phat
###### partab 20 daneshamuz ######
iter=20
alpha=0.05
zalpha<-qnorm(1-alpha/2)
phat<-0
L<-0
U<-0
for(i in 1:iter){
x=sample(1:6, n, replace=TRUE)
x<-sum(x==6)
phat[i]<-x/n
L[i]<-phat[i]-zalpha*sqrt(phat[i]*(1-phat[i])/n)
U[i]<-phat[i]+zalpha*sqrt(phat[i]*(1-phat[i])/n)
}
phat
L
U
round(cbind(phat,L,U),3)
p<-1/6
p
cover<-ifelse((p>=L & p<=U),1,0)
cover
round(cbind(phat,L,U,cover),3)
####### jalaseye 5 #######
## tolide adade tasadofi ##
### mesale 2.1 ###
d=53
a=20
b=0
seed=30
n=105
r<-numeric(n)
r
r[1]<-seed
for(i in 1:(n-1)){ 
r[i+1]<-(a*r[i]+b)%%d
print(r[i+1])
}
r
table(r)
barplot(table(r))
ts.plot(r)
#### tolide uniform ###
d=4.32*10^500
a=65539
b=0
seed=11
n=1000
r<-numeric(n)
r
r[1]<-seed
for(i in 1:(n-1)) r[i+1]<-(a*r[i]+b)%%d
r
table(r)
barplot(table(r))
u<-(r+0.5)/d
u
hist(u)
####### tamrine jalaseye bad: test uniform ########
####### nemudar faseleye etminan ############
######hamon soal balaaa faghat az u(0,1) estefade shavad#####
######jalase sheshom######
##500 adad tasadofi az tozi uniform (0,1) b ravash hamnehesht khati va sepas chek konid######
d=4.32*10^50
a=65539
b=0
seed=117
n=500
r<-numeric(n)
r[1]<-seed
for(i in 1:(n-1)) r[i+1]<-(a*r[i]+b)%%d
u<-(r+0.5)/d
u
hist(u)
par(mfrow = c(1, 4))
qqplot(qt(ppoints(100),df=2),u)
qqline(u,distribution=function(p) qt(p,df=1))

qqplot(qnorm(ppoints(100)),u)
qqline(u,distribution=function(p) qnorm(p))
 
qqplot(qchisq(ppoints(100),df=2),u)
qqline(u,distribution=function(p) qchisq(p,df=2))

qqplot(qunif(ppoints(100)),u)
qqline(u,distribution=function(p) qunif(p))
ks.test(u,"pt",df=1)
ks.test(u,"pnorm") 
ks.test(u,"pchisq",df=2)
ks.test(u,"punif")
##baresi nemone mored nazar ba tozi T student ba df 2 #########
par(mfrow = c(1, 3))
x<-rnorm(100)
qqplot(qt(ppoints(100),df=1),x)
qqline(x,distribution=function(p) qt(p,df=1))

qqplot(qnorm(ppoints(100)),x)
qqline(x,distribution=function(p) qnorm(p))
 
qqplot(qchisq(ppoints(100),df=2),x)
qqline(x,distribution=function(p) qchisq(p,df=2))

ks.test(x,"pt",df=1)
ks.test(x,"pnorm")
ks.test(x,"pchisq",df=2)
####yek nemone 500 az tozi normal tolid konid dar x gharar dahid x ra b tavan 2 beresanid dar y gharar dahid
 1_ham tozihi y ham nemodari ham adadi2- ham tozihi jazry##########
#### tamrin 1 jalase 6 ####
###########################

x <- rnorm(500)
y <- x ^ 2

# t-student x
par(mfrow = c(1,4))
qqplot(qt(ppoints(100),df = 1), x)
qqline(x, distribution= function(p) qt(p,df = 1))
ks.test(x, 'pt', df = 1)

# normality x
qqplot(qnorm(ppoints(100)),x)
qqline(x, distribution = function(p) qnorm(p))
ks.test(x, 'pnorm')


# uniform x
qqplot(qunif(ppoints(100)), x)
qqline(x, distribution = function(p) qunif(p))
ks.test(x, 'punif')

# chi-squared x
qqplot(qchisq(ppoints(100), df = 2), x)
qqline(x, distribution = function(p) qchisq(p, df = 2))
ks.test(x, 'pchisq', df = 2)

# t-student y 
par(mfrow = c(1,4))
qqplot(qt(ppoints(100),df = 1), y)
qqline(y, distribution= function(p) qt(p,df = 1))
ks.test(y, "pt", df =1)

# normality y
qqplot(qnorm(ppoints(100)), y)
qqline(y, distribution = function(p) qnorm(p))
ks.test(y, "pnorm")

# uniform y
qqplot(qunif(ppoints(100)), y)
qqline(y, distribution = function(p) qunif(p))
ks.test(y, "punif")

# chi-squared y
qqplot(qchisq(ppoints(100), df = 2), y)
qqline(y, distribution = function(p) qchisq(p, df = 2))
ks.test(y, "pchisq", df = 2)

# distribution equality of x and y 
ks.test(y, x)


# square root of y
qqplot(qchisq(ppoints(100), df = 2), sqrt(y))
qqline(sqrt(y), distribution = function(p) qchisq(p, df =2))
ks.test(sqrt(y), "pchisq", df =2 )

#####################
#####################################################
########jalase 7#######
######################################

x<-rnorm(500)
y<-x^2

hist(y, freq=FALSE, ylim=c(0,1))
lines(density(y), lty=2, col=2)

#chi-squared y
qqplot(qchisq(ppoints(100), df=2),y)
qqline (y, distribution=function(p) qchisq(p, df=2))
ks.test(y,"pchisq",df=1)

qqplot(qchisq(ppoints(100), df=1),y)
qqline (y, distribution=function(p) qchisq(p, df=1))
abline(a=0, b=1, col="tomato4", lty=2, lwd=3)
ks.test(y,"pchisq",df=1)

##fisher distributiony
qqplot(qf(ppoints(100), df1=2,df2=2),y)
qqline (y, distribution=function(p) qf(p, df1=2, df2=2))
abline(a=0, b=1, col="tomato4", lty=2, lwd=3)
ks.test(y,"pf",df1=2, df2=2)

#colors()


###################################################################
set.seed(13)
x<-rnorm(500)
x
y<-x^2
z = sqrt(y)
hist(z, freq=FALSE)
lines(density(z), col=1,lty=2, lwd=1)
curve(dchisq(x, df=3),0,4, lty=3, col=4, lwd=2, add=TRUE)
curve(sqrt(2/pi)*exp(-x^2/2), 0, 4 ,lty = 4, col= "tomato3", lwd =2 , add = TRUE)
curve(dnorm(x), 0, 4 , lty = 5, col = "green", lwd = 3, add = TRUE)
# install.packages("fdrtool", dep = TRUE)
library(fdrtool)
?dhalfnorm

ks.test(z , "phalfnorm")


#### Tamrin ####
# farz konid x unif x^2 che tozi darad?
# 2 tabdile maruf entekhab konid va marahele jalase 7 ra ruye an tozi ha ejra konid.
###### tamrin jalaseye 7 ######

set.seed(70)
x <- runif(10000)
y <- x^2 

hist(y, freq = FALSE)
lines(density(y), lty = 2 , col = 'blue' , lwd = 2)
curve(dexp(x , rate = 3),0, 1, lwd = 2, lty = 8,col = 'red 3', add = TRUE)

qqplot(qexp(ppoints(100), rate = 3), y)
qqline(y, distribution = function(p) qexp(p, rate = 3))

ks.test(y, "pexp", rate = 3)

#######################
set.seed(33)
x <- rnorm(500)
y <- exp(x)

hist(y, ylim = c(0, 0.6), freq = FALSE)
lines(density(y) , lwd = 2 , col = 2)
curve(dlnorm(x), 0 , 35 , lwd = 2 , col = 4 , add = TRUE)

qqplot(qlnorm(ppoints(100)), y)
qqline(y , distribution = function(k) qlnorm(k))

ks.test(y , 'plnorm')

########################
set.seed(7)
x <- rt(500 , df = 1)
y <- x ^ 2 
hist(y , freq = FALSE)
curve(df(x , df1 = 1, df2 =1 ) , lwd = 2 , col = 'purple' , add= TRUE)

qqplot(qf(ppoints(100), df1 =1  , df2 =1 ), y)
qqline(y , distribution = function(p) qf(p , df1 = 1 , df2 = 1))

ks.test(y , 'pf', df1 = 1 , df2 =1)
#x = seq(0, 10, by = 0.1)
#fx = function(t) {-t * exp(-t) - 2 * exp(-t ) -2 - 2 * runif(1)}
#y = fx(x)
#plot(y, x)

##############################
######## jalaseye 9 ##########
n = 10000
u1 = runif(n)
u2 = runif(n)

z1 = sqrt(-2 * log(u1)) * cos(2 * pi * u2)
z2 = sqrt(-2 * log(u1)) * sin(2 * pi * u2)

par(mfrow = c(2,2), mar = c(2,3,2,2))
hist(z1)
hist(z2)
qqnorm(z1)
qqline(z1)

qqnorm(z2)
qqline(z2)

shapiro.test(z1)
shapiro.test(z2)

ks.test(z1 , "pnorm")
ks.test(z2 , "pnorm")

####################################################
################## fasle 3 #########################
####################################################
### antegral monte carlo va ghazie haye had markazi #####

# taghribe riman baraye hale antegral
# z ~ N(0,1)
# P(a<z<b) = ? 
n = 1000 
a = 0
b = 1
w = (b - a)/ n
#w
#w/ 2
g = seq(a + w/2 , b - w/2  , length = n)
g
approx = sum(w * 1/sqrt(2 * pi) * exp(-g ^ 2 /2))
approx
real = pnorm(b) - pnorm(a)
real
#####################################
fx = function(x) {x * exp(- x^2 / 2)}


n = 100
a = 0
b = 10
w = (b - a)/ n
g = seq(a + w/2 , b- w/2 , length = n)
approx = sum(w * fx(g))
approx

curve(fx(x), 0 , 10)# dar curve tabe bar hasbe x bashad
points(g, rep(0, length= length(g)) , pch = 16, col = 2)
#####################################
# antegrale zir ra be raveshe riman hal konid 
# function: cos2pix * exp(-xsin2pix) dx
# baze: (1, 5)
## jalaseye 11
fx = function(x) {cos(2 * pi * x) * exp( -x * sin(2 * pi * x))}


n = 10000
a = 1
b = 5
w = (b - a)/ n
g = seq(a + w/2 , b- w/2 , length = n)
approx = sum(w * fx(g))
approx

curve(fx(x), -100 , 100)# dar curve tabe bar hasbe x bashad

integrate(fx, 1, 5)
# tamrin: max tabe fogh ra dar bazehaye zir bedast avarid
# bazeye 1 ta 5 , -5 ta 5 , -100 ta 100


#####################################
##### monte carlo method
# z ~ N(0, 1)
# p(0<z<1) = ?
# raveshe aval: tozi moshakhas
n = 100000
z = rnorm(n)
#z
#hist(z)
mean(z > 0 & z < 1)
# hamgaraei
n = seq(10 , 10 ^ 4 ,l = 100)
result = 0

for(i in 1:100){
z = rnorm(n[i])
result[i] = mean(z > 0 & z < 1)
}
plot(n ,result, type = "l")
abline(h = 0.3413, lty = 2 , col = 2)
## tamrin x ~ exp(lambda = 5)
# x ~ chisq(4)
# x ~ t-student(5)
# p(1<x<5)
# p(10<x<100)
####### x ~ exp & p(1<x<5)
n = 1000
data = rexp(n, rate = 5)
mean(data>1 & data<5)

n = seq(10 , 10 ^ 4 , l = 100)
res = 0
for(i in 1 : 100){
  data = rexp(n[i], rate = 5)
  res[i] = mean(data>1 & data<5)
}
plot(n , res, type = "l")
abline(h = 0.0065 , lty = 2, col = 2)
####### range (10 , 100)
n = 1000
data = rexp(n, rate = 5)
mean(data>10 & data<100)

n = seq(10 , 10 ^ 4 , l = 100)
res = 0
for(i in 1 : 100){
  data = rexp(n[i], rate = 5)
  res[i] = mean(data>10 & data<100)
}
plot(n , res, type = "l")
abline(h = 0 , lty = 2, col = 2)
##### x ~ chisq(4)
n= 1000
x = rchisq(n , df = 4)
mean(x>1 & x<5)

n = seq(10 , 10^4 , l = 100)
res = 0
for (i in 1:100){
  x = rchisq(n[i], df = 4)
  res[i] <- mean(x>1 & x<5)
}
plot(n , res , type = "l")
abline(h = 0.62 , lty = 2, col = 2)
##### range(10 , 100)
n= 1000
x = rchisq(n , df = 4)
mean(x>10 & x<100)

n = seq(10 , 10^4 , l = 100)
res = 0
for (i in 1:100){
  x = rchisq(n[i], df = 4)
  res[i] <- mean(x>10 & x<100)
}
plot(n , res , type = "l")
abline(h = 0.04 , lty = 2, col = 2)
#### t-student(5)
n = 1000
x = rt(n , df = 5)
mean(x>1 & x<5)

n =seq(10 , 10^4, l = 100)
result = 0
for (i in 1:100){
  x = rt(n[i] , df = 5)
  result[i] <- mean(x>1 & x<5)
}
plot(n , result, type = "l")
abline(h=0.18 , lty = 2, col = 2)
###### range (10 ,100)
n = 1000
x = rt(n , df = 5)
mean(x>10 & x<100)

n =seq(10 , 10^4, l = 100)
result = 0
for (i in 1:100){
  x = rt(n[i] , df = 5)
  result[i] <- mean(x>10 & x<100)
}
plot(n , result, type = "l")
abline(h=10 ^ -5 , lty = 2, col = 2)
##### jalaseye 12 #######
#### raveshe monte carlo 
#### tozie na moshakhas a , b motenahi
#### integral x * exp(-x) baze: 1 ,5
m = 10000
a = 1
b = 5
x <- runif(m, a ,b)
mean((b-a) * x * exp(-x))


real = 2 * exp(-1 ) - 6 * exp(-5)
real

### tamrin1 : x ^ 2 *exp(-x ^ 2) baze 0, 1
### tamrin2 : sin(x) * exp(-exp(-x)) baze 0 ,2
m = 100
a= 0
b = 1
x <- runif(m , a, b)
mean((b-a) * x ^ 2 * exp(-x ^ 2))

n = seq(10 , 10^4 , l = 100)
result = 0 
for(i in n){
  x <- runif(i, a, b)
  gx = (b-a) * x^2 * exp(-x^2)
  result = c(result , mean(gx))
}
result = result[-1]
plot(n , result ,type = "l")

#### tamrin 2
m = 100
a = 0
b = 2
x <- runif(m , a , b)
mean((b-a) * sin(x) * exp(-exp(-x)))

n = seq(10 , 10 ^ 4 , l = 100)
result = 0
k = 1
for(i in n){
  x <- runif(i, a , b) 
  gx = (b -a) * sin(x) * exp(-exp(-x))
  result[k] = mean(gx)
  k = k+1
}
plot(n , result , type = "l")


#### raveshe monte carlo 
#### tozie na moshakhas a ya b namotenahi
#### x * exp(-x) baze 0 , + inf
#### fx = exp(-x) 
m = 5000
x <- rexp(m,1)
gx = x * exp(-x)
fx = exp(-x)
hx = gx / fx
mean(hx)
### nemudar hamgaraei

n = seq(10 , 10^4 , l = 100)
result = 0 
for(i in n){
	x <- rexp(i,1)
	gx = x * exp(-x)
	fx = exp(-x)
	hx = gx / fx
	result = c(result , mean(hx))
}
result = result[-1]
plot(n , result ,ylim = c(0 , 2) ,type = "l")
abline(h = 1 , lty =2 , col =2)

#### tamrin: omid tozie cauchy
#### integral nasere
#### baze -inf , +inf 
m = 10000000
x <- rcauchy(m)
mean(x)

n = seq(10 , 10 ^ 7 , l = 100)
result = 0
k = 1
for(i in n){
x <- rcauchy(i)
result[k] = mean(x)
k = k + 1
}
plot(n , result, type = "l")

x <- rnorm(m, 0 , 10000)
fx = dnorm(x , 0 ,10000)
gx = x * dcauchy(x)
hx = gx / fx
mean(hx)

curve(dcauchy , -10 , 10 , ylim = c(-0.1 , .5))
curve(dnorm , -10 , 10 , add = TRUE , lty =2 , col =2)
abline(h = 0 , col =3)

######## jalaseye 13 ######
###########################
## ghazieye hade markazi ##
n = 100
iter = 500
xbar = 0
for(i in 1:iter){
x <- runif(n)
xbar[i] =  mean(x)
}
par(mfrow = c(1 ,2))
hist(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
pval
qqnorm(xbar)
qqline(xbar)
text(-1 , .6 , paste("p-value = ", pval), col = 2)
shapiro.test(xbar)
?text
####### jalaseye 14 #######
n = 5
iter = 1000
p = 0.9
m = 5
xbar = 0
for(i in 1:iter){
x <- rbinom(n,m,p)
xbar[i] <- mean(x)
}
par(mfrow = c(4,2), mar = c(2,2,1,1))
hist(xbar,main = "n = 5")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)
####

n = 10

xbar = 0
for(i in 1:iter){
x <- rbinom(n,m,p)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar,main = "n = 10")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)
###
n = 30

xbar = 0
for(i in 1:iter){
x <- rbinom(n,m,p)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar,main = "n = 30")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)
####
n = 100

xbar = 0
for(i in 1:iter){
x <- rbinom(n,m,p)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar, main = "n = 100")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)


#################################################
######## cauchy #############
n = 500
iter = 1000

xbar = 0
for(i in 1:iter){
x <- rcauchy(n)
xbar[i] <- mean(x)
}
par(mfrow = c(4,2), mar = c(2,2,1,1))
hist(xbar,main = "n = 5")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)
####

n = 10000

xbar = 0
for(i in 1:iter){
x <- rcauchy(n)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar,main = "n = 10")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)
###
n = 100000

xbar = 0
for(i in 1:iter){
x <- rcauchy(n)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar,main = "n = 30")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
usr <- par("usr")
text(locator(1), paste("p-value = ", pval), col = 4 )
shapiro.test(xbar)
####
n = 1000000

xbar = 0
for(i in 1:iter){
x <- rcauchy(n)
xbar[i] <- mean(x)
}
#par(mfrow = c(1,2))
hist(xbar, main = "n = 100")
qqnorm(xbar)
qqline(xbar)
pval = round(shapiro.test(xbar)$p.value, 3)
text(locator(1), paste("p-value = ", pval), col = 4 , cex = 3)
shapiro.test(xbar)

#############################################
######## tozie laplace ra ba estefade az raveshe cdf nemuneye tasadofi tolid konid 
######## ba nemudare histogram check konid nemunehaye tolid shode az in tozi peiravi mikonad
##### jalaseye 15 #### 
## tozie laplace ra ba estefade az raveshe cdf nemuneye tasadofi tolid konid
## ba nemudare histogram check konid nemunehaye tolid shode az in tozi peiravi mikonad
n = 100000
u <- runif(n)
x <- ifelse(u > 0 & u < 1/2 , log(2 * u) , -log(2 * (1 - u)))

hist(x,ylim = c(0 , 0.5), freq = FALSE)
lines(density(x), lty = 2 , col = 2)
curve((1/2) * exp(-abs(x)),-6 , 6, add = TRUE, col = 4 , lty = 4)

## tolide nemune az tozie normal be raveshe rado paziresh
# tozie hadaf : normal standard
# tozie pishnehadi : laplace
curve(exp(-1 / 2 * (x ^ 2 - abs(x))) , -2 , 2)
abline(v = c(-0.5,0.5), lty = 2 , col = 2)
exp(-1 / 2 * (.5 ^ 2 - abs(.5))

m = 1.5
n = 10000
u <- runif(n)
y <- ifelse(u > 0 & u < 1/2 , log(2 * u) , -log(2 * (1 - u)))
u1 <- runif(n)
#u1
z <- 1/2 * exp(-y^2 / 2)/ (m * (1/2) * exp(-abs(y)))
#z
x <- ifelse(u1 <= z, y, NA)
#x
x1 <- as.numeric(na.omit(x))
#x1
hist(x1, freq = FALSE)
lines(density(x1), lty = 3 , col = 2)
curve(dnorm(x), -5 , 5 , lty = 2 , col = 4, add = TRUE)


