#!/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/>.

'''Uniform.py implements the uniform distribution.
'''

from random import random
from Probability import Probability

class Uniform(Probability):
	'''The uniform probability law on the interval [0, 1].
	
	Use Affine(Uniform(), a, b) for the uniform law on the interval [a, a+b], where 0 < b.
	'''

	def Distribution(self, xX):
		if xX < 0.: return 0.
		if 1. < xX: return 1.
		return xX
	
	def Density(self, xX):
		if (xX  < 0.) or (1.  < xX): return 0.
		if (xX == 0.) or (1. == xX): return 0.5
		return 1.
	
	def InverseDistribution(self, xY):
		if xY < 0. or 1. < xY:
			raise ValueError, 'y = %s is outside of [0,1]' % xY
		return xY
	
	def getMean(self): return 0.5
	def getVariance(self): return 1./12.
	
	def Sample(self): return random()

if __name__ == '__main__':
	from DistributionTests import Tests
	prob = Uniform()
	xMin = -0.2
	xMax = +1.2
	xStep = 0.1
	Tests(prob, xMin, xMax, xStep, 1)
