2014-02-10 16:01:39 +00:00
|
|
|
#include <iostream>
|
2015-10-01 00:13:34 +01:00
|
|
|
#include <LatAnalyze/Io.hpp>
|
2015-09-28 18:18:54 +01:00
|
|
|
#include <LatAnalyze/CompiledFunction.hpp>
|
|
|
|
#include <LatAnalyze/Plot.hpp>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/RandGen.hpp>
|
2014-02-10 16:01:39 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
2015-09-28 18:18:54 +01:00
|
|
|
constexpr int seqLength = 25;
|
|
|
|
constexpr int saveStep = 9;
|
|
|
|
constexpr Index nDraw = 20000;
|
|
|
|
const string stateFileName = "exRand.seed";
|
2014-02-10 16:01:39 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-02-20 23:52:45 +00:00
|
|
|
RandGenState state;
|
2015-09-28 18:18:54 +01:00
|
|
|
RandGen gen[2];
|
|
|
|
AsciiFile stateFile(stateFileName, File::Mode::write|File::Mode::read);
|
|
|
|
DVec gauss(nDraw);
|
|
|
|
Plot p;
|
|
|
|
Histogram h;
|
2014-02-10 16:01:39 +00:00
|
|
|
|
|
|
|
cout << "- GENERATOR STATE I/O TESTS" << endl;
|
|
|
|
cout << "-- generating a " << seqLength << " steps random sequence..."
|
|
|
|
<< endl;
|
|
|
|
for (int i = 0; i < seqLength; ++i)
|
|
|
|
{
|
|
|
|
if (i == saveStep)
|
|
|
|
{
|
|
|
|
state = gen[0].getState();
|
|
|
|
stateFile.save(state, "exRand");
|
|
|
|
cout << "generator state after step " << saveStep - 1
|
|
|
|
<< " saved in '" << stateFileName << "'" << endl;
|
|
|
|
}
|
|
|
|
cout << "step " << i << "\t: " << gen[0].uniform() <<endl;
|
|
|
|
}
|
|
|
|
cout << "-- setting up another generator from '" << stateFileName << "'..."
|
|
|
|
<< endl;
|
2014-02-20 23:52:45 +00:00
|
|
|
gen[1].setState(stateFile.read<RandGenState>("exRand"));
|
2014-02-10 16:01:39 +00:00
|
|
|
cout << "-- generating a " << seqLength << " steps random sequence..."
|
|
|
|
<< endl;
|
|
|
|
for (int i = 0; i < seqLength; ++i)
|
|
|
|
{
|
|
|
|
cout << "step " << i << "\t: " << gen[1].uniform() <<endl;
|
|
|
|
}
|
2015-09-28 18:18:54 +01:00
|
|
|
cout << "-- generating " << nDraw << " Gaussian random numbers..." << endl;
|
|
|
|
FOR_VEC(gauss, i)
|
|
|
|
{
|
|
|
|
gauss(i) = gen[0].gaussian();
|
|
|
|
}
|
|
|
|
h.setFromData(gauss, -5., 5., 40);
|
|
|
|
h.normalize();
|
2015-11-24 16:27:47 +00:00
|
|
|
cout << " median= " << h.median() << endl;
|
|
|
|
for (double s = 1.; s < 5.; ++s)
|
|
|
|
{
|
|
|
|
auto ci = h.confidenceInterval(s);
|
|
|
|
|
|
|
|
cout << static_cast<int>(s) << " sigma(s) interval= [";
|
|
|
|
cout << ci.first << ", " << ci.second << "]" << endl;
|
|
|
|
}
|
2015-09-28 18:18:54 +01:00
|
|
|
p << PlotHistogram(h);
|
|
|
|
p << PlotFunction(compile("return exp(-x_0^2/2)/sqrt(2*pi);", 1), -5., 5.);
|
|
|
|
p.display();
|
|
|
|
|
2014-02-10 16:01:39 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|