#9670: Bring probability/random_variable.py to 100% coverage
-----------------------------+----------------------------------------------
   Reporter:  kcrisman       |          Owner:  mvngu   
       Type:  defect         |         Status:  new     
   Priority:  major          |      Milestone:  sage-5.0
  Component:  documentation  |       Keywords:          
Work_issues:                 |       Upstream:  N/A     
   Reviewer:                 |         Author:          
     Merged:                 |   Dependencies:          
-----------------------------+----------------------------------------------

Comment(by kohel):

 I agree the code (among the first I wrote) should be improved or
 rewritten.

 Also note that "discrete" should be "finite" -- I never implemented any
 code  for infinite discrete probability spaces and the finiteness of X a
 rather heavy assumption.

 That said, despite the lack of documentation, I assert that the above
 output  is correct.  The idea of the discrete or finite probability spaces
 is that  the underlying set S of X can be anything:

 {{{
 sage: n = 6 sage: S = list('abcdef')
 sage: P = dict([(S[i],1/n) for i in [1..n]])
 sage: X = DiscreteProbabilitySpace(S, P)
 sage: X.expectation()
 0.166666666666667
 }}}

 It so happens that a probability function IS a random variable so that
 one can ask for its expectation.  Thus probability space inherits from
 random variables which generalize them.

 With S so defined, it should be clear that it certainly doesn't make
 sense to form \sum_{x \in S} x p(x), since S need not be contained in  a
 module over the reals for which this sum makes sense.  Rather for a
 probability space, the expectation is \sum p(x)^2.^

 For reasons of avoiding coefficient blowup in the rationals, the default
 uses a real ring. This is a design decision which is perhaps questionable,
 but avoids problems with undergraduate teaching. If you want the
 probability space with value ring in the rationals, then explicitly set
 the codomain:

 {{{
 sage: X = DiscreteProbabilitySpace(S, P, codomain = QQ)
 sage: X.expectation()
 1/6
 }}}

 To get the random variable you intended, you need to first create the
 probability space as above, then define the random variable with respect
 to this:

 {{{
 sage: f = dict([(S[i],i+1) for i in range(n)])
 sage: F = DiscreteRandomVariable(X,f)
 sage: F.expectation()
 3.50000000000000
 }}}

 Again, for exact values, set the codomain:

 {{{
 sage: F = DiscreteRandomVariable(X,f,codomain = QQ)
 sage: F.expectation()
 7/2
 }}}

 This two-step creation for a random variable is a bit awkward  if all you
 want is the uniform probability function. Since this  occurs quite often
 in practice (for a finite probability space), it might be practical to
 have a shortcut which defines the  uniform probability space by default.
 In that case the syntax  might be changed from (X,f,codomain) to
 (f,X,codomain), or  without change, just letting X be a list or finite set
 and  setting up the uniform probability on it.  This is NOT implemented,
 but would be a more intuitive construction for what you intended:

 {{{
 sage: S = list('abcdef')
 sage: F = DiscreteRandomVariable(S,dict([(S[i],i+1) for i in range(n)]))
 sage: X = F.probability_space(); X
 Discrete probability space defined by {'a': 1/6, 'c': 1/6, 'b': 1/6, 'e':
 1/6, 'd': 1/6, 'f': 1/6}
 }}}

 At the time of writing this (and maybe still true) there was no class of
 finite sets and morphisms of sets, which would provide  a more natural
 input to the constructors for probability space  and random variables.  It
 would be a cleaner construction to  have the codomain attached to the
 function f or probability  function p, and have some general mechanism for
 checking that  the domains of f and p agree and that codomain of f is a
 module  over the codomain of p. Python dictionaries were the closest  I
 could come (without implementing a category of sets) to the  datastructure
 for morphism of sets.

 Currently the domain of a random variable is assumed to be some  real ring
 or subring but vector-valued random variables would  make sense in a
 number of settings.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/9670#comment:3>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to