{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a routine to return a Gaussian PDF value given an input; include an option to return the natural log as well" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def gauss(x,mu,sig,log=False) :\n", " \"\"\" Funtion to evaluate a Gaussian and log(Gaussian)\n", " \"\"\"\n", " if log :\n", " return \n", " else :\n", " return " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a routine to return the likelihood of a full data set (sample), where each point is drawn from a Gaussian with known mean and standard deviation. Include a keyword option (log=) to return the log(likelihood) instead" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def likelihood(sample,mu,sig,log=False) :\n", " \"\"\" Calculate likelihood or ln(L) of a sample, given a mean and standard deviation\n", " \"\"\"\n", " p=1\n", " sum=0\n", " for i,s in enumerate(sample) :\n", " if log :\n", " sum += \n", " else :\n", " p *= \n", " \n", " if log : return sum \n", " else : return p " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw a random sample from a normal distribution with some mean and standard devation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu=\n", "sig=\n", "n=\n", "sample=np.random.normal(mu, sig, size=n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now numerically try a range of \"guesses\" for the mean, calculate the likelihood for each, and determine the maximum likelihood value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lik=[]\n", "# array of input guesses\n", "x=np.arange\n", "for mu in x :\n", " lik.append(likelihood(sample,mu,sig))\n", " \n", "plt.plot(x,lik)\n", "imax=np.argmax(lik)\n", "print('max: ',x[imax])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstrate that the maximum likelihood value is equal to the mean of the sample" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x,lik)\n", "mean=\n", "plt.plot([mean,mean],plt.ylim())\n", "print('max: ',x[imax])\n", "print('mean: ',mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given what we have previously discussed in class, what is your estimate of the uncertainty of the maximum likelihood estimator (i.e. the mean!)?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER HERE: \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Experiment with samples with different standard deviations. How does the shape/breadth of the likelihood change with the sample standard deviation?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER HERE: \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a routine to return a Poisson PDF instead of a Gaussian." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def poiss(x,mu,log=False) :\n", " \"\"\" Funtion to evaluate a Poissonn PDF and log(Poisson)\n", " \"\"\"\n", " if log :\n", " return \n", " else :\n", " return " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now do the same exercise of drawing a sample from a Poisson distribution, calculate the maximum likelihood, and compare with the mean. In particular, try this for Poisson distribution with a very low mean (<5)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.11" } }, "nbformat": 4, "nbformat_minor": 2 }