You are on page 1of 2

> ber<-function(n,theta)

+
+{
+
+ u=runif(n)
+ resul=rep(0,n)
+ for(i in 1:n)
+{
+ if(u[i]>=1-theta)
+{
+ resul[i]=1
+}
+ else
+{
+ resul[i]=0
+}
+}
+ print(resul)
+}
>
> ber(25,0.03)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> ber(25,0.03)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> a=ber(25,0.3)
[1] 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0
> ks.test(a,"pbinom",1,0.3)

One-sample Kolmogorov-Smirnov test

data: a
D = 0.7, p-value = 4.579e-11
alternative hypothesis: two-sided

Warning message:
In ks.test(a, "pbinom", 1, 0.3) : cannot compute correct p-values with ties
> chisq.test(table(a),p=c(0.7,0.3))

Chi-squared test for given probabilities

data: table(a)
X-squared = 0.4286, df = 1, p-value = 0.5127
---------------------------------------------------------------------------------------------------------

> discrete=function(n,Rx,prob)
+{
+ unifor=runif(n)
+ resul=rep(0,n)
+ for(i in 1:n)
+{
+ if(unifor[i]<=prob[1])
+{
+ resul[i]=Rx[1]
+}
+ if(unifor[i]>prob[1]&unifor[i]<=(prob[1]+prob[2]))
+
+{
+ resul[i]=Rx[2]
+}
+ if(unifor[i]>(prob[1]+prob[2])&unifor[i]<=1)
+{
+ resul[i]=Rx[3]
+}
+}
+ print(resul)
+}
> discrete(30,Rx,prob)
Error: object 'prob' not found
> discrete(30,Rx,prob)
Error: object 'prob' not found
> discrete(30,Rx,prob)
Error: object 'prob' not found
> Rx=c(2,3,4)
> prob=c(0.3,0.5,0.2)
> discrete(30,Rx,prob)
[1] 3 3 3 2 3 2 2 2 4 2 2 3 3 2 3 4 3 3 2 3 2 3 4 3 4 2 4 2 3 4
> a=discrete(30,Rx,prob)
[1] 3 4 4 3 3 3 4 3 2 3 2 4 2 4 3 3 2 2 3 4 2 2 3 3 3 3 3 2 3 3
> chisq.test(table(a),p=prob)

Chi-squared test for given probabilities

data: table(a)
X-squared = 0.1778, df = 2, p-value = 0.915

>

You might also like