NEST  2.6.0,not_revisioned_source_dir@0
binomial_randomdev.h
Go to the documentation of this file.
1 /*
2  * binomial_randomdev.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "config.h"
24 
25 #ifndef BINOMIAL_RANDOMDEV_H
26 #define BINOMIAL_RANDOMDEV_H
27 
28 #include <cmath>
29 #include <vector>
30 #include "randomgen.h"
31 #include "randomdev.h"
32 #include "poisson_randomdev.h"
33 #include "exp_randomdev.h"
34 #include "lockptr.h"
35 #include "dictdatum.h"
36 
37 
38 
39 /*BeginDocumentation
40 Name: rdevdict::binomial - binomial random deviate generator
41 Description:
42  Generates binomially distributed random numbers.
43 
44  p(k) = (n! / k!(n-k)!) p^k (1-p)^(n-k) , 0<=k<=n, n>0
45 
46 Parameters:
47  p - probability of success in a single trial (double)
48  n - number of trials (positive integer)
49 
50 SeeAlso: CreateRDV, RandomArray, rdevdict
51 Author: Hans Ekkehard Plesser, Moritz Deger
52 */
53 
54 
55 namespace librandom {
56 
75 /* ----------------------------------------------------------------
76  * Draw a binomial random number using the BP algoritm
77  * Sampling From the Binomial Distribution on a Computer
78  * Author(s): George S. Fishman
79  * Source: Journal of the American Statistical Association, Vol. 74, No. 366 (Jun., 1979), pp. 418-423
80  * Published by: American Statistical Association
81  * Stable URL: http://www.jstor.org/stable/2286346 .
82  * ---------------------------------------------------------------- */
83 
84 
86  {
87  public:
88  // accept only lockPTRs for initialization,
89  // otherwise creation of a lock ptr would
90  // occur as side effect---might be unhealthy
91  BinomialRandomDev(RngPtr, double p_s = 0.5, unsigned int n_s=1);
92  BinomialRandomDev(double p_s = 0.5, unsigned int n_s=1);
93 
100  void set_p_n (double, unsigned int);
101  void set_p (double);
102  void set_n (unsigned int);
103 
112  using RandomDev::operator();
113  using RandomDev::ldev;
114 
115  long ldev(RngPtr) const;
116  bool has_ldev() const { return true; }
117 
118  double operator()(RngPtr) const;
119 
121  void set_status(const DictionaryDatum&);
122 
124  void get_status(DictionaryDatum&) const;
125 
126 
127  private:
130  double p_;
131  double phi_;
132  long m_;
133  unsigned int n_;
134  std::vector<double_t> f_;
135  unsigned int n_tablemax_;
136 
137  void init_();
138  void PrecomputeTable(size_t);
139 
140  };
141 
142  inline
144  {
145  return static_cast<double>(ldev(rthrd));
146  }
147 }
148 
149 #endif
150 
void set_p_n(double, unsigned int)
set parameters for p and n p - success probability for single trial n - number of trials ...
Definition: binomial_randomdev.cpp:126
double p_
probability p of binomial distribution
Definition: binomial_randomdev.h:130
long m_
Definition: binomial_randomdev.h:132
virtual double operator()(void)
Operator delivering doubles.
Definition: randomdev.h:199
void PrecomputeTable(size_t)
compute the internal lookup table
Definition: binomial_randomdev.cpp:58
BinomialRandomDev(RngPtr, double p_s=0.5, unsigned int n_s=1)
Definition: binomial_randomdev.cpp:40
void init_()
check and initialize internal parameters
Definition: binomial_randomdev.cpp:153
void get_status(DictionaryDatum &) const
get distribution parameters from SLI dict
Definition: binomial_randomdev.cpp:215
double phi_
Definition: binomial_randomdev.h:131
void set_p(double)
set p
Definition: binomial_randomdev.cpp:137
std::vector< double_t > f_
precomputed table of f
Definition: binomial_randomdev.h:134
ExpRandomDev exp_dev_
source of exponential random numbers
Definition: binomial_randomdev.h:129
bool has_ldev() const
true if RDG implements ldev function
Definition: binomial_randomdev.h:116
Class PoissonRandomDev Create Poisson distributed random numbers.
Definition: poisson_randomdev.h:103
unsigned int n_tablemax_
current maximal n with precomputed values
Definition: binomial_randomdev.h:135
void set_n(unsigned int)
set n
Definition: binomial_randomdev.cpp:143
void set_status(const DictionaryDatum &)
set distribution parameters from SLI dict
Definition: binomial_randomdev.cpp:189
Abstract base class for access to non-uniform random deviate generators.
Definition: randomdev.h:131
virtual long ldev(void)
integer valued functions for discrete distributions
Definition: randomdev.h:206
Class BinomialRNG.
Definition: binomial_randomdev.h:85
Class ExpRandomDev Create exponential random numbers.
Definition: exp_randomdev.h:52
PoissonRandomDev poisson_dev_
source of Poisson random numbers
Definition: binomial_randomdev.h:128
unsigned int n_
parameter n in binomial distribution
Definition: binomial_randomdev.h:133