示例#1
0
 def __init__(self, X, P, codomain = None, check = False):
     r"""
     Create the discrete probability space with probabilities on the
     space X given by the dictionary P with values in the field
     real_field.
     
     EXAMPLES::
     
         sage: S = [ i for i in range(16) ] 
         sage: P = {}
                sage: for i in range(15): P[i] = 2^(-i-1)
         sage: P[15] = 2^-16 
         sage: X = DiscreteProbabilitySpace(S,P)
         sage: X.domain()
         (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
         sage: X.set()
         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
         sage: X.entropy()
                1.9997253418
     
     A probability space can be defined on any list of elements.
     
     EXAMPLES::
     
         sage: AZ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
         sage: S = [ AZ[i] for i in range(26) ]
         sage: P = { 'A':1/2, 'B':1/4, 'C':1/4 }
         sage: X = DiscreteProbabilitySpace(S,P)
         sage: X
         Discrete probability space defined by {'A': 1/2, 'C': 1/4, 'B': 1/4}
         sage: X.entropy()
                1.5
     """
     if codomain is None:
         codomain = RealField()
     if not is_RealField(codomain) and not is_RationalField(codomain):
         raise TypeError, "Argument codomain (= %s) must be the reals or rationals" % codomain
     if check:
         one = sum([ P[x] for x in P.keys() ])
         if is_RationalField(codomain):    
             if not one == 1:
                 raise TypeError, "Argument P (= %s) does not define a probability function"
         else:
             if not Abs(one-1) < 2^(-codomain.precision()+1):
                 raise TypeError, "Argument P (= %s) does not define a probability function"
     ProbabilitySpace_generic.__init__(self, X, codomain)
     DiscreteRandomVariable.__init__(self, self, P, codomain, check)
示例#2
0
 def __init__(self, X, P, codomain=None, check=False):
     r"""
     Create the discrete probability space with probabilities on the
     space X given by the dictionary P with values in the field
     real_field.
     
     EXAMPLES::
     
         sage: S = [ i for i in range(16) ] 
         sage: P = {}
                sage: for i in range(15): P[i] = 2^(-i-1)
         sage: P[15] = 2^-16 
         sage: X = DiscreteProbabilitySpace(S,P)
         sage: X.domain()
         (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
         sage: X.set()
         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
         sage: X.entropy()
                1.9997253418
     
     A probability space can be defined on any list of elements.
     
     EXAMPLES::
     
         sage: AZ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
         sage: S = [ AZ[i] for i in range(26) ]
         sage: P = { 'A':1/2, 'B':1/4, 'C':1/4 }
         sage: X = DiscreteProbabilitySpace(S,P)
         sage: X
         Discrete probability space defined by {'A': 1/2, 'C': 1/4, 'B': 1/4}
         sage: X.entropy()
                1.5
     """
     if codomain is None:
         codomain = RealField()
     if not is_RealField(codomain) and not is_RationalField(codomain):
         raise TypeError, "Argument codomain (= %s) must be the reals or rationals" % codomain
     if check:
         one = sum([P[x] for x in P.keys()])
         if is_RationalField(codomain):
             if not one == 1:
                 raise TypeError, "Argument P (= %s) does not define a probability function"
         else:
             if not Abs(one - 1) < 2 ^ (-codomain.precision() + 1):
                 raise TypeError, "Argument P (= %s) does not define a probability function"
     ProbabilitySpace_generic.__init__(self, X, codomain)
     DiscreteRandomVariable.__init__(self, self, P, codomain, check)