This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cs401r_w2016:lab3 [2015/12/23 20:52] admin |
cs401r_w2016:lab3 [2021/06/30 23:42] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===Objective:=== | + | ====Objective:==== |
To understand how to sample from different distributions, and to | To understand how to sample from different distributions, and to | ||
Line 6: | Line 6: | ||
a small library of random variable types. | a small library of random variable types. | ||
+ | ---- | ||
====Deliverable:==== | ====Deliverable:==== | ||
Line 22: | Line 23: | ||
{{:cs401r_w2016:lab3_2d.png?nolink|}} | {{:cs401r_w2016:lab3_2d.png?nolink|}} | ||
+ | ---- | ||
====Description:==== | ====Description:==== | ||
- | You must implement a random variable object. These should all inherit from a base random variable object that supports the following methods: | + | You must implement seven random variable objects. For each type, you should be able to sample from that distribution, and compute the log-likelihood of a particular value. All of your classes should inherit from a base random variable object that supports the following methods: |
<code python> | <code python> | ||
Line 39: | Line 41: | ||
pass | pass | ||
- | def ll( self ): | + | def log_likelihood( self ): |
- | return NaN | + | pass |
def propose( self ): | def propose( self ): | ||
Line 47: | Line 49: | ||
</code> | </code> | ||
- | For example, your univariate Gaussian class might look like this: | + | You don't need to implement the ''get'' or ''propose'' methods yet. For example, your univariate Gaussian class might look like this: |
<code python> | <code python> | ||
Line 60: | Line 62: | ||
return self.mu + self.sigma * numpy.Random.randn() | return self.mu + self.sigma * numpy.Random.randn() | ||
- | def ll( self ): | + | def log_likelihood( self, X, mu, sigma ): |
- | return | + | return -numpy.log( sigma*numpy.sqrt(2*pi) ) - (X-mu)**2/(sigma**2) |
| | ||
</code> | </code> | ||
+ | **Given that framework, you should implement:** | ||
+ | * The following one dimensional, continuous valued distributions. To visualize | ||
+ | these, you should plot a histogram of sampled values, and also plot the PDF of the random variable on the | ||
+ | same axis; they should (roughly) match. //Note: it is **not** sufficient to let seaborn estimate the PDF using its built-in KDE estimator; you need to plot the true PDF. In other words, you can't just use seaborn.kdeplot!// | ||
- | + | * ''Beta (a=1, b=3)'' | |
- | * The following one dimensional, continuous valued distributions. For | + | |
- | these, you should also plot the PDF of the random variable on the | + | |
- | same plot; the curves should match. //Note: it is **not** sufficient to let seaborn estimate the PDF using its built-in KDE estimator; you need to plot the true PDF. In other words, you can't just use seaborn.kdeplot!// | + | |
- | + | ||
- | * ''Beta (alpha=1, beta=3)'' | + | |
* ''Poisson (lambda=7)'' | * ''Poisson (lambda=7)'' | ||
* ''Univariate Gaussian (mean=2, variance=3)'' | * ''Univariate Gaussian (mean=2, variance=3)'' | ||
Line 78: | Line 79: | ||
* The following discrete distributions. For these, plot predicted and | * The following discrete distributions. For these, plot predicted and | ||
empirical histograms side-by-side: | empirical histograms side-by-side: | ||
- | * ''Bernoulli (p=0.7)'' | + | * ''Bernoulli (p=0.7)'' (hint: you may need a uniform random number) |
- | * ''Multinomial (theta=[0.1, 0.2, 0.7])'' | + | * ''Multinomial (pvals=[0.1, 0.2, 0.7])'' |
- | * The following multidimensional distributions. For these, | + | * The following multidimensional distributions. For these, use a contour or surface plot to visualize the empirical distribution of samples vs. the PDF: |
- | * Two-dimensional Gaussian | + | * ''Multivariate Gaussian ( mean=[2.0,3.0], cov=[[1.0,0.9],[0.9,1.0]] )'' |
- | * 3-dimensional Dirichlet | + | * ''Dirichlet ( alpha=[ 0.1, 0.2, 0.7 ] )'' |
+ | **Important notes:** | ||
+ | |||
+ | **You //may// use [[http://docs.scipy.org/doc/numpy-1.10.0/reference/routines.random.html|numpy.random]] to sample from the appropriate distributions.** | ||
+ | |||
+ | **You may //not// use any existing code to calculate the log-likelihoods.** But you can, of course, use any online resources or the book to find the appropriate definition of each PDF. | ||
+ | |||
+ | ---- | ||
====Hints:==== | ====Hints:==== | ||
Line 90: | Line 98: | ||
<code python> | <code python> | ||
+ | |||
+ | numpy.random | ||
matplotlib.pyplot.contour | matplotlib.pyplot.contour | ||
Line 106: | Line 116: | ||
</code> | </code> | ||
- | |||