See

<project-file type=“source”/> <content> /* * Probability distributions * * Copyright 2006 Junling Ma * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */

#ifndef SIMDISTRIBUTION_H #define SIMDISTRIBUTION_H

#include <stdlib.h> #include <math.h> #include <vector>

/ SimDistribution models a probability distribution. */ class SimDistribution { public: / draw generates a random number from this distribution */

  virtual float draw() const = 0;
  virtual float mean() const;

};

/ define the exponentially distributed random number generator */ float expRandom(float rate); / SimExpDistribution defines an exponential distribution */ class SimExpDistribution : public SimDistribution { public:

  /** the default constructor */
  SimExpDistribution(float rate);
  /** the default destructor */
  virtual ~SimExpDistribution();
  virtual float draw() const;
  /** return the rate */
  float rate() const { return Rate; }
  virtual float mean() const { return 1 / Rate; }

protected:

  /** 1/rate is the mean */
  float Rate;

};

/ normal distribution */ class SimNormalDistribution : public SimDistribution { public: / the default constructor */

  SimNormalDistribution(float mean=0, float std=1);
  /** the default destructor */
  virtual ~SimNormalDistribution();
  virtual float draw() const;
  /** return the mean */
  virtual float mean() const { return Mean; }
  /** return the standard deviation */
  float std() const { return Std; }

protected:

  /** the mean */
  float Mean;
  /** the standard deviation */
  float Std;

};

/ generate a random number from a gamma distribution */ float gammaRandom(float gamma, float lambda); / gamma distribution with shape gamma, lambda */ class SimGammaDistribution: public SimDistribution { public:

  /** the default constructor mean = gamma * lambda, var = gamma * lamdba^2*/
  SimGammaDistribution(float gamma, float lambda);
  /** the default destructor */
  virtual ~SimGammaDistribution();
  virtual float draw() const;
  /** return the shape parameter */
  float gamma() const { return Gamma; }
  /** return the rate */
  float lambda() const { return Lambda; }
  virtual float mean() const { return Gamma / Lambda; }

protected:

  /** the shape parameter */
  float Gamma;
  /** the rate */
  float Lambda;

};

/ uniform distribution between U1 and U2 */ class SimUniformDistribution: public SimDistribution { public: / default constructor */

  SimUniformDistribution(float u1=0, float u2=1);
  /** the default destructor */
  virtual ~SimUniformDistribution();
  virtual float draw() const;
  /** return U1 */
  float u1() const { return Left; }
  /** reutrn U2 */
  float u2() const { return ( Left + Length ); }
  virtual float mean() const { return Left + Length / 2; }

protected:

  /** the left limit of the interval */
  float Left;
  /** the length of the interval */
  float Length;

};

/ truncate a given distribution outside the interval (left, right)*/ class SimCutOff: public SimDistribution { public: / default constructor */

  SimCutOff(SimDistribution* dist, float left, float right);
  /** the default destructor */
  virtual ~SimCutOff();
  virtual float draw() const;
  /** return the original distribution */
  SimDistribution *original() { return Distribution; }
  /** return the left limit of truncation */
  float left() const { return Left; }
  /** return the right limit of truncation */
  float right() const { return Right; }

protected:

  /** the distribution to be truncated */
  SimDistribution *Distribution;
  /** the left limit of truncation */
  float Left;
  /** the right limit of truncation */
  float Right;

};

/ models a general discrete distribution */ class SimDiscreteDistribution: public SimDistribution { public: / default constructor */

  SimDiscreteDistribution ();
  /** the default destructor */
  virtual ~SimDiscreteDistribution();
  virtual float draw() const;
  /** set a frequency at a point */
  void set(float value, float frequency);
  /** return the frequency at point n */
  float frequency(int n) const { return Points[n].frequency(); }
  /** return the n-th point */
  float point(int n) const { return Points[n].value(); }
  /** return the number of point */
  float points() const { return Points.size(); }
  virtual float mean() const;

protected:

  class Point {
  public:
      Point(float value, float frequency);
      float value() const { return Value; }
      float frequency() const { return Frequency; }
      float &frequency() { return Frequency; }
      float sum() const { return Sum; }
      float &sum() { return Sum; }
      bool operator > ( const Point &p ) const { return ( Value > p.value() ); }
      bool operator < ( const Point &p ) const { return ( Value < p.value() ); }
      bool operator >= ( const Point &p ) const { return ( Value >= p.value() ); }
      bool operator <= ( const Point &p ) const { return ( Value <= p.value() ); }
      bool operator == ( const Point &p ) const { return ( Value == p.value() ); }
      bool operator > ( float v ) const { return ( Value > v ); }
      bool operator < ( float v ) const { return ( Value < v ); }
      bool operator >= ( float v ) const { return ( Value >= v ); }
      bool operator <= ( float v ) const { return ( Value <= v ); }
      bool operator == ( float v ) const { return ( Value == v ); }
 protected:
      float Value;
      float Frequency;
      float Sum;
  };
  float sum(int n) const { return Points[n].sum(); }
  float &sum(int n) { return Points[n].sum(); }
  /** the the points */
  std::vector<Point> Points;

};

#endif

</content>