473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 '):
"""Bernoull i 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_likel ihood 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,h ab)]))

like=self.berno ulli_like(x,p)

I get the following when an invalid parameter is passed:

Traceback (most recent call last):
File "C:\Conroy\work ing\resource_se lection_ms\anal yses\IIbq\sampl ed\new_chris\mo del_000.py",
line 381, in ?
model.sample(it erations=iter, burn=burn,plot= False)
File "C:\Python23\Li b\site-packages\PyMC\M CMC.py", line 1691, in sample
self._like = self.calculate_ likelihood()
File "C:\Conroy\work ing\resource_se lection_ms\anal yses\IIbq\sampl ed\new_chris\mo del_000.py",
line 194, in calculate_likel ihood
like+=self.bern oulli_like(x,p)
File "C:\Python23\Li b\site-packages\PyMC\M CMC.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 1383
Chris Fonnesbeck <fo********@gma il.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.berno ulli_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(it erations=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_likel ihood
like+=self.bern oulli_like(x,p)
File "...\MCMC.p y", 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***********@A cm.Org
Jul 28 '05 #3

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

Similar topics

5
4596
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 this: Sql = "SELECT dirs.rootname,dirs.dirname FROM dirs" RS.open Sql RS.movefirst do while not RS.EOF temp1=trim(RS("rootname"))
8
1980
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 conditional expression Source Code: location.href=themes.php?scheme=(form.color_schemes.options.value) My Code for the dropdown box is: <form name='color_schemes' method='post' action='themes.php' >
3
4587
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 errors, any help would be 'greatly' appreciated! :) Thanks heaps! --------------------Configuration: CppRichTextItem - Win32 Debug-------------------- Compiling...
13
6621
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
1519
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 programming languages, so why is C# so different? Again, here is my baffling C# question. I have C# code that opens an application, but right after it opens I am confronted by prompts for information (i.e. "Please enter your id"). I would like to...
1
7506
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 a bunch of "missing ;" errors. I did reinstall the whole thing a few times but it didn't work. Anyone have any idea? Thanks c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\xutility(862) :
3
2236
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 project. However, that still produces the following from the linker: FFTW2 error LNK2020: unresolved token (0A0000C6) fftw_execute Running ildasm on the resulting dll shows: ..method public static unmanagedexp void
2
2536
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 id="contactHeader" class="client" > in which is displayed the contact's name. Within this header area div I also want to display in smaller font
3
3115
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 sure my users/groups/permissions/etc are all good. Apache is running as user apache group ftpusers. server root for default server is at the top /export/home chowned to apache and grped to ftpusers recursively. Even so with permissions...
1
1293
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 (for an art project). My python code runs nicely when launched manually from a login shell, ps shows its using 11MB or so of memory. However, when I try to launch the same code from a startup script, it
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.