Sage: Ticket #10770: Expectation of probability spaces is defined improperly
https://trac.sagemath.org/ticket/10770
<p>
I'm not sure if anyone really maintains the file
</p>
<p>
sage/local/lib/python2.6/site-packages/sage/probability/random_variable.py
</p>
<p>
but there's a definite problem with the way expectation is defined.
</p>
<pre class="wiki">sage: X=frequency_distribution([1,1,1,2,2,2])
sage: X.expectation()
0.500000000000000
</pre><p>
Instead of weighting values by probabilities the function sums the squares of the probabilities involved. It should be an easy fix, I"ll try to put a patch up soon, if you're used to submitting patches and bored, you'll probably get to it before me.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/10770
Trac 1.2Jason GroutFri, 11 Feb 2011 04:29:34 GMT
https://trac.sagemath.org/ticket/10770#comment:1
https://trac.sagemath.org/ticket/10770#comment:1
<p>
I think I understand it a bit differently, but I'm not an expert in statistics. For me, it's easier to understand if we use letters rather than numbers.
</p>
<pre class="wiki">sage: X=frequency_distribution('AAAABBB')
sage: X('A')
0.500000000000000
</pre><p>
So the value of 'A' is 0.5.
</p>
<pre class="wiki">sage: X.probability_space()('A')
0.500000000000000
</pre><p>
So the probability of 'A' is also 0.5. Thus summing up the products of the values (0.5 for each of 'A' and 'B') and the probabilities of (also 0.5 for each of 'A' and 'B') gives 0.5.
</p>
<p>
I don't know if that's the natural way to do things, but I do see some of the reasoning behind the code.
</p>
TicketJason GroutFri, 11 Feb 2011 04:37:53 GMT
https://trac.sagemath.org/ticket/10770#comment:2
https://trac.sagemath.org/ticket/10770#comment:2
<p>
Maybe this is what you intended?
</p>
<pre class="wiki">sage: P=frequency_distribution([1,1,1,2,2,2])
sage: P
Discrete probability space defined by {1: 0.500000000000000, 2: 0.500000000000000}
sage: X=DiscreteRandomVariable(P, {1:1, 2:2})
sage: X
Discrete random variable defined by {1: 1, 2: 2}
sage: X.expectation()
1.50000000000000
</pre><p>
Maybe the confusion is that <a class="missing wiki">DiscreteProbabilitySpace?</a> inherits from <a class="missing wiki">DiscreteRandomVariable?</a> (and so inherits the expectation method from <a class="missing wiki">DiscreteRandomVariable?</a>)? Again, I'm not a statistician, so I'm not sure what's natural.
</p>
TicketJason GroutFri, 11 Feb 2011 04:41:21 GMT
https://trac.sagemath.org/ticket/10770#comment:3
https://trac.sagemath.org/ticket/10770#comment:3
<p>
This is more clear, at least in my mind:
</p>
<pre class="wiki">sage: S='AAABBB'
sage: P=frequency_distribution(S)
sage: P
Discrete probability space defined by {'A': 0.500000000000000, 'B': 0.500000000000000}
sage: X=DiscreteRandomVariable(P, {'A':1, 'B':2})
sage: X.expectation()
1.50000000000000
</pre><p>
That says we want a probability space that calculates the frequency over the set 'AB' of the letters in the string 'AAABBB'. So we first define the probability space. Then we create a random variable by assigning the value 1 to 'A' and 2 to 'B'. Then we can ask for the expectation of the random variable, which uses the probability space and our mapping of S to real numbers to correctly calculate the expectation.
</p>
<p>
I guess the confusing thing before was that the value of 1 was the frequency of 1, not the actual number 1.
</p>
TicketJamie WeigandtFri, 11 Feb 2011 05:46:42 GMTstatus changed; resolution set
https://trac.sagemath.org/ticket/10770#comment:4
https://trac.sagemath.org/ticket/10770#comment:4
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<p>
Oh, oops! Sorry for being confused here. Maybe this could have better documentation, but I think there's already a ticket open for that. So I suppose this should be closed.
</p>
<p>
Thanks Jason for showing me what I was trying to do!
</p>
TicketMinh Van NguyenSat, 12 Feb 2011 04:31:28 GMTmilestone changed
https://trac.sagemath.org/ticket/10770#comment:5
https://trac.sagemath.org/ticket/10770#comment:5
<ul>
<li><strong>milestone</strong>
changed from <em>sage-4.7</em> to <em>sage-duplicate/invalid/wontfix</em>
</li>
</ul>
Ticket