(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 27821, 682] NotebookOptionsPosition[ 26506, 633] NotebookOutlinePosition[ 26937, 650] CellTagsIndexPosition[ 26894, 647] WindowFrame->Normal ContainsDynamic->True *) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["MLEs and MAPs", "Title", CellChangeTimes->{{3.3984625771490726`*^9, 3.398462606901127*^9}, { 3.398466900639695*^9, 3.3984669028210278`*^9}}], Cell["Joseph E. Gonzalez", "Text", CellChangeTimes->{{3.398462740046832*^9, 3.398462743872199*^9}}], Cell[CellGroupData[{ Cell["Is it Learning...", "Subtitle", CellChangeTimes->{{3.398462714154646*^9, 3.398462715382607*^9}, { 3.3984627546071787`*^9, 3.398462773964181*^9}, {3.398463038359494*^9, 3.398463039265473*^9}}], Cell["\<\ In class we considered the problem of flipping a thumbtack. Let's put some \ of the magic back into learning by playing a simple game. I will place a \ coin in either my left or right hand. If you guess the correct hand you get \ the coin. \ \>", "Text", CellChangeTimes->{{3.3984602057255487`*^9, 3.398460376939501*^9}, 3.398461270650323*^9, {3.398463067187011*^9, 3.3984631050420303`*^9}}], Cell["\<\ Suppose you were designing a robot to play this game. You would want it to \ learn which hand I favor. If I charge you to play, you may also want to \ learn how much I favor one hand over the other. If we assume that my current \ choice of hand is independent of previous hand choices, you would likely want \ to solve the thumbtack problem discussed in class. \ \>", "Text", CellChangeTimes->{{3.398461281665975*^9, 3.39846132794969*^9}, { 3.398461399194721*^9, 3.39846144301772*^9}, {3.39846147403703*^9, 3.398461769597225*^9}, {3.39846181218423*^9, 3.3984618754333353`*^9}, { 3.398462797682025*^9, 3.3984630237477016`*^9}, {3.3984631251605797`*^9, 3.398463300865614*^9}, {3.39846756119656*^9, 3.398467586776408*^9}}], Cell[TextData[{ "From time to time I will try to compose a ", StyleBox["Mathematica", FontSlant->"Italic"], " demonstration notebook for you to play with the graphs and concepts \ presented in class." }], "Text", CellChangeTimes->{{3.3984633582649517`*^9, 3.398463387339952*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Likelihood", "Subtitle", CellChangeTimes->{{3.398463305773367*^9, 3.398463305938181*^9}, { 3.3984633998293858`*^9, 3.3984634068250513`*^9}}], Cell[TextData[{ "The following demonstration shows the plot of the data likelihood function \ with respect to the value of the parameter \[Theta] for different values of \ the data ", Cell[BoxData[ FormBox[ RowBox[{"D", "=", RowBox[{"{", RowBox[{ SubscriptBox["\[Alpha]", "H"], ",", SubscriptBox["\[Alpha]", "T"]}], "}"}]}], TraditionalForm]]] }], "Text", CellChangeTimes->{{3.3984634201272717`*^9, 3.398463421627632*^9}, { 3.398463470157967*^9, 3.398463553951007*^9}, {3.398463605347084*^9, 3.398463613342394*^9}, {3.39846764888245*^9, 3.398467665641746*^9}, { 3.398467696044363*^9, 3.398467696824958*^9}, {3.398467742200921*^9, 3.398467757151567*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{"n", "=", "20"}], ";"}], "\[IndentingNewLine]", RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"Plot", "[", RowBox[{ RowBox[{ SuperscriptBox["\[Theta]", "h"], " ", SuperscriptBox[ RowBox[{"(", RowBox[{"1", "-", "\[Theta]"}], ")"}], "t"]}], ",", RowBox[{"{", RowBox[{"\[Theta]", ",", "0", ",", "1"}], "}"}], ",", RowBox[{"AxesLabel", "\[Rule]", " ", RowBox[{"{", RowBox[{"\"\<\[Theta]\>\"", ",", "\"\
\""}], "}"}]}], ",", RowBox[{"PlotLabel", "\[Rule]", RowBox[{"\"\<(\!\(\*SubscriptBox[\(\[Alpha]\), \(H\)]\)=\>\"", "<>", RowBox[{"ToString", "[", "h", "]"}], "<>", "\"\<, \!\(\*SubscriptBox[\(\[Alpha]\), \(T\)]\)=\>\"", " ", "<>", " ", RowBox[{"ToString", "[", "t", "]"}], "<>", "\"\<)\>\""}]}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "h", ",", "3", ",", "\"\<\!\(\*SubscriptBox[\(\[Alpha]\), \(H\)]\)\>\""}], "}"}], ",", "0", ",", "n", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "t", ",", "3", ",", "\"\<\!\(\*SubscriptBox[\(\[Alpha]\), \(T\)]\)\>\""}], "}"}], ",", "0", ",", "n", ",", "1"}], "}"}]}], "]"}]}], "Input", CellChangeTimes->{{3.398463567260326*^9, 3.398463599784747*^9}, { 3.39846363158537*^9, 3.3984636732936707`*^9}, {3.398463730374741*^9, 3.398463800570305*^9}, {3.398463836719531*^9, 3.398463845532794*^9}, { 3.3984639086067743`*^9, 3.398463931040599*^9}, {3.398463992143897*^9, 3.398464089849262*^9}, {3.398464146544834*^9, 3.398464226213574*^9}, { 3.3984668572516527`*^9, 3.398466875167938*^9}}], Cell[BoxData[ TagBox[ StyleBox[ DynamicModuleBox[{$CellContext`h$$ = 0, $CellContext`t$$ = 0, Typeset`show$$ = True, Typeset`bookmarkList$$ = {}, Typeset`bookmarkMode$$ = "Menu", Typeset`animator$$, Typeset`animvar$$ = 1, Typeset`name$$ = "\"untitled\"", Typeset`specs$$ = {{{ Hold[$CellContext`h$$], 3, "\!\(\*SubscriptBox[\(\[Alpha]\), \(H\)]\)"}, 0, 20, 1}, {{ Hold[$CellContext`t$$], 3, "\!\(\*SubscriptBox[\(\[Alpha]\), \(T\)]\)"}, 0, 20, 1}}, Typeset`size$$ = {450., {154., 161.}}, Typeset`update$$ = 0, Typeset`initDone$$, Typeset`skipInitDone$$ = True, $CellContext`h$37653$$ = 0, $CellContext`t$37654$$ = 0}, DynamicBox[Manipulate`ManipulateBoxes[ 1, StandardForm, "Variables" :> {$CellContext`h$$ = 3, $CellContext`t$$ = 3}, "ControllerVariables" :> { Hold[$CellContext`h$$, $CellContext`h$37653$$, 0], Hold[$CellContext`t$$, $CellContext`t$37654$$, 0]}, "OtherVariables" :> { Typeset`show$$, Typeset`bookmarkList$$, Typeset`bookmarkMode$$, Typeset`animator$$, Typeset`animvar$$, Typeset`name$$, Typeset`specs$$, Typeset`size$$, Typeset`update$$, Typeset`initDone$$, Typeset`skipInitDone$$}, "Body" :> Plot[$CellContext`\[Theta]^$CellContext`h$$ ( 1 - $CellContext`\[Theta])^$CellContext`t$$, {$CellContext`\[Theta], 0, 1}, AxesLabel -> {"\[Theta]", "P(D|\[Theta])"}, PlotLabel -> StringJoin["(\!\(\*SubscriptBox[\(\[Alpha]\), \(H\)]\)=", ToString[$CellContext`h$$], ", \!\(\*SubscriptBox[\(\[Alpha]\), \(T\)]\)=", ToString[$CellContext`t$$], ")"]], "Specifications" :> {{{$CellContext`h$$, 3, "\!\(\*SubscriptBox[\(\[Alpha]\), \(H\)]\)"}, 0, 20, 1}, {{$CellContext`t$$, 3, "\!\(\*SubscriptBox[\(\[Alpha]\), \(T\)]\)"}, 0, 20, 1}}, "Options" :> {}, "DefaultOptions" :> {}], ImageSizeCache->{494.5, {220.5, 227.}}, SingleEvaluation->True], Deinitialization:>None, DynamicModuleValues:>{}, SynchronousInitialization->True, UnsavedVariables:>{Typeset`initDone$$}, UntrackedVariables:>{Typeset`size$$}], "Manipulate", Deployed->True, StripOnInput->False], Manipulate`InterpretManipulate[1]]], "Output", CellChangeTimes->{ 3.398464150028273*^9, {3.398464220091257*^9, 3.398464226746992*^9}, 3.398466876667012*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Beta Distribution", "Subtitle", CellChangeTimes->{{3.3984642625428762`*^9, 3.398464264864565*^9}}], Cell[TextData[{ "In class we also introduced the Beta distribution which is the conjugate \ prior of the Bernoulli distribution. Alternatively, if we treat the \ parameter \[Theta] as a random variable, then we typically assume that \ \[Theta] is drawn from a Beta distribution. The Beta distribution is itself \ parameterized by two values ", Cell[BoxData[ FormBox[ SubscriptBox["\[Beta]", "H"], TraditionalForm]]], " and ", Cell[BoxData[ FormBox[ SubscriptBox["\[Beta]", "T"], TraditionalForm]]], "." }], "Text", CellChangeTimes->{{3.398464267978813*^9, 3.3984644599587812`*^9}, { 3.398467300600881*^9, 3.3984673034686832`*^9}, {3.398467335835832*^9, 3.398467350328505*^9}, 3.398467771120989*^9, {3.3984679686556377`*^9, 3.398467977728384*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"Plot", "[", RowBox[{ RowBox[{"PDF", "[", RowBox[{ RowBox[{"BetaDistribution", "[", RowBox[{"\[Alpha]", ",", "\[Beta]"}], "]"}], ",", "\[Theta]"}], "]"}], ",", RowBox[{"{", RowBox[{"\[Theta]", ",", "0", ",", "1"}], "}"}], ",", RowBox[{"AxesLabel", "\[Rule]", " ", RowBox[{"{", RowBox[{"\"\<\[Theta]\>\"", ",", "\"\
\""}], "}"}]}], ",", " ", RowBox[{"PlotRange", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", "Automatic"}], "}"}]}], "}"}]}], ",", "\[IndentingNewLine]", RowBox[{"PlotLabel", " ", "\[Rule]", " ", RowBox[{"\"\<(\!\(\*SubscriptBox[\(\[Beta]\), \(H\)]\)=\>\"", "<>", RowBox[{"ToString", "[", "\[Alpha]", "]"}], "<>", "\"\<, \!\(\*SubscriptBox[\(\[Beta]\), \(T\)]\)=\>\"", "<>", RowBox[{"ToString", "[", "\[Beta]", "]"}], "<>", "\"\<)\>\""}]}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "\[Alpha]", ",", "1", ",", "\"\<\!\(\*SubscriptBox[\(\[Beta]\), \(H\)]\)\>\""}], "}"}], ",", "0", ",", "4"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "\[Beta]", ",", "1", ",", "\"\<\!\(\*SubscriptBox[\(\[Beta]\), \(T\)]\)\>\""}], "}"}], ",", "0", ",", "4"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.398464483565795*^9, 3.3984645643451014`*^9}, { 3.398464602395162*^9, 3.398464616798511*^9}, {3.398466448752433*^9, 3.398466457588498*^9}, {3.398467882901579*^9, 3.398467888044367*^9}, { 3.398468014493657*^9, 3.398468019147232*^9}, {3.398516142057756*^9, 3.398516157875298*^9}, {3.3985170906042347`*^9, 3.398517138332477*^9}}], Cell[BoxData[ TagBox[ StyleBox[ DynamicModuleBox[{$CellContext`\[Alpha]$$ = 0.195, $CellContext`\[Beta]$$ = 0.195, Typeset`show$$ = True, Typeset`bookmarkList$$ = {}, Typeset`bookmarkMode$$ = "Menu", Typeset`animator$$, Typeset`animvar$$ = 1, Typeset`name$$ = "\"untitled\"", Typeset`specs$$ = {{{ Hold[$CellContext`\[Alpha]$$], 1, "\!\(\*SubscriptBox[\(\[Beta]\), \(H\)]\)"}, 0, 4}, {{ Hold[$CellContext`\[Beta]$$], 1, "\!\(\*SubscriptBox[\(\[Beta]\), \(T\)]\)"}, 0, 4}}, Typeset`size$$ = { 450., {154., 161.}}, Typeset`update$$ = 0, Typeset`initDone$$, Typeset`skipInitDone$$ = True, $CellContext`\[Alpha]$557$$ = 0, $CellContext`\[Beta]$558$$ = 0}, DynamicBox[Manipulate`ManipulateBoxes[ 1, StandardForm, "Variables" :> {$CellContext`\[Alpha]$$ = 1, $CellContext`\[Beta]$$ = 1}, "ControllerVariables" :> { Hold[$CellContext`\[Alpha]$$, $CellContext`\[Alpha]$557$$, 0], Hold[$CellContext`\[Beta]$$, $CellContext`\[Beta]$558$$, 0]}, "OtherVariables" :> { Typeset`show$$, Typeset`bookmarkList$$, Typeset`bookmarkMode$$, Typeset`animator$$, Typeset`animvar$$, Typeset`name$$, Typeset`specs$$, Typeset`size$$, Typeset`update$$, Typeset`initDone$$, Typeset`skipInitDone$$}, "Body" :> Plot[ PDF[ BetaDistribution[$CellContext`\[Alpha]$$, $CellContext`\[Beta]$$], \ $CellContext`\[Theta]], {$CellContext`\[Theta], 0, 1}, AxesLabel -> {"\[Theta]", "p(\[Theta])"}, PlotRange -> {{0, 1}, {0, Automatic}}, PlotLabel -> StringJoin["(\!\(\*SubscriptBox[\(\[Beta]\), \(H\)]\)=", ToString[$CellContext`\[Alpha]$$], ", \!\(\*SubscriptBox[\(\[Beta]\), \(T\)]\)=", ToString[$CellContext`\[Beta]$$], ")"]], "Specifications" :> {{{$CellContext`\[Alpha]$$, 1, "\!\(\*SubscriptBox[\(\[Beta]\), \(H\)]\)"}, 0, 4}, {{$CellContext`\[Beta]$$, 1, "\!\(\*SubscriptBox[\(\[Beta]\), \(T\)]\)"}, 0, 4}}, "Options" :> {}, "DefaultOptions" :> {}], ImageSizeCache->{494.5, {220.5, 227.}}, SingleEvaluation->True], Deinitialization:>None, DynamicModuleValues:>{}, SynchronousInitialization->True, UnsavedVariables:>{Typeset`initDone$$}, UntrackedVariables:>{Typeset`size$$}], "Manipulate", Deployed->True, StripOnInput->False], Manipulate`InterpretManipulate[1]]], "Output", CellChangeTimes->{{3.39846454028283*^9, 3.3984645649537354`*^9}, 3.398464617460828*^9, 3.398466460304537*^9, 3.3984678928241*^9, 3.3984680213501062`*^9, 3.398517142270302*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Being Bayesian", "Subtitle", CellChangeTimes->{{3.3984648125186863`*^9, 3.398464815841021*^9}}], Cell[TextData[{ "If we use a ", Cell[BoxData[ FormBox[ RowBox[{"Beta", "(", RowBox[{ SubscriptBox["\[Beta]", "H"], ",", SubscriptBox["\[Beta]", "T"]}], ")"}], TraditionalForm]]], " prior on ", Cell[BoxData[ FormBox["\[Theta]", TraditionalForm]]], " then we obtain the new likelihood. The red line is proportional to the \ likelihood ", Cell[BoxData[ FormBox[ RowBox[{"P", "(", RowBox[{"D", "|", "\[Theta]"}], ")"}], TraditionalForm]]], " used to compute the MLE and the blue line is proportional to the bayesian \ posterior ", Cell[BoxData[ FormBox[ RowBox[{"P", "(", RowBox[{"\[Theta]", "|", "D"}], ")"}], TraditionalForm]]], " used to compute the MAP." }], "Text", CellChangeTimes->{{3.398464881345537*^9, 3.398464922270751*^9}, { 3.398464957007257*^9, 3.398464962332693*^9}, {3.3984670788535957`*^9, 3.398467200270493*^9}, {3.3984680771368647`*^9, 3.398468078510509*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{"n", "=", "20"}], ";"}], "\[IndentingNewLine]", RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"GraphicsGrid", "[", RowBox[{"{", RowBox[{"{", "\[IndentingNewLine]", RowBox[{ RowBox[{"Plot", "[", RowBox[{ RowBox[{"PDF", "[", RowBox[{ RowBox[{"BetaDistribution", "[", RowBox[{"u", ",", "v"}], "]"}], ",", "\[Theta]"}], "]"}], ",", RowBox[{"{", RowBox[{"\[Theta]", ",", "0", ",", "1"}], "}"}], ",", RowBox[{"AxesLabel", "\[Rule]", " ", RowBox[{"{", RowBox[{"\"\<\[Theta]\>\"", ",", "\"\
\""}], "}"}]}],
",", " ",
RowBox[{"PlotRange", "\[Rule]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"0", ",", "1"}], "}"}], ",",
RowBox[{"{",
RowBox[{"0", ",", "Automatic"}], "}"}]}], "}"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"PlotLabel", " ", "\[Rule]", " ",
RowBox[{
"\"\