Changeset 2889


Ignore:
Timestamp:
Dec 7, 2012, 9:06:07 AM (9 years ago)
Author:
Peter
Message:

Binomial distribution. fixes #730

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/rnd.cc

    r2881 r2889  
    6161  Gaussian g;
    6262
     63  Binomial bin(0.5, 10);
     64
    6365  return suite.return_value();
    6466}
  • trunk/yat/random/random.cc

    r2881 r2889  
    205205
    206206
     207  Binomial::Binomial(double p, unsigned int n)
     208    : p_(p), n_(n)
     209  {
     210  }
     211
     212
     213  unsigned long Binomial::operator()(void) const
     214  {
     215    return gsl_ran_binomial(rng_->rng(), p_, n_);
     216  }
     217
     218
    207219  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
    208220    : gen_(NULL)
  • trunk/yat/random/random.h

    r2838 r2889  
    239239    void clone(const gsl_rng&);
    240240  };
    241    
     241
    242242
    243243  // --------------------- Discrete distribtuions ---------------------
     
    290290    ///
    291291    virtual unsigned long operator()(void) const = 0;
    292    
     292
    293293  protected:
    294294    /// GSL random gererator
     
    296296  };
    297297
     298  /**
     299     \brief Binomial distribution
     300
     301     \see gsl_ran_binomial
     302
     303     \since New in yat 0.10
     304   */
     305  class Binomial : Discrete
     306  {
     307  public:
     308    /**
     309       \brief Constructor
     310
     311       Create an object that generates random numbers from binomial
     312       distribution, the number of successes in \n trials each with
     313       success probability \a p.
     314     */
     315    Binomial(double p, unsigned int n);
     316
     317    /**
     318       \return number from binomial distrubtion as parametrized in constructor.
     319     */
     320    unsigned long operator()(void) const;
     321  private:
     322    double p_;
     323    unsigned int n_;
     324  };
     325
    298326  ///
    299327  /// @brief General
    300328  ///
    301   class DiscreteGeneral : public Discrete 
     329  class DiscreteGeneral : public Discrete
    302330  {
    303331  public:
     
    308336    ///
    309337    explicit DiscreteGeneral(const statistics::Histogram& hist);
    310    
     338
    311339    /**
    312340       \brief Copy constructor
     
    348376  /**
    349377     @brief Discrete uniform distribution
    350  
     378
    351379     Discrete uniform distribution also known as the "equally likely
    352380     outcomes" distribution. Each outcome, in this case an integer
    353381     from [0,n-1] , have equal probability to occur.
    354      
     382
    355383     Distribution function \f$ p(k) = \frac{1}{n+1} \f$ for \f$ 0 \le
    356      k < n \f$ \n 
    357      Expectation value: \f$ \frac{n-1}{2} \f$ \n 
     384     k < n \f$ \n
     385     Expectation value: \f$ \frac{n-1}{2} \f$ \n
    358386     Variance: \f$ \frac{1}{12}(n-1)(n+1) \f$
    359387  */
    360   class DiscreteUniform 
    361     : public Discrete, 
     388  class DiscreteUniform
     389    : public Discrete,
    362390      public std::unary_function<unsigned long, unsigned long>
    363391  {
     
    407435  /**
    408436     @brief Poisson Distribution
    409  
     437
    410438     Having a Poisson process (i.e. no memory), number of occurences
    411439     within a given time window is Poisson distributed. This
     
    414442     succesful is small (in such a way that the expected number of
    415443     succesful attempts is \f$ m \f$.
    416      
     444
    417445     Probability function \f$ p(k) = e^{-m}\frac{m^k}{k!} \f$ for \f$ 0 \le
    418      k  \f$ \n 
    419      Expectation value: \f$ m \f$ \n 
     446     k  \f$ \n
     447     Expectation value: \f$ m \f$ \n
    420448     Variance: \f$ m \f$
    421449  */
     
    550578     \brief Generator of random numbers from an exponential
    551579     distribution.
    552      
     580
    553581     The distribution function is \f$ f(x) = \frac{1}{m}\exp(-x/a)
    554582     \f$ for \f$ x \f$ with the expectation value \f$ m \f$ and
     
    584612  /**
    585613     @brief Gaussian distribution
    586      
     614
    587615     Class for generating a random number from a Gaussian distribution
    588616     between zero and unity. Utilizes the Box-Muller algorithm, which
    589617     needs two calls to random generator.
    590      
     618
    591619     Distribution function \f$ f(x) =
    592620     \frac{1}{\sqrt{2\pi\sigma^2}}\exp(-\frac{(x-\mu)^2}{2\sigma^2})
Note: See TracChangeset for help on using the changeset viewer.