Distributions: Primitives and Combinations
In this section, we will build up some primitive distributions, as well as one means of combining them. See section 8.5 of the readings for information and examples of the types of distributions below.
For each of the boxes below, assume that DDist,
make_joint_distribution, and total_probability are available.
Square Distribution
Define a function square_dist(lo, hi, loLimit=None, hiLimit=None) that
returns an instance of DDist representing a distribution that is
uniform across integer elements from lo to hi-1, inclusive.
Any probability mass that would be associated with elements below
loLimit or above hiLimit is assigned to loLimit or
hiLimit, respectively.
Triangle Distribution
Define a function triangle_dist(peak, halfWidth, loLimit, hiLimit) that
returns an instance of DDist representing a distribution over
integers whose probability mass has its peak at peak and falls off
linearly from there, reaching probability 0 at peak+halfWidth
and peak-halfWidth. As with square_dist, any probability mass
that would be associated with elements below loLimit or above
hiLimit should be assigned to loLimit or hiLimit,
respectively.
Mixture
We can make a new distribution by defining it to be a mixture of two
existing distributions. By specifying two distributions d1 and
d2 and a mixture probability p, the mixture distribution
assigns to each element x a probability equal to p times the
probability of x in distribution d1 plus (1-p) times the
probability of x in distribution d2.
Implement a function mixture(d1,d2,p), which returns an instance of
DDist representing the mixure of d1 and d2 with
mixing probability p.