versor  3.0
C++11 library for Geometric algebra
vsr_stat.h
1 //
2 // Prob.h
3 // vsr
4 //
5 // Created by Pablo Colapinto on 5/2/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8 //
9 
10 #ifndef vsr_Prob_h
11 #define vsr_Prob_h
12 
13 #include <stdlib.h>
14 #include <time.h>
15 #include <math.h>
16 //#include <tr1/random>
17 
18 namespace vsr{
19 
20  //using namespace std::tr1;
21 
22  //mersenne twister
23  //mt19937 gen( time(NULL) );
24 
25 
29  struct Rand {
30 
31 
33 
34  // inline static void Seed(int num){
35  // srand(num);
36  // gen.seed( num );
37  // }
38 
39  inline static void Seed() {
40 
41  //typical srand
42  srand( time(NULL) );
43  //gen.seed( time(NULL) );
44 
45  }
46 
47  inline static void Seed(int num){
48  srand( num );
49  }
50 
52  inline static double Num() { return 1.0 * rand() / (RAND_MAX); }
53 
55  inline static double Num(double max) { return max * rand() / (RAND_MAX); }
56 
58  inline static double Num(double low, double high){
59  return low + ( (high-low) * rand() / RAND_MAX );
60  }
61 
63  inline static bool Boolean() { return rand() & 1; }
64 
66  inline static int Int( int high, int low = 0 ) {
67  int diff = high - low;
68 
69  int result = floor( Num(diff+1) );
70 
71  return low + result;
72  }
73 
74  // inline static double Poisson(double mean) {
75  // poisson_distribution<double> poisson(mean);
76  // return poisson( gen );
77  // }
78 
80  // inline static int Uniform(int low, int high){
81  // uniform_int<int> uniform(low, high);
82  // return uniform( gen );
83  // }
84 
86  inline static double Uniform(){
87  //uniform_real<double> uniform(0,1);
88  //return uniform( gen );
89  return Num();
90  }
91 
93  // inline static double Normal(double mean, double dev) {
94  // variate_generator< mt19937, normal_distribution<double> > gaussian( mt19937(gen), normal_distribution<double>(mean, dev) ) ;
95  // //normal_distribution<double> gaussian(mean, dev) ;
96  // return gaussian();
97  // }
98 
99 
101  // inline static double Exponential(double rate){
102  // exponential_distribution<double> exponential(rate);
103  // return exponential( gen );
104  // }
105 
107  inline static double Pareto(double scale, double shape=1.0){
108  //return exp( Exponential(rate) );
109  return scale / ( pow( 1 - Num(), 1.0 / shape ) );
110  }
111 
112  };
113 
114  struct Eval {
115  inline static double Gaussian(double x, double b= 1, double c = 1, double a = 1){
116  double num = (x-b);
117  double den = 2 * c * c;
118  return a * exp( -(num*num) / den ) ;
119  }
120 
121  };
122 
123  class Stat {
124 
125  public:
126 
127  inline static bool Gaussian(double x, double b= 1, double c = 1, double a = 1){
128  return Prob( Eval::Gaussian(x,b,c,a) );
129  }
130 
131  inline static bool Power(double p, double k, double a = 1){
132  return Prob( a * pow(p,k) );
133  }
134 
136  inline static bool Prob(double p){
137  return ( Rand::Num() <= p ) ? 1 : 0;
138  }
139  };
140 }
141 
142 
143 #endif
Definition: vsr_stat.h:114
Definition: vsr_stat.h:123
static double Pareto(double scale, double shape=1.0)
Normal (Gaussian) Distribution.
Definition: vsr_stat.h:107
static double Num()
Number Between 0 and 1;.
Definition: vsr_stat.h:52
static double Uniform()
Uniform Distribution [0,1)
Definition: vsr_stat.h:86
static bool Boolean()
Random Boolean.
Definition: vsr_stat.h:63
static bool Prob(double p)
Bernoulli: Returns true with a probability of p.
Definition: vsr_stat.h:136
static void Seed()
Seed.
Definition: vsr_stat.h:39
the versor library namespace
Definition: vsr_algebra.h:29
static double Num(double max)
Number Between 0 and max.
Definition: vsr_stat.h:55
Probability Density Functions.
Definition: vsr_stat.h:29
static double Num(double low, double high)
Number Between low and high.
Definition: vsr_stat.h:58
static int Int(int high, int low=0)
Integer between high and low.
Definition: vsr_stat.h:66