Simulation With R
when I studied lectures in Ph.D. I did some trials on simulation.
So I wanted to show popular simulation techincs in this post.
On this page, you’ll find some examples of the types of simulation.
Linear Congruential Generators
sample1:
rng.rand<- function(n=10){
rng<-vector(length=n)
for(i in 1:n) {
d<-(9*i) %%32
rng[i]<- d
}
return(rng)
}
sample2:
ali<-function(n=10)
{
ing<-vector(length=n)
for(i in 1:n){
d<-2/(1+ 5**1/2)
k<- (d+(10+k)*sin(10)) %% 1
ing[i]<- d
}
return(ing)
}
ali<-function(n=10)
{
ing<-vector(length=n)
for(i in 1:n){
d<-sum(runif(10))/10
ing[i]<-d
}
return(ing)
}
Goodness of fit test Through Uniform Distribution Number Generator
N=300
Y <- sample(runif(N), N, replace = T)
S <- sum(Y)/N
R <- S/(1:N)
plot(R)
plot(R,
ylim = c(0.3, 0.7),
type = "l",
col = "steelblue",
lwd = 2,
xlab = "n",
ylab = "R_n")
lines(c(0, N),
c(0.2,0.2),
col = "darkred",
lty = 2,
lwd = 1)
Gof test for uniform distribution
ali<-function(n=50){
mali<-vector(length=n)
for(i in 1:n){
d<-(123*i+971)%%1000
mali[i]<-d/1000
}
return(mali)
}
breaks = seq(0, 1, by=0.1)
duration.cut = cut(ali(), breaks, right=FALSE)
duration.freq = table(duration.cut)
as<-data.frame(duration.freq)
attach(as)
#n/k =5
r<-5*(sum((Freq-5)**2)/10)
KS test for uniform distribution
ali<-function(n=50){
mali<-vector(length=n)
for(i in 1:n){
d<-(123*i+971)%%1000
mali[i]<-d/1000
}
return(mali)
}
breaks = seq(0, 1, by=0.1)
duration.cut = cut(ali(), breaks, right=FALSE)
duration.freq = table(duration.cut)
as<-data.frame(duration.freq)
attach(as)
#n/k =5
r<-5*(sum((Freq-5)**2)/10)
veri2<-cbind(Freq,50)
veri<-as.matrix(veri2)
colnames(veri)<-c("Freq","n")
as<-as.data.frame(veri)
top1<-sum(Freq2)
top2<-sum(n1)
F0x<-cumsum(Freq2/top1)
Ftn<-cumsum(n1/top2)
veri5<-cbind(Freq,50,F0x,Ftn)