Skip to content

Commit

Permalink
pdfs: introduce UniPdf (uniform distribution), includes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strohel committed Oct 31, 2010
1 parent 2487baf commit 80e47f2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pybayes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

"""TODO: documentation"""

from pdfs import Pdf, GaussPdf
from pdfs import Pdf, UniPdf, GaussPdf
from kalman import Kalman
2 changes: 1 addition & 1 deletion pybayes/numpywrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

from numpy import any as np_any, array, asarray, diag, dot, dot as dotvv, ndarray
from numpy.linalg import cholesky, inv, slogdet
from numpy.random import normal
from numpy.random import normal, uniform
2 changes: 1 addition & 1 deletion pybayes/numpywrap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from numpy cimport import_array, int, npy_intp, NPY_DOUBLE, PyArray_EMPTY, PyArray_ISCARRAY_RO, PyArray_ISFARRAY_RO
from numpy import any as np_any, array, asarray, diag, empty
from numpy.linalg import cholesky, slogdet
from numpy.random import normal
from numpy.random import normal, uniform

cimport tokyo as t

Expand Down
5 changes: 5 additions & 0 deletions pybayes/pdfs.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ cdef class Pdf:
cpdef ndarray sample(self)


cdef class UniPdf(Pdf):

cdef public double a, b


cdef class GaussPdf(Pdf):

cdef public ndarray mu
Expand Down
41 changes: 40 additions & 1 deletion pybayes/pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Probability density functions"""

from math import log

from numpywrap import *


Expand Down Expand Up @@ -31,10 +33,47 @@ def sample(self):
raise NotImplementedError("Derived classes must implement this function")


class UniPdf(Pdf):
"""Simple uniform single-dimensional probability density function
.. math: f(x|a, b) = \THETA {x-a} \THETA {b-x} {1} \over {b-a} TODO
"""

def __init__(self, a, b):
"""Initialise uniform distribution with left point a and right point b
a must be greater that b
"""
if b <= a:
raise ValueError("b must be grater than a")
self.a = float(a)
self.b = float(b)

def shape(self):
return (1,)

def mean(self):
return array([(self.a+self.b)/2.])

def variance(self):
return array([((self.b-self.a)**2)/12.])

def eval_log(self, x):
if x is None: # cython-specific, but wont hurt in python
raise ValueError("x must be numpy.ndarray")
x0 = x[0]
if x0 <= self.a or x0 >= self.b:
return float('-inf')
return -log(self.b-self.a)

def sample(self):
return uniform(self.a, self.b, self.shape())


class GaussPdf(Pdf):
"""Unconditional Gaussian (normal) probability density function
.. math: f(x|\mu,b) \propto \exp(-(x-\mu)'R^{-1}(x-\mu))
.. math: f(x|\mu, R) \propto \exp(-(x-\mu)'R^{-1}(x-\mu))
"""

def __init__(self, mean=array([0]), covariance=array([[1]])):
Expand Down
32 changes: 31 additions & 1 deletion pybayes/tests/test_pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"""Tests for pdfs"""

from math import exp
from math import exp, log
import unittest as ut

import numpy as np
Expand Down Expand Up @@ -32,6 +32,36 @@ def test_abstract_methods(self):
self.assertRaises(NotImplementedError, self.pdf.sample)


class TestUniPdf(ut.TestCase):
"""Test uniform pdf"""

def setUp(self):
self.uni = pb.UniPdf(-10., 20.)

def test_init(self):
self.assertEqual(type(self.uni), pb.UniPdf)

def test_invalid_init(self):
self.assertRaises(ValueError, pb.UniPdf, 1.0, 0.5)

def test_shape(self):
self.assertEqual(self.uni.shape(), (1,))

def test_mean(self):
self.assertTrue(np.all(self.uni.mean() == np.array([5.])))

def test_variance(self):
self.assertTrue(np.all(self.uni.variance() == np.array(75.)))

def test_eval_log(self):
self.assertEqual(self.uni.eval_log(np.array([-10.1])), float('-inf'))
self.assertEqual(self.uni.eval_log(np.array([12.547])), log(1./30.))
self.assertEqual(self.uni.eval_log(np.array([-10.1])), float('-inf'))

def test_sample(self):
pass


class TestGaussPdf(ut.TestCase):
"""Test Gaussian pdf"""

Expand Down

0 comments on commit 80e47f2

Please sign in to comment.