[Another response in this thread from David Kohel (who maybe should be posting on list)]
Hi William and Paul, Actually, I correct myself -- the average should be over the values of the function, weighted by the probabilities. The domain of the function (the keys) can be in any set (e.g. "A","B","C"), so the current behavior is correct. In the defintition of the probability space itself: sage: ps = DiscreteProbabilitySpace([1,2,3],{1:1/3,2:1/3,3:1/3}) Consider replacing the domain of ps with such a set. Then it should be clear that you can't average over "A", "B", and "C". S = ["A","B","C"] P = {} for i in range(3): P[S[i]] = 1/3 ps = DiscreteProbabilitySpace(S,P) ps.expectation() # 0.33 This is the random variable with f("A") = 1, f("B") = 2, f("C") = 3, for which the expectation is 2: f = {} for i in range(3): f[S[i]] = i+1 rv = DiscreteRandomVariable(ps,f) rv.expectation() # 2.00 On the other hand, I'd be happy with some syntax for creating a random variable with the uniform distribution P(x) = 1/n on some set or tuple. Currently this gives an error: sage: rv = DiscreteRandomVariable(S,f) but it could easily create the uniform distribution on S behind the scenes. The reason I didn't do this is the following: sage: ps1 = DiscreteProbabilitySpace(S,P) sage: ps2 = DiscreteProbabilitySpace(S,P) sage: ps1 == ps2 False Probably this should be considered a bug. But to avoid creating many copies of the "same" space, one should consider caching the probability spaces. However, since dictionaries are mutable, it can't be cached. The correct solution might be to define additional types for the uniform distribution and other standard distributions (which are the most likely candidates to be created over and over) and have only one instance of each. My type DiscreteProbabilitySpace is also too naive -- it really only implements a FiniteProbabilitySpace. It is suitable for the examples I've taught in classical cryptography, but for a statistics class one certainly needs infinite discrete probability spaces and more classes for distributions on continuous intervals. Some thought also needs to be given to their ease of use. The usual approach to this is to first write some documentation which describes the desired behavior and then implement the classes and functions. --David ----- Forwarded message from "David R. Kohel" <[EMAIL PROTECTED]> ----- Date: Mon, 1 Dec 2008 09:04:12 +0100 From: "David R. Kohel" <[EMAIL PROTECTED]> To: William Stein <[EMAIL PROTECTED]> Cc: Paul Butler <[EMAIL PROTECTED]> Subject: Re: Fwd: [sage-devel] Expected value of probability space User-Agent: Mutt/1.5.6i 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 -~----------~----~----~----~------~----~------~--~---