Probability Distributions (lib601.dist)¶
Discrete probability distributions
-
class
lib601.dist.DDist(elts)¶ Discrete probability distribution. Can be sparse, in the sense that elements that are not explicitly contained in the dictionary are assumed to have zero probability.
Parameters: elts – A dictionary mapping support elements to their associated probabilities. The values of this dictionary (the probabilities) must be nonnegative, and they must sum to 1. -
condition(testFunc)¶ Parameters: testFunc – a function mapping support elements to booleans: Trueif the element should be included in the resulting distribution, andFalseotherwise.Returns: new distribution, conditioned on the event described by testfunc
-
draw()¶ Returns: A randomly drawn element from the distribution
-
expectation(f)¶ Returns: The expected value of function f, with respect to this distribution
-
max_prob_elt()¶ Returns: The element in this domain with maximum probability
-
prob(elt)¶ Parameters: elt – an element of the domain of this distribution Returns: the probability associated with elt
-
project(mapFunc)¶ Parameters: mapFunc – a function mapping elements in the support to modified elements Returns: a DDistover some new set of elements, where the probability associated with each element \(e\) in the original distribution is associated with \(e'\) = \(\mathtt{mapFunc}(e)\) in the new distribution.
-
support()¶ Returns: A list (in arbitrary order) of the elements of this distribution with non-zero probabability.
-
-
lib601.dist.bayes_rule(PA, prob_b_given_a, b)¶ Parameters: Returns: Updated distribution given evidence, \(\Pr(A~|~B=b)\)
-
lib601.dist.delta_dist(v)¶ Distribution with all of its probability mass on value
vParameters: v – the element that should have probability 1
-
lib601.dist.make_joint_distribution(PA, prob_b_given_a)¶ Create a joint distribution on \(\Pr(A,~B)\) (in that order), represented as a
DDistParameters:
-
lib601.dist.mixture(d1, d2, p)¶ A mixture of two
DDistinstances,d1andd2, with mixture parameterp. Probability of an element \(x\) under this distribution is \(p \cdot \mathtt{d1.prob}(x) + (1 - p) \cdot \mathtt{d2.prob}(x)\).
-
lib601.dist.square_dist(lo, hi, loLimit=None, hiLimit=None)¶ Construct and return a DDist over integers. The distribution will have a uniform distribution on integers from
lotohi-1(inclusive). Any probability mass that would be belowloLimitor abovehiLimitis instead assigned toloLimitorhiLimit.
-
lib601.dist.total_probability(PA, prob_b_given_a)¶ Parameters: Returns: \(\Pr(B)\), computed using the law of total probability.
-
lib601.dist.triangle_dist(peak, halfWidth, loLimit=None, hiLimit=None)¶ Construct and return a DDist over integers. The distribution will have its peak at index
peakand fall off linearly from there, reaching 0 at an indexhalfWidthon either side ofpeak. Any probability mass that would be belowloLimitor abovehiLimitis instead assigned toloLimitorhiLimit
-
lib601.dist.uniform_dist(elts)¶ Uniform distribution over a given finite set of
eltsParameters: elts – list of any kind of item