473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

baffling error-handling problem

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.
Jul 28 '05 #1
2 1362
Chris Fonnesbeck <fo********@gmail.com> writes:
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

--
<bruce> how are the jails in israel?
<itamar> well, the one I was in was pretty nice
-- from Twisted.Quotes
Jul 28 '05 #2
Chris Fonnesbeck wrote:
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
Sc***********@Acm.Org
Jul 28 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TimS | last post by:
I am getting a baffling File not found error (0x800A0035). I am writing asp on a windows 2000 server. I build a path and filename from several fields in a jet database using SQL commands, like...
8
by: Michelle | last post by:
I have tried every variation of the "onchange" statement below without any actual reloading of the page: The FireFox javaScript Console reports the following message: Error: missing : in...
3
by: Andrew Luke | last post by:
Hi all you C++ guru's! I'm 'very, very' new to C++ and I'm having a little trouble configuring my VS environment I think - when I try and compile some sample code I'm getting the following...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
12
by: Mike | last post by:
I have posted this before, but no one responds, so here it goes again. I think my question is intimidating people and I have no idea why?!? This is not a new concept. You can do this with other...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
3
by: Brian Victor | last post by:
I'm trying to get FFTW (fftw.org) to work in such a way that it can be called from a managed C++ module. The approach I've found to generate the fewest linker errors is a mixed managed/unmanaged...
2
by: noddy | last post by:
In both IE7 and FF2(using firebug) I get the same error. I have a page that displays work contacts' details within a <DIV class = "contactBase" >. The div has a header area <div...
3
by: bluedogsd | last post by:
I have a new server RHE4 and I am having an issue I have never had before and it's kind of urgent. Running Apache 2.0 I need the web server to have writable access and getting denied - I'm pretty...
1
by: Tim Redfern | last post by:
Hi I'm having an issue that is probably obvious to somebody here but its baffling me.. I'm using python (2.5.1) on an embedded linux sytem system (kernel 2.6.19 I think) with only 32MB memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.