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

Source Code for Module BIP.Bayes.like

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