/* Evaluates distribution dst at point x
   known distributions are:
   1. Discrete distributions
   -- BernoulliDistribution(p)
   -- BinomialDistribution(p,n)
   -- DiscreteUniformDistribution(a,b)
   -- PoissonDistribution(l)
   2. Continuous distributions
   -- ExponentialDistribution(l)
   -- NormalDistrobution(a,s)
   -- ContinuousUniformDistribution(a,b)
   -- tDistribution(m)
   -- GammaDistribution(m)
   -- ChiSquareDistribution(m)
   
  DiscreteDistribution(domain,probabilities) represent arbitrary
  distribution with finite number of possible values; domain list
  contains possible values such that 
  Pr(X=domain[i])=probabilities[i].
  TODO: Should domain contain numbers only?
*/


10 # PDF(BernoulliDistribution(_p),0) <-- p;
10 # PDF(BernoulliDistribution(_p),1) <-- 1-p;
10 # PDF(BernoulliDistribution(_p),x_IsNumber)_(x != 0 And x != 1) <-- 0;
10 # PDF(BernoulliDistribution(_p),_x) <-- Hold(If(x=0,p,If(x=1,1-p,0))); 

10 # PDF(BinomialDistribution(_p,_n),_k) <-- Bin(n,k)*p^k*(1-p)^(n-k); 

10 # PDF(DiscreteUniformDistribution(_a,_b), x_IsNumber) <-- If(x<a Or x>b, 0 ,1/(b-a+1));
11 # PDF(DiscreteUniformDistribution(_a,_b), _x) <-- Hold(If(x<a Or x>b, 0 ,1/(b-a+1)));

10 # PDF( PoissonDistribution(_l), n_IsNumber) <-- If(n<0,0,Exp(-l)*l^n/n!);
11 # PDF(PoissonDistribution(_l),_n) <-- Exp(-l)*l^n/n!;

10 # PDF(GeometricDistribution(_p),_n) <--If(n<0,0,p*(1-p)^n);

10 # PDF(ExponentialDistribution(_l), _x) <-- If(x<0,0,l*Exp(-l*x));

10 # PDF(NormalDistribution(_a,_s),_x) <-- Exp(-(x-a)^2/(2*s))/Sqrt(2*Pi*s);

10 # PDF(ContinuousUniformDistribution(_a,_b),x)_(a<b) <-- If(x<a Or x>b,0,1/(b-a));

10 # PDF( DiscreteDistribution( dom_IsList, prob_IsList), _x)_( Length(dom)=Length(prob) And Simplify(Add(prob))=1) <--
    [ 
      Local(i); 
      i:=Find(dom,x);
      If(i = -1,0,prob[i]);
    ];
10 # PDF( ChiSquareDistribution( _m),x_IsRationalOrNumber)_(x<=0) <-- 0;
20 # PDF( ChiSquareDistribution( _m),_x) <-- x^(m/2-1)*Exp(-x/2)/2^(m/2)/Gamma(m/2);

10 # PDF(tDistribution(_m),x) <-- Gamma((m+1)/2)*(1+x^2/m)^(-(m+1)/2)/Gamma(m/2)/Sqrt(Pi*m);

/* Evaluates Cumulative probability function CDF(x)=Pr(X<x) */

10 # CDF(BernoulliDistribution(_p), x_IsNumber) <-- If(x<=0,0,If(x>0 And x<=1, p,1));
11 # CDF(BernoulliDistribution(_p), _x) <-- Hold(If(x<=0,0,If(x>0 And x<=1, p,1)));

10 # CDF(BinomialDistribution(_p,_n),m_IsNumber)_(m<=0) <-- 0;
10 # CDF(BinomialDistribution(_p,n_IsInteger),m_IsNumber)_(m>n) <-- 1;
10 # CDF(BinomialDistribution(_p,_n),_m) <-- Sum @ { i, 0, m-1, PDF(BinomialDistribution(p,n),i)};

10 # CDF(DiscreteUniformDistribution( a_IsNumber, b_IsNumber), x_IsNumber)_(x<=a) <-- 0;
10 # CDF(DiscreteUniformDistribution( a_IsNumber, b_IsNumber), x_IsNumber)_(x>b) <-- 1;
10 # CDF(DiscreteUniformDistribution( a_IsNumber, b_IsNumber), x_IsNumber)_(a<x And x<=b) <-- (x-a)/(b-a+1);
11 # CDF(DiscreteUniformDistribution( _a, _b), _x) <--Hold(If(x<=a,0,If(x<=b,(x-a)/(b-a),1)));

10 # CDF(PoissonDistribution(_l), x_IsNumber)_(x<=0) <-- 0;
10 # CDF(PoissonDistribution(_l), _x) <-- Sum @ {i,0,x-1,PDF(PoissonDistribution(l),i)};

10 # CDF( ChiSquareDistribution(_m), _x) <-- IncompleteGamma(x/2,m/2)/Gamma(x/2);
10 # CDF( DiscreteDistribution( dom_IsList, prob_IsList), _x)   <--
      [
	 Local(i,cdf,y);
       
	 i := 1; 
	 cdf:=0;
	 y:=dom[i];
	 While(y<x) [cdf:=cdf+prob[i];i++;];
	 cdf;
      ];
      
10 # Expectation(BernoulliDistribution(_p)) <-- 1-p;

10 # Expectation(BinomialDistribution(_p,_n)) <-- n*p;

10 # Expectation( DiscreteDistribution( dom_IsList, prob_IsList))_( Length(dom)=Length(prob) And Simplify(Sum(prob))=1) <-- Sum @ {i,1,Length(dom),dom[i]*prob[i]};
 
