Last active
August 29, 2015 14:02
-
-
Save dbasilioesp/e13c233d347cd5415004 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
class Random { | |
private: | |
int seed; | |
int lastRandom; | |
int module; | |
int constantMultiple; | |
int constantAditive; | |
int number; | |
public: | |
Random::Random(int _seed){ | |
seed = _seed; | |
lastRandom = seed; | |
module = 64; | |
constantMultiple = 21; | |
constantAditive = 1; | |
} | |
int randomCalc(){ | |
lastRandom = (constantMultiple * lastRandom + constantAditive) % module; | |
return lastRandom; | |
} | |
double randomLCG(){ | |
int calc = randomCalc(); | |
return ((double) calc/(module-1)); | |
} | |
int uniformRandom(int init, int final){ | |
number = init + (randomLCG() * (final - init)); | |
return number; | |
} | |
int exponencial(int media){ | |
number = (-media * log(1 - randomLCG())); | |
return number; | |
} | |
int normal(int media, int dp){ | |
double u1, u2, v1, v2, w, y, x1; | |
u1 = randomLCG(); | |
u2 = randomLCG(); | |
v1 = 2 * u1 - 1; | |
v2 = 2 * u2 - 1; | |
w = (v1*v1) + (v2*v2); | |
y = sqrt((-2*log(w))/w); | |
x1 = v1 * y; | |
return media + x1 * dp; | |
} | |
}; | |
int main(){ | |
int limitFirstClass = 0; | |
int limitMedia = 0; | |
int limitInf = 0; | |
int limitSup = 0; | |
double media = 0; | |
int classSize = 8; | |
int classNumber = 8; | |
int * frequency = new int[classSize]; | |
int totalFrequency = 100; | |
int totalFrequencyMedia = 0; | |
int classIndex = 0; | |
double df = 0; // Graus de Liberdade | |
double e = 0; | |
double o = 0; | |
int libertyDegrees = 0; | |
int uniformEstimator = 0; | |
int exponencialEstimator = 1; | |
int normalEstimator = 2; | |
// Generate frequency array | |
Random yrandom(3); | |
for (int i = 0; i < classSize; i++){ | |
frequency[i] = 0; | |
} | |
for (int i = 0; i < totalFrequency; i++) | |
{ | |
//int number = yrandom.uniformRandom(0, 99); | |
// int number = yrandom.exponencial(5); | |
int number = yrandom.normal(50, 50); | |
classIndex = number % classSize; | |
frequency[classIndex] += 1; | |
} | |
for (int i = 0; i < classSize; i++){ | |
cout << i << ": " << frequency[i] << endl; | |
} | |
cout << endl; | |
// Calculate media | |
for (int i = 0; i < classSize; i++){ | |
limitInf = i * classSize; | |
limitSup = (i + 1) * classSize; | |
limitMedia = (limitInf + limitSup) / 2; | |
totalFrequencyMedia += limitMedia * frequency[i]; | |
} | |
media = totalFrequencyMedia / totalFrequency; | |
// Calculate uniform df | |
e = totalFrequency / classNumber; | |
libertyDegrees = classNumber - exponencialEstimator - 1; | |
for (int i = 0; i < classSize; i++){ | |
o = frequency[i] - e; | |
df += (o * o) / e; | |
} | |
// Result | |
cout << "Graus de Liberdade: " << libertyDegrees << endl; | |
cout << "Porcentagem Esperada 0.975 97.5%" << endl; | |
cout << "Qui-quadrada Esperada: 1.69" << endl; | |
cout << "Qui-quadrada Obtida: " << df << endl; | |
cout << endl; | |
system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment