Professional Documents
Culture Documents
Deap Assi No-2
Deap Assi No-2
#Q1
> library(randtests)
> z=c(0.324,0.485,0.724,0.624,0.135,0.821,0.514,0.635,0.292,0.443,0.356,0.629);z
[1] 0.324 0.485 0.724 0.624 0.135 0.821 0.514 0.635 0.292 0.443 0.356 0.629
> y=runs.test(z);y
Runs Test
data: z
0.5448
> Tcal=y$statistic;Tcal
statistic
-0.6055301
> pv=y$p.value;pv
[1] 0.5448269
> los=0.05
> cat("H0:Generated numbers are random against H1:Generated numbers are not random\n");
H0:Generated numbers are random against H1:Generated numbers are not random
> cat("pvalue:",pv,"\n");
pvalue: 0.5448269
> los1=100*los;los1
[1] 5
> ##Q2.
>
z=c(0.05,0.41,0.68,0.89,0.94,0.74,0.91,0.55,0.62,0.36,0.27,0.19,0.72,0.75,0.08,0.54,0.02,0.01,0.36,0.
16,0.28,0.18,0.01,0.95,0.69,0.18,0.47,0.23,0.32,0.82,0.53,0.31,0.42,0.73,0.04,0.83,0.45,0.13,0.57,0.6
3,0.29);z
[1] 0.05 0.41 0.68 0.89 0.94 0.74 0.91 0.55 0.62 0.36 0.27 0.19 0.72 0.75 0.08
[16] 0.54 0.02 0.01 0.36 0.16 0.28 0.18 0.01 0.95 0.69 0.18 0.47 0.23 0.32 0.82
[31] 0.53 0.31 0.42 0.73 0.04 0.83 0.45 0.13 0.57 0.63 0.29
> y=runs.test(z);y
Runs Test
data: z
0.5217
> Tcal=y$statistic;Tcal
statistic
-0.6407233
> pv=y$p.value;pv
[1] 0.5217025
> los=0.05
> cat("H0:Generated numbers are random against H1:Generated numbers are not random\n");
H0:Generated numbers are random against H1:Generated numbers are not random
> cat("pvalue:",pv,"\n");
pvalue: 0.5217025
> los1=100*los;los1
[1] 5
> if(pv<los){cat("since,pv<los Reject H0 at",los1,"% level of significance \n");}else{cat("since pv>los
Accept H0 at",los1,"% level of significance\n");}
**********************************************************************************
> #Q3.
> ofreq=c(680,289,31);ofreq
> N=sum(ofreq);N
[1] 1000
> k=length(ofreq);k
[1] 3
> p=c(0.72,0.27,0.01);p
> efreq=rep(N*p);efreq
> los=0.05;
> cat("To Test:H0:Random numbers are independent against H1:Random numbers are not
independent \n");
To Test:H0:Random numbers are independent against H1:Random numbers are not independent
> Tcal=sum(((ofreq-efreq)^2)/efreq);Tcal
[1] 47.65926
> pv=pchisq(Tcal,df=(k-1),lower.tail=F);
> cat("pvalue:",pv,"\n");
pvalue: 4.476343e-11
> ##Q4.
> library(randtests)
> lcg<-function(a,c,m,run.length,seed)
+{
+ x<-rep(0,run.length)
+ x[1]<-seed
+ for(i in 1:(run.length-1))
+{
+ x[i+1]<-c(a*x[i]+c)%%m
+}
+ return(list(x=x,u=u))
+}
>
> los=0.05
>
> z1=lcg(13,3,26,30,1);z1
$x
[1] 1 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3
[26] 16 3 16 3 16
$u
> d=runs.test(z1$u)
> Tcal=d$statistic;Tcal
statistic
5.202563
> pv=d$p.value;pv
[1] 1.965582e-07
> los=0.05
> cat("H0:Generated numbers are random at H1:Generated numbers are not random\n");
> cat("pvalue:",pv,"\n");
pvalue: 1.965582e-07
> los1=100*los;
>
>
> z2=lcg(13,3,26,30,2);z2
$x
[1] 2 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16
[26] 3 16 3 16 3
$u
> d=runs.test(z2$u)
> Tcal=d$statistic;Tcal
statistic
-2.54951
> pv=d$p.value;pv
[1] 0.01078745
> los=0.05
> cat("H0:Generated numbers are random against H1:Generated numbers are not random\n");
H0:Generated numbers are random against H1:Generated numbers are not random
> cat("pvalue:",pv,"\n");
pvalue: 0.01078745
> los1=100*los;
>
> z3=lcg(13,3,26,30,3);z3
$x
[1] 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3
[26] 16 3 16 3 16
$u
> d=runs.test(z3$u)
> Tcal=d$statistic;Tcal
statistic
5.202563
> pv=d$p.value;pv
[1] 1.965582e-07
> los=0.05
> cat("H0:Generated numbers are random against H1:Generated numbers are not random\n");
H0:Generated numbers are random against H1:Generated numbers are not random
> cat("pvalue:",pv,"\n");
pvalue: 1.965582e-07
> los1=100*los;
>
> z4=lcg(13,3,26,30,4);z4
$x
[1] 4 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16 3 16
[26] 3 16 3 16 3
$u
> d=runs.test(z4$u)
> Tcal=d$statistic;Tcal
statistic
5.202563
> pv=d$p.value;pv
[1] 1.965582e-07
> los=0.05
> cat("H0:Generated numbers are random against H1:Generated numbers are not random\n");
H0:Generated numbers are random against H1:Generated numbers are not random
> cat("pvalue:",pv,"\n");
pvalue: 1.965582e-07
> los1=100*los;
> *******************************************************************************
>
> ##Q5.
> ofreq=c(560,394,32,13,1);ofreq
> N=sum(ofreq);N
[1] 1000
> k=length(ofreq);k
[1] 5
> p=c(0.504,0.432,0.027,0.036,0.001);p
> efreq=rep(N*p);efreq
> los=0.05;
To Test:H0:Random numbers are independent against H1:Random numbers are not independent
> Tcal=sum(((ofreq-efreq)^2)/efreq);Tcal
[1] 25.18519
> pv=pchisq(Tcal,df=(k-1),lower.tail=F);pv
[1] 4.61752e-05
> cat("pvalue:",pv,"\n");
pvalue: 4.61752e-05
> los1=100*los;los1
[1] 5
***********************************************************************
> ##Q6.
>
> lcg<-function(a,c,m,run.length,seed)
+{
+ x<-rep(0,run.length)
+ x[1]<-seed
+ for(i in 1:(run.length-1))
+{
+ x[i+1]<-(a*x[i]+c)%%m
+}
+ U<-x/m
+ return (list(x=x,U=U))
+}
>
$x
[1] 1 126 3463 2796 1341 3786 2211 1944 1337 3286 1151 516 3061 1698
[15] 3355 1584 1393 2094 3703 28 3501 3450 1171 3016 169 646 2927 1332
[29] 2661 850 3851 2144 1761 3038 2919 332 541 2090 3203 3064 2073 1078
[43] 3679 1124 1237 3074 3323 1680 1105 2958 1111 3708 653 3802 115 2088
[57] 2953 486 3407 3988 2885 178 1771 192 3521 1854 2375 1964 3837 394
[71] 99 88 2809 2966 2111 1732 3509 354 3291 1776 817 3822 2615 3292
[85] 1901 58 3155 1160 1641 326 3887 2548 3109 3602 3787 2336 1185 670
[99] 1831 3596 3037 2794 1091 1208 3545 758 543 2340 1685 1730 3259 1872
[113] 529 590 23 2876 3149 410 2099 232 329 166 271 1108 3333 2930
[127] 1707 384 2945 3582 1287 1132 2237 1098 2083 2328 185 2646 3071 2948
[141] 3957 3106 3227 1968 241 1454 1527 2460 301 762 1043 3400 3113 6
[155] 751 3764 3557 2258 3723 2528 609 2398 743 2764 1437 3498 3075 3448
[169] 921 438 1503 3556 2133 386 3195 2064 4049 2318 3031 2044 1549 1114
[183] 4083 2472 1801 3942 1231 2324 3781 1586 1643 576 2369 1214 199 300
[197] 637 1802 4067 472 1657 2326 4031 68 309 1762 3163 2160 3761 3182
[211] 439 1628 2797 1466 3027 1544 489 3782 1711 884 4005 914 3659 2720
[225] 33 30 3751 1932 3933 106 963 1592 2393 118 2463 676 2581 3138
[239] 3131 2256 3473 4046 1943 1212 4045 1818 1971 616 3273 3622 2191 3540
[253] 133 242 1579 768 1793 2942 3207 3564 3133 2506 1955 2712 3129 2006
[267] 895 1284 757 418 3099 2352 3185 814 3447 796 1197 2170 915 3784
[281] 1961 3462 2671 2100 357 3666 3595 2912 3553 1758 2663 1100 2333 810
[295] 2947 3832 3865 3894 3423 1892 3029 1794 3067 2448 2897 1678 855 380
[309] 2445 2522 3955 2856 649 3302 3151 660 581 2994 1515 960 1217 574
[323] 2119 2732 1533 3210 3939 856 505 1686 1855 2500 1205 3170 3035 2544
[337] 2609 2542 2359 4060 3693 2874 2899 1928 3433 3142 3631 3316 805 2322
[351] 3531 3104 2977 3486 1575 268 733 1514 835 1976 1241 3574 287 3108
[365] 3477 450 3003 2640 2321 3406 3863 3644 845 3226 1843 1000 2121 2982
[379] 15 1876 1029 1650 1451 1152 641 2302 1031 1900 4029 3914 1827 3096
[393] 1977 1366 2815 3716 1653 1826 2971 2736 2033 174 1271 3228 2093 3578
[407] 787 72 809 2822 495 436 1253 978 3467 3296 2401 1118 487 3532
[421] 3229 2218 2819 120 2713 3254 1247 228 3925 3202 2939 2832 1745 1038
[435] 2775 2812 3341 3930 3827 3240 3593 2662 975 3092 1477 306 1387 1344
[449] 65 4030 4039 1068 2429 522 3811 1240 3449 1046 3775 836 2101 482
[463] 2907 2928 1457 1902 183 2396 493 186 2771 2312 2281 2502 1455 1652
[477] 1701 3730 3403 3488 1825 2846 3495 2700 1629 2922 707 2360 89 2934
[491] 2207 1444 277 1858 2875 3024 1169 2766 1687 1980 1741 538 1715 1384
[505] 969 2342 1935 212 1925 3058 1323 1536 3585 1662 2951 236 829 1226
[519] 1699 3480 825 726 639 2052 2549 3234 2843 3120 881 3630 3191 1564
[533] 2989 890 659 456 3753 2182 2415 2868 2149 2386 3339 3680 1249 478
[547] 2407 1868 29 3626 2691 504 1561 2614 3167 2660 725 514 2811 3216
[561] 593 398 599 1148 141 1242 3699 3624 2441 2022 2895 1428 2373 1714
[575] 1259 1728 3009 3390 1863 3500 3325 1930 3683 1624 2297 406 1599 3268
[589] 2997 1890 2779 3312 305 1262 2103 732 1389 1594 2643 2696 1129 1862
[603] 3375 4084 2597 1042 3275 3872 673 2206 1319 1036 2525 234 579 2744
[617] 3033 2294 31 3876 1173 3266 2747 3408 17 2126 3607 316 2637 1946
[631] 1587 1768 3913 1702 3855 2644 2821 370 1195 1920 2433 1022 775 2668
[645] 1725 2634 1571 3864 3769 86 2559 388 3445 546 2715 3504 3825 2990
[659] 1015 3996 3885 2298 531 840 2601 1542 239 1204 3045 3794 3211 4064
[673] 97 3934 231 204 925 938 2563 888 409 1974 991 996 1621 1922
[687] 2683 3600 3537 3854 2519 3580 1037 2650 3571 4008 1289 1382 719 3860
[701] 3269 3122 1131 2112 1857 2750 3783 1836 125 3338 3555 2008 1145 3862
[715] 3519 1604 3893 3298 2651 3696 3249 622 4023 3164 2285 3002 2515 3080
[729] 4073 1222 1199 2420 3493 2450 3147 160 3617 1566 3239 3468 3421 1642
[743] 451 3128 1881 1654 1951 2212 2069 578 2619 3792 2961 1486 1431 2748
[757] 3533 3354 1459 2152 2761 1062 1679 980 3717 1778 1067 2304 1281 382
[771] 2695 1004 2621 4042 1443 152 2617 3542 383 2820 245 1954 2587 3888
[785] 2673 2350 2935 2332 685 3706 403 1224 1449 902 2159 3636 3941 1106
[799] 3083 352 3041 3294 2151 2636 1821 2346 2435 1272 3353 1334 2911 3428
[813] 2517 3330 2555 3984 2385 3214 343 1916 1933 4058 3443 296 137 742
[827] 2639 2196 69 434 1003 2496 705 2110 1607 172 1021 650 3427 2392
[841] 4089 3222 1343 4036 693 610 2523 4080 2097 4078 1847 1500 3181 314
[855] 2387 3464 2921 582 3119 756 293 3858 3019 544 2465 926 1063 1804
[869] 221 3050 323 3512 729 1014 3871 548 2965 1986 2491 80 1809 846
[883] 3351 1084 333 666 1331 2536 1609 422 3599 3412 517 3186 939 2688
[897] 129 3838 519 3436 3517 1354 1315 536 1465 2902 2303 1156 1141 3362
[911] 2459 176 1521 1710 759 668 1581 1018 275 1608 297 262 4079 1972
[925] 741 2514 2955 736 1889 2654 4071 972 2717 3754 2307 1656 2201 694
[939] 735 1764 3413 642 2427 272 1233 2574 2263 252 2829 1370 3315 680
[953] 3081 102 463 532 965 1842 875 2880 3649 1470 3527 2604 1917 2058
[967] 3299 2776 2937 2582 3263 2372 1589 2018 2395 368 945 3438 3767 3932
[981] 4077 1722 2259 3848 1769 4038 943 3188 1189 1170 2891 928 1313 286
$U
> n1=0;n2=0;n3=0;n4=0;n5=0;n6=0;n7=0;n8=0;n9=0;n10=0
> n=1000;n
[1] 1000
> x=z$U;x
+{
+ if(x[i]<0.1){n1=n1+1}
+ if((x[i]>=0.1)&&(x[i]<0.2)){n2=n2+1}
+ if((x[i]>=0.2)&&(x[i]<0.3)){n3=n3+1}
+ if((x[i]>=0.3)&&(x[i]<0.4)){n4=n4+1}
+ if((x[i]>=0.4)&&(x[i]<0.5)){n5=n5+1}
+ if((x[i]>=0.5)&&(x[i]<0.6)){n6=n6+1}
+ if((x[i]>=0.6)&&(x[i]<0.7)){n7=n7+1}
+ if((x[i]>=0.7)&&(x[i]<0.8)){n8=n8+1}
+ if((x[i]>=0.8)&&(x[i]<0.9)){n9=n9+1}
+ if((x[i]>=0.9)&&(x[i]<1.0)){n10=n10+1}
+}
>
> ofreq=c(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10);ofreq
> N=sum(ofreq);N
[1] 1000
> k=length(ofreq);k
[1] 10
> p=1/k;p
[1] 0.1
> efreq=rep(N*p);efreq
[1] 100
> los=0.05;
> cat("To test:H0:Generated random numbers follow U(0,1) Against H1:Generated random numbers
do not follow U(0,1)\n");
To test:H0:Generated random numbers follow U(0,1) Against H1:Generated random numbers do not
follow U(0,1)
> Tcal=sum(((ofreq-efreq)^2)/efreq);Tcal
[1] 10.26
> pv=pchisq(Tcal,df=(k-1),lower.tail=F);
> cat("pvalue:",pv,"\n");
pvalue: 0.3298498
> los1=100*los;
***********************************************************************
#Q7
> lcg<-function(a,c,m,run.length,seed)
+{
+ x<-rep(0,run.length)
+ x[1]<-seed
+ for(i in 1:(run.length-1))
+{
+ x[i+1]<-(a*x[i]+c)%%m
+}
+ U<-x/m
+ return(list(x=x,U=U))
+}
>
> z=lcg(1103515245,12345,2^32,30,7);z
$x
$U
> los=0.05;los
[1] 0.05
> rt=ks.test(z$U,"punif",0,1);rt
data: z$U
> Tcal=rt$statistic;Tcal
0.2162086
> pv=rt$p.value;pv
[1] 0.1037274
> cat("H0:sample has been drawn from uniform distribution over (0,1) Against H1:sample has not
been drawn from uniform distribution over (0,1)","\n");
H0:sample has been drawn from uniform distribution over (0,1) Against H1:sample has not been
drawn from uniform distribution over (0,1)
p value= 0.1037274
***************************************************************************