KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsl_randist.h
Go to the documentation of this file.
1 
6 /*
7  * $Id: gsl_randist.h 17393 2014-08-23 20:19:14Z predoehl $
8  */
9 
10 #ifndef GSL_RANDIST_H_INCLUDED_LIBKJB_UOFARIZONAVISION
11 #define GSL_RANDIST_H_INCLUDED_LIBKJB_UOFARIZONAVISION
12 
13 #include <l_cpp/l_exception.h>
14 #include <m_cpp/m_vector.h>
15 #include <gsl_cpp/gsl_rng.h>
16 
17 #ifdef KJB_HAVE_GSL
18 // these are GSL headers, not our wrapper
19 #include "gsl/gsl_randist.h"
20 #else
21 #warning "Compiling GNU GSL wrapper without GNU GSL; it will not run properly"
22 #endif
23 
24 
25 namespace kjb {
26 
38 
39 #ifdef KJB_HAVE_GSL
40  gsl_ran_discrete_t* m_opaque;
41 #endif
42 
43 public:
44 
50  Gsl_ran_discrete( size_t event_count, const double* event_probs )
51 #ifdef KJB_HAVE_GSL
52  : m_opaque( gsl_ran_discrete_preproc( event_count, event_probs ) )
53  {
54  ETX_2( 00 == m_opaque, "Gsl_ran_discrete ctor: bad alloc" );
55  }
56 #else
57  {
58  KJB_THROW_2( Missing_dependency, "GNU GSL" );
59  }
60 #endif
61 
67  Gsl_ran_discrete( const Vector& event_probs )
68 #ifdef KJB_HAVE_GSL
69  : m_opaque( 00 )
70  {
71  Gsl_ran_discrete grd(
72  event_probs.size(),
73  event_probs.get_c_vector() -> elements
74  );
75  swap( grd );
76  }
77 #else
78  {
79  KJB_THROW_2( Missing_dependency, "GNU GSL" );
80  }
81 #endif
82 
83 
89  Gsl_ran_discrete( const std::vector< double >& event_probs )
90 #ifdef KJB_HAVE_GSL
91  : m_opaque( 00 )
92  {
93  // Herb Sutter tells me the following code is legit:
94  // http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/
95  Gsl_ran_discrete grd( event_probs.size(), & event_probs[ 0 ] );
96  swap( grd );
97  }
98 #else
99  {
100  KJB_THROW_2( Missing_dependency, "GNU GSL" );
101  }
102 #endif
103 
115  double sample( const gsl_rng* rng )
116  {
117 #ifdef KJB_HAVE_GSL
118  return gsl_ran_discrete( rng, m_opaque );
119 #endif
120  }
121 
128  double pdf( size_t k )
129  {
130 #ifdef KJB_HAVE_GSL
131  return gsl_ran_discrete_pdf( k, m_opaque );
132 #endif
133  }
134 
135 
137  void swap( Gsl_ran_discrete& that )
138  {
139 #ifdef KJB_HAVE_GSL
140  using std::swap;
141 
142  swap( m_opaque, that.m_opaque );
143 #endif
144  }
145 
146 
147 #ifdef KJB_HAVE_GSL
150  {
151  gsl_ran_discrete_free( m_opaque );
152  }
153 #endif
154 
155 };
156 
157 
158 } // end ns kjb
159 
160 #endif /* GSL_RANDIST_H_INCLUDED_LIBKJB_UOFARIZONAVISION */
void gsl_rng
Definition: gsl_rng.h:22
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
for k
Definition: APPgetLargeConnectedEdges.m:61
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
double pdf(size_t k)
Recompute the probability mass (or density) for event k.
Definition: gsl_randist.h:128
Class defs for C++ wrapper on GNU GSL random number generators.
Gsl_ran_discrete(const std::vector< double > &event_probs)
build the sampler from a std::vector< double > of event weights
Definition: gsl_randist.h:89
Gsl_ran_discrete(size_t event_count, const double *event_probs)
build the sampler from an array of event weights or probabilities
Definition: gsl_randist.h:50
void swap(Gsl_ran_discrete &that)
swap the internal state of two of these objects
Definition: gsl_randist.h:137
double sample(const gsl_rng *rng)
sample from the discrete distribution
Definition: gsl_randist.h:115
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
#define ETX_2(a, msg)
Definition: l_exception.h:78
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
const Impl_type * get_c_vector() const
Get const pointer to the underlying kjb_c::Vector C struct.
Definition: m_vector.h:844
Gsl_ran_discrete(const Vector &event_probs)
build the sampler from a Vector event weights or probabilities
Definition: gsl_randist.h:67
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Randomly sample discrete events with an empirical distribution.
Definition: gsl_randist.h:37