Example usage code
This page contains examples of how to use the library in your code.
Multivariate Normal
Using Eigen containers
#include <iostream>
#include "NormalDist.h"
int main(int argc, char* argv[])
{
std::cout.precision(4);
Eigen::Vector2d MeanVector;
MeanVector(0)=1.0;
MeanVector(1)=-1.0;
Eigen::Matrix2d VarMatrix;
VarMatrix(0,0)=3;
VarMatrix(0,1)=1;
VarMatrix(1,0)=1;
VarMatrix(1,1)=3;
BivarNorm.SetRandomSeed(99);
Eigen::Vector2d TempCoords;
TempCoords(0)=1.0;
TempCoords(1)=0.5;
double Density=BivarNorm.GetDensity(TempCoords);
double CumDensity=BivarNorm.GetCumulativeDesity(TempCoords);
std::cout << "Density in [1 ; 0.5] = " << std::fixed << Density
<< std::endl << "Cumulative Density in [1 ; 0.5] = " << std::fixed << CumDensity;
Eigen::Vector2d Quant83=BivarNorm.GetQuantile(0.83);
std::cout << std::endl << "83rd Percentile = [" << Quant83.transpose()<< "] " << "that has cdf: " << BivarNorm.GetCumulativeDesity(Quant83);
Eigen::Matrix<double,10,2> Samples=BivarNorm.ExtractSamples(10);
std::cout << std::endl << "Simulation Results:" << std::endl << " Var 1\t Var2"<< std::endl << Samples << std::endl;
return 0;
}
The output produced will be:
Density in [1 ; 0.5] = 0.0369
Cumulative Density in [1 ; 0.5] = 0.4390
Simulation Results:
Var 1 Var2
2.7742 -0.7284
0.6430 -3.0028
4.3652 1.1009
1.2452 -1.2843
1.2112 -0.4647
1.2562 1.1469
6.0257 2.0565
4.2924 -0.5663
1.2785 -0.5571
2.9207 -2.2104
Using STL containers
#include <iostream>
#include <vector>
#include "NormalDist.h"
int main(int argc, char* argv[])
{
std::cout.precision(4);
BivarNorm.SetRandomSeed(88);
std::vector<double> MeanVector(2);
MeanVector[0]=1.0;
MeanVector[1]=-1.0;
BivarNorm.SetMeanVector(MeanVector);
std::vector<double> VarMatrix(4);
VarMatrix[0]=3.0;
VarMatrix[1]=1.0;
VarMatrix[2]=1.0;
VarMatrix[3]=3.0;
BivarNorm.SetVarCovMatrix(VarMatrix);
std::vector<double> TempCoords;
TempCoords.push_back(1.0);
TempCoords.push_back(0.5);
double Density=BivarNorm.GetDensity(TempCoords);
double CumDensity=BivarNorm.GetCumulativeDesity(TempCoords);
std::cout << "Density in [1 ; 0.5] = " << std::fixed << Density
<< std::endl << "Cumulative Density in [1 ; 0.5] = " << std::fixed << CumDensity ;
std::map<unsigned int,std::vector<double> > Samples=BivarNorm.ExtractSamplesMap(10);
std::cout << std::endl << "Simulation Results:"<< std::endl << "Var 1\t| Var2"<< std::endl << "_________________";
for(int j=0;j<10;j++){
std::cout << std::endl;
for(unsigned int i=0;i<BivarNorm.GetDimension();i++){
std::cout << std::fixed << Samples.at(i).at(j);
if(i<BivarNorm.GetDimension()-1) std::cout << "\t| ";
}
}
std::cout << std::endl;
return 0;
}
The output produced will be:
Density in [1 ; 0.5] = 0.0369
Cumulative Density in [1 ; 0.5] = 0.4431
Simulation Results:
Var 1 | Var2
_________________
4.3795 | -0.0478
3.7745 | -3.0145
-0.7895 | -2.9816
-0.1412 | -0.4594
1.9040 | -2.3109
3.2947 | 1.9221
0.4840 | -3.7591
0.5675 | -1.0132
0.4181 | -1.6335
1.8001 | -4.1868