On Sun, Nov 30, 2008 at 9:39 PM, Paul Butler <[EMAIL PROTECTED]> wrote: > I've been experimenting with probability and found that in Sage, a > probability space is also a random variable by inheritance. This may be > useful. Without it, creating a random variable requires two classes: a > probability space and a random variable on that probability space. > > Unfortunately, the random variable doesn't work like I expected. For > example: > > sage: ps = DiscreteProbabilitySpace([1,2,3],{1:1/3,2:1/3,3:1/3}) > sage: ps.expectation() > 0.333333333333333 > > (I expected 2.00000000000000) > > I've prepared a patch that gives me the value I'd expect, but I'd like to > make sure this is the proper behavior. > > -- Paul >
Response from David Kohel: Dear William, Paul, Indeed, the function definition should be: def expectation(self): r""" The expectation of the discrete random variable, namely $\sum_{x \in S} p(x) X[x]$, where $X$ = self and $S$ is the probability space of $X$. """ E = 0 Omega = self.probability_space() for x in self._function.keys(): E += Omega(x) * x return E rather than: def expectation(self): r""" The expectation of the discrete random variable, namely $\sum_{x \in S} p(x) X[x]$, where $X$ = self and $S$ is the probability space of $X$. """ E = 0 Omega = self.probability_space() for x in self._function.keys(): E += Omega(x) * self(x) return E Cheers, David --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---