Consider the MCG with parameters: a = 23, M = 10^8 + 1, and let the seed be 47594118. (This is the original MCG proposed by Lehmer in 1948.) Apply the Kolmogorov-Smirnov test to the first 1000 random numbers (including the seed) from this generator. Compute the KS-statistic and find its p-value. What is your conclusion for the generator?
I was able to compute the MCG successfully but I am not sure how to compute the KS-statistic. From the notes I have we use the cumulative distribution function for distribution U(0,1), then computing the KS-statistic D_n (I will post the math here in a picture):
I tried using the max()
function but for some reason I get this error:
cannot convert 'double' to 'double*' for argument '1' to 'double* F(double*)
and this error:
Invalid arguments '
Candidates are:
const #0 & max(const #0 &, const #0 &)
#0 max(std::initializer_list<#0>, #1)
const #0 & max(const #0 &, const #0 &, #1)
#0 max(std::initializer_list<#0>)
Here is my code:
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
double *MCG(int n);
double *F(double u[]);
void KS(int n, double u[]);
int main(){
int n = 4;
double *u = new double[n];
u = MCG(n);
KS(n,u);
return 0;
}
double *MCG(int n){
double *x = new double[n];
double a = 23.0;
x[0] = 47594118; // Set seed to 47594118
for(int i = 1; i < n; i++){
x[i] = fmod(a*x[i-1], pow(10.0,8.0) + 1);
//cout << setprecision(10) << x[i] << endl;
}
double *u = new double[n];
for(int i = 0; i < n; i++){
u[i] = x[i]/(pow(10.0,8.0) + 1);
//cout << u[i] << endl;
}
return u;
}
double *F(double u[]){
return u;
}
void KS(int n, double u[]){
sort(u,u + n);
double D[n];
for(int i = 0; i < n; i++){
for(int k = 1; k <= n; k++){
D[i] = fmax((k/n)-F(u[k]),F(u[k]) - (k-1)/n));
}
}
}
Any suggestions on how I should fix the latter above would be greatly appreciated. I have also tried the fmax()
function but that does seem to work either.
Aucun commentaire:
Enregistrer un commentaire