Package BIP :: Package Bayes :: Module like
[hide private]
[frames] | no frames]

Source Code for Module BIP.Bayes.like

  1  # -*- coding:utf-8 -*- 
  2  #----------------------------------------------------------------------------- 
  3  # Name:        like.py 
  4  # Project:  BayesianInference 
  5  # Purpose:     log-likelihood functions 
  6  # 
  7  # Author:      Flávio Codeço Coelho <fccoelho@gmail.com> 
  8  # 
  9  # Created:     2008-11-26 
 10  # Copyright:   (c) 2008 by the Author 
 11  # Licence:     GPL 
 12  #----------------------------------------------------------------------------- 
 13  __docformat__ = "restructuredtext en" 
 14  import scipy 
 15  from scipy.special import gammaln 
 16  from numpy import * 
 17   
 18   
19 -def Categor(x,hist):
20 """ 21 Categorical Log-likelihood 22 generalization of a Bernoulli process for variables with any constant 23 number of discrete values. 24 25 :Parameters: 26 - `x`: data vector (list) 27 - `hist`: tuple (prob,classes) classes contain the superior limit of the histogram classes 28 29 >>> Categor([1],([.3,.7],[0,1])) 30 -0.356674943939 31 """ 32 like =0.0 33 x = array(x) 34 prob = array(hist[0]) 35 sup = array(hist[1]) 36 ind = searchsorted(sup,x) 37 like += sum(log(prob[ind])) 38 return like
39
40 -def Normal(x,mu,tau):
41 """ 42 Normal Log-like 43 44 :Parameters: 45 - `mu`: mean 46 - `tau`: precision (1/sd) 47 48 >>> Normal([0],0,1) 49 -0.918938533205 50 """ 51 x = array(x) 52 n = x.size 53 like = sum(-0.5 * tau * (x-mu)**2) 54 like += n*0.5*log(0.5*tau/pi) 55 return like
56
57 -def Lognormal(x,mu,tau):
58 """ 59 Lognormal Log-likelihood 60 61 :Parameters: 62 - `mu`: mean 63 - `tau`: precision (1/sd) 64 65 >>> Lognormal([0.5,1,1.2],0,0.5) 66 -3.15728720569 67 """ 68 x = array(x) 69 n = x.size 70 like = n * 0.5 * (log(tau) - log(2.0*pi)) + sum(0.5*tau*(log(x)-mu)**2 - log(x)) 71 return -like
72
73 -def Poisson(x,mu):
74 """ 75 Poisson Log-Likelihood function 76 >>> Poisson([2],2) 77 -1.30685281944 78 """ 79 x=array(x) 80 sumx = sum(x*log(mu)-mu) 81 sumfact = sum(log(scipy.factorial(x))) 82 like = sumx-sumfact 83 return like
84
85 -def Negbin(x,r,p):
86 """ 87 Negative Binomial Log-Likelihood 88 >>> Negbin([2,3],6,0.3) 89 -9.16117424315 90 """ 91 x = array(x) 92 like = sum(r*log(p)+x*log(1-p)+log(scipy.factorial(x+r-1))-log(scipy.factorial(x))-log(scipy.factorial(r-1))) 93 return like
94
95 -def Binomial(x,n,p):
96 """ 97 Binomial Log-Likelihood 98 >>> Binomial([2,3],6,0.3) 99 -2.81280615454 100 """ 101 x = array(x) 102 like = sum(x*log(p)+ (n-x)*log(1.-p)+log(scipy.factorial(n))-log(scipy.factorial(x))-log(scipy.factorial(n-x))) 103 return like
104
105 -def Weibull(x,alpha,beta):
106 """ 107 Log-Like Weibull 108 >>> Weibull([2,1,0.3,.5,1.7],1.5,3) 109 -7.811955373 110 """ 111 x=array(x) 112 beta = float(beta) 113 n = x.size 114 #Normalizing constant 115 like = n * (log(alpha) - alpha*log(beta)) 116 # Kernel of the distribution 117 like += sum((alpha-1) * log(x) - (x/beta)**alpha) 118 return like
119
120 -def Bernoulli(x,p):
121 """ 122 Log-Like Bernoulli 123 >>> Bernoulli([0,1,1,1,0,0,1,1],0.5) 124 -5.54517744448 125 """ 126 x=array(x) 127 like = sum(x*log(p) + (1-x)*log(1.-p)) 128 return like
129
130 -def Gamma(x,alpha,beta):
131 """ 132 Log-Like Gamma 133 >>> Gamma([2,3,7,6,4],2,2) 134 -11.015748357 135 """ 136 x = array(x) 137 beta = float(beta) 138 n = x.size 139 #Normalizing constant 140 like = -n * (gammaln(alpha) + alpha*log(beta)) 141 # Kernel of the distribution 142 like += sum((alpha - 1.0)*log(x) - x/beta) 143 return like
144
145 -def Beta(x,a,b):
146 """ 147 Log-Like Beta 148 >>> Beta([.2,.3,.7,.6,.4],2,5) 149 -0.434845728904 150 """ 151 x = array(x) 152 n = x.size 153 #Normalizing constant 154 like = n * (gammaln(a+b) - gammaln(a) - gammaln(b)) 155 # Kernel of the distribution 156 like += sum((a-1.0)*log(x) + (b-1.0)*log(1.0-x)) 157 return like
158
159 -def Simple(x,w,a,start=0):
160 """ 161 find out what it is. 162 """ 163 m=len(a) 164 n=len(x) 165 like = 0.0 166 s = sum(a*(x/w)**(2*range(n))) 167 like += log(1+s) 168 return like
169 170 if __name__=="__main__": 171 import doctest 172 doctest.testmod(verbose=True) 173