baffling error-handling problem

C

Chris Fonnesbeck

I thought I knew how to do error handling in python, but apparently I
dont. I have a bunch of code to calculate statistical likelihoods, and
use error handling to catch invalid parameters. For example, for the
bernoulli distribution, I have:

def bernoulli_like(self, x, p, name='bernoulli'):
"""Bernoulli log-likelihood"""

# Ensure proper dimensionality of parameters
dim = shape(x)
p = resize(p,dim)

# Ensure valid values of parameters
if sum(p>=1 or p<=0): raise LikelihoodError

... etc.

where LikelihoodError is simply a subclass of ValueError that I created:

class LikelihoodError(ValueError):
"Log-likelihood is invalid or negative infinite"


I catch these errors with the following:

try:
like = self.calculate_likelihood()
except LikelihoodError:
return 0

So far, so good. When I calculate_likelihood is called in the above,
which contains a call to bernoulli_like:

p = invlogit(beta0 + sum([b*h for b,h in zip(self.beta,hab)]))

like=self.bernoulli_like(x,p)

I get the following when an invalid parameter is passed:

Traceback (most recent call last):
File "C:\Conroy\working\resource_selection_ms\analyses\IIbq\sampled\new_chris\model_000.py",
line 381, in ?
model.sample(iterations=iter, burn=burn,plot=False)
File "C:\Python23\Lib\site-packages\PyMC\MCMC.py", line 1691, in sample
self._like = self.calculate_likelihood()
File "C:\Conroy\working\resource_selection_ms\analyses\IIbq\sampled\new_chris\model_000.py",
line 194, in calculate_likelihood
like+=self.bernoulli_like(x,p)
File "C:\Python23\Lib\site-packages\PyMC\MCMC.py", line 868, in bernoulli_like
if sum(p>=1 or p<=0): raise LikelihoodError
LikelihoodError


I have no idea how this can happen, given how I have coded this.
Anyone see what I must be missing?

Thanks,
C.
 
M

Michael Hudson

Chris Fonnesbeck said:
I thought I knew how to do error handling in python, but apparently I
dont. I have a bunch of code to calculate statistical likelihoods, and
use error handling to catch invalid parameters. For example, for the
[...]

bernoulli distribution, I have:
I have no idea how this can happen, given how I have coded this.
Anyone see what I must be missing?

Is it possible you have two classes called LikelihoodError? One in
__main__, one in some_module_of_yours, maybe.

Cheers,
mwh
 
S

Scott David Daniels

Chris said:
I thought I knew how to do error handling in python, but apparently I
dont. I have a bunch of code to calculate statistical likelihoods, and
use error handling to catch invalid parameters. For example, for the
bernoulli distribution, I have:

def bernoulli_like(self, x, p, name='bernoulli'):
... if sum(p>=1 or p<=0): raise LikelihoodError ...

where LikelihoodError is simply a subclass of ValueError that I created:

class LikelihoodError(ValueError):
"Log-likelihood is invalid or negative infinite"

I catch these errors with the following:

try: like = self.calculate_likelihood()
except LikelihoodError:
return 0

> ... [when using] ...
like=self.bernoulli_like(x,p)

I get the following when an invalid parameter is passed:

Traceback (most recent call last):
File "...\model_000.py", line 381, in ?
model.sample(iterations=iter, burn=burn,plot=False)
File "...\PyMC\MCMC.py", line 1691, in sample
self._like = self.calculate_likelihood()
File "...\model_000.py", line 194, in calculate_likelihood
like+=self.bernoulli_like(x,p)
File "...\MCMC.py", line 868, in bernoulli_like
if sum(p>=1 or p<=0): raise LikelihoodError
LikelihoodError

I have no idea how this can happen, given how I have coded this.

Might you be referring to a different LikelihoodError in the
try: ... except ... part of your code than in the ... raise ... part?
Similarly defined classes are not the same class. If you didn't
get LikelihoodError in mode4l_000.py with the moral equivalent of
from MCMC import LikelihoodError
then this is what is going wrong.
By the way, if it were I, I'd: raise LikelihoodError(p) just so I
could discover a bit of what went wrong.

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top