Ask Your Question

Probability space valued functions

asked 2012-10-15 05:46:16 -0500

Juan Simoes gravatar image

updated 2015-01-14 03:57:39 -0500

FrédéricC gravatar image

I am trying to define a FiniteProbabilitySpace whose probabilities depend on a value x:

P[1] = lambda x: x
P[-1] = lambda x: x
P[0] = lambda x: 1.-2*x
Sp = DiscreteProbabilitySpace(S,P)

Then if I say Sp.entropy(), I get the result:

TypeError: bad operand type for unary -: 'function'

which seems logic, I did not determine x. But if I say Sp(0).entropy(), I get:

TypeError: Unable to convert x (='<function<lambda>at0xa65d2a8>') to real number.

I do not understand this. Is it possible to do what I want?

Thank you very much!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2012-10-15 07:11:32 -0500

kcrisman gravatar image

First off, the var('x') is not useful; the lambda functions will ignore that, I think.

Secondly, your first syntax was right; entropy was a property of the whole probability space. There isn't any room in the syntax for your variable, though, so this isn't possible in exactly this way.

Is this sufficient?

sage: S=[-1,0,1] 
sage: P = [(1,lambda x:x)]
sage: P.append((0,lambda x:1.-2.*x))
sage: P.append((-1,lambda x:x))
sage: Sp = lambda x: DiscreteProbabilitySpace(S,dict([(a,b(x)) for (a,b) in P ]))
sage: Sp(0)
Discrete probability space defined by {0: 1.00000000000000, 1: 0, -1: 0}
sage: Sp(0).entropy()
sage: Sp(.5).entropy()
sage: Sp(.3).entropy()

I assume that this one doesn't make sense since it's not actually a probability distribution - maybe we should look for that in this code...

sage: Sp(1).entropy()
edit flag offensive delete link more


It solved my problem, thank you very much!

Juan Simoes gravatar imageJuan Simoes ( 2012-10-15 07:29:01 -0500 )edit

Sweet! Remember, learning a little Python will go a LONG ways in using Sage.

kcrisman gravatar imagekcrisman ( 2012-10-15 07:41:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-10-15 05:46:16 -0500

Seen: 277 times

Last updated: Oct 15 '12