#!/usr/local/bin/pythonw
# coding: iso-8859-1
# Copyright © 2009 by Amos Newcombe
# 
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# 
# This program 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''Gamma.py
'''

from Probability import Probability, Affine
from mathlib.Gamma import Gamma, IGamma, InverseIGamma
from math import exp, pow

class GammaDistBase(Probability):
	
	def __init__(self, xAlpha):
# 		print '%s(%s)' % (self.__class__.__name__, xAlpha)
		self.xAlpha = float(xAlpha)
	
	def __repr__(self): return '%s(%s)' % (self.__class__.__name__, self.xAlpha)
	
	def Distribution(self, x):
# 		print '%s.Distribution(%s)' % (self.__class__.__name__, x)
		return IGamma(self.xAlpha, x)
	
	def Density(self, x):
# 		print '%s.Density(%s)' % (self.__class__.__name__, x)
		return exp(-x) * pow(x, self.xAlpha-1.) / Gamma(self.xAlpha)
	
	def InverseDistribution(self, y): return InverseIGamma(self.xAlpha, y)
	
	def getMean(self): return self.xAlpha
	def getVariance(self): return self.xAlpha

class GammaDist(Affine):
	
	def __init__(self, xAlpha, xBeta):
# 		print '%s(%s, %s)' % (self.__class__.__name__, xAlpha, xBeta)
		self.xAlpha = float(xAlpha)
		self.xBeta  = float(xBeta)
		Affine.__init__(self, GammaDistBase(self.xAlpha), 0., self.xBeta)
# 		print self.probBase
	
	def __repr__(self): return '%s(%s, %s)' % \
		(self.__class__.__name__, self.xAlpha, self.xBeta)

class Chi2(GammaDist):
	
	def __init__(self, nDF):
# 		print '%s(%s)' % (self.__class__.__name__, nDF)
		self.nDF = int(nDF)
		GammaDist.__init__(self, self.nDF/2., 2.)
# 		print self.probBase
	
	def __repr__(self): return '%s(%s)' % (self.__class__.__name__, self.nDF)

if __name__ == '__main__':
	
	prob = GammaDist(4,4)
	print '%s.probBase = %s' % (repr(prob), prob.probBase) 
