You are on page 1of 3

Assignment of Computational Techniques

FAIQA

2022-11-19
Question:1

dtarget=function(x,mu,sigma){
dtar=(2.7183^(((x-mu)^2)/sigma^2))/(sigma*sqrt(2*3.14*x))
return(dtar)}
#### write function for proposal density
dproposal=function(y,theta){
dprop=(2.7183^-(y/theta))/theta
return(dprop)}
###### write function for random number from proposal density
rproposal=function(n,theta){
U1=runif(n)
y=-1/theta*(log(1-U1))
return(y)}
#### try ratio
y=rproposal(n=50000,theta=1)
head(y)

## [1] 1.12456300 1.24380371 0.28343932 0.67065958 0.03507273 0.63202531

## [1] 0.4796034 0.5143731 2.5413987 0.6870177 0.6406747 3.0991687


x=y[runif(50000)<(dtarget(y,mu=0,sigma=1))/(1.32*dproposal(y,theta=1))]
# through indexing #
length(x)

## [1] 47895

## [1] 47859
hist(x, main = "Distribution of random numbers generated for target
density")
Question:2

anova_test<-function(o,p,q,alpha){
k=3
o1=length(o)
p1=length(p)
q1=length(q)
n=sum(o1,p1,q1)
o2=sum(o)
p2=sum(p)
q2=sum(q)
o_bar<-o2/o1
p_bar<-p2/p1
q_bar<-q2/q1
o_p_q_bar<-(o2+p2+q2)/(o1+p1+q1)
c1<-(o_bar-o_p_q_bar)^2
c2<-(p_bar-o_p_q_bar)^2
c3<-(q_bar-o_p_q_bar)^2
sst<-sum(c1,c2,c3)
mst=sst/(k-1)

d1<-(o-o_bar)^2
d2<-(p-p_bar)^2
d3<-(q-q_bar)^2
mse<-(sum(d1,d2,d3)-sst)/(n-k-2)

f=mst/mse
df1=k-1
df2=n-k-2
F_cal<-qf(alpha,df1,df2)
if (f>F_cal){
pvalue<-1-pf(f,df1,df2)
}
if (pvalue<alpha && pvalue<F_cal){
decide<-"reject null hypothesis"
}
else {
decide<-"accept the null hypothesis"
}
result<-list(f=f,pvalue=pvalue,F_cal=F_cal,decide=decide)
return(result)
}
sample1<-c(12,19,56,87,45,33,45)
sample2<-c(4,34,65,28,95,33,86)
sample3<-c(12,45,75,22,89,14,9)
anova_test(sample1,sample2,sample3,0.01)

## $f
## [1] 0.03090684
##
## $pvalue
## [1] 0.9696236
##
## $F_cal
## [1] 0.01005665
##
## $decide
## [1] "accept the null hypothesis"

You might also like