Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
NonparaRuns
//g++ 7.4.0 //Nonparametric Test: The Runs Test for Randomness //code is created by Rezaul Hoque on February 4,2021 //please contact at jewelmrh@yahoo.com #include <iostream> #include <cmath> #include <bits/stdc++.h> using namespace std; int main() { char a[]="htththhhthhtthththhthtththhtht"; cout<<a; cout <<endl; int n; n= strlen(a); int countH=0; for (int i=0;i<n;i++) { if(a[i]=='h') ++countH; } int countT=0; for (int i=0;i<n;i++) { if(a[i]=='t') ++countT; } cout<<"There are "<<countH<<" heads and "<<countT<<" tails"; cout<<endl; //get rid of longer pattern like 'hhh' or 'ttt' ; convert them into other characters not common in the sequence; it will help later to count individual 'h' or 't' for(int i=0;i<n;i++){ if(a[i]=='h'&& a[i+1]=='h' && a[i+2]=='h') a[i]=a[i+1]=a[i+2]='s';} cout<<a; cout<<endl; //get rid of 'hh' for(int i=0;i<n;i++){ if(a[i]=='h' && a[i+1]=='h') a[i]=a[i+1]= '#'; } cout<<a; cout <<endl; //get rid of 'tt' for(int i=0;i<n;i++){ if(a[i]=='t' && a[i+1]=='t') a[i]=a[i+1]= '&'; } cout <<"After all the conversions the sequence looks like:\n"; cout<<a; cout <<endl; int count =0; //calculate the Run for(int i=0;i<n;i++){ if(a[i]=='h'||a[i]=='t'||(a[i]=='s'&&a[i+1]=='s' && a[i+2]=='s' )||( a[i]=='#'&&a[i+1]=='#') || (a[i]=='&' &&a[i+1]=='&')) ++count;} cout<<"There are "<<count<<" runs.\n"; int n1= countH, n2= countT; float meanRun, varRun, stdRun,zed; meanRun=((2*n1*n2)/(n1+n2))+1; varRun= (2*n1*n2*(2*n1*n2-n1-n2))/((n1+n2)*(n1+n2)*(n1+n2-1)); stdRun=sqrt(varRun); zed=(count-meanRun)/stdRun; cout <<"mean: "<<meanRun<<endl; cout <<"variance: "<<varRun<<endl; cout <<"z: "<<zed<<endl; if(zed>-1.96 && zed<1.96) cout <<" Accept the null hypothesis of randomness at 0.05 level of significance.\n"; else cout<<"Reject the null hypothesis of randomness at 0.05 level of significance.\n"; return 0; }
run
|
edit
|
history
|
help
0
Test 1(2021)
code
Test
UB mutex lock
Tray
dijkstra's algo
Empty C++ Script
Nieparzyste w macierzy
Simple use of function templete and namespace
queue