Added exercises for mle
This commit is contained in:
27
statistics/exercises/mlepdffit.m
Normal file
27
statistics/exercises/mlepdffit.m
Normal file
@@ -0,0 +1,27 @@
|
||||
% plot gamma pdfs:
|
||||
xx = 0.0:0.1:10.0;
|
||||
shapes = [ 1.0, 2.0, 3.0, 5.0];
|
||||
cc = jet(length(shapes) );
|
||||
for i=1:length(shapes)
|
||||
yy = gampdf(xx, shapes(i), 1.0);
|
||||
plot(xx, yy, '-', 'linewidth', 3, 'color', cc(i,:), ...
|
||||
'DisplayName', sprintf('s=%.0f', shapes(i)) );
|
||||
hold on;
|
||||
end
|
||||
|
||||
% generate gamma distributed random numbers:
|
||||
n = 50;
|
||||
x = gamrnd(3.0, 1.0, n, 1);
|
||||
|
||||
% histogram:
|
||||
[h,b] = hist(x, 15);
|
||||
h = h/sum(h)/(b(2)-b(1));
|
||||
bar(b, h, 1.0, 'DisplayName', 'data');
|
||||
|
||||
% maximum likelihood estimate:
|
||||
p = mle(x, 'distribution', 'gamma');
|
||||
yy = gampdf(xx, p(1), p(2));
|
||||
plot(xx, yy, '-k', 'linewidth', 5, 'DisplayName', 'mle' );
|
||||
|
||||
hold off;
|
||||
legend('show');
|
||||
31
statistics/exercises/mlepropfit.m
Normal file
31
statistics/exercises/mlepropfit.m
Normal file
@@ -0,0 +1,31 @@
|
||||
m = 2.0; % slope
|
||||
sigma = 1.0; % standard deviation
|
||||
n = 100; % number of data pairs
|
||||
|
||||
% data pairs:
|
||||
x = 5.0*rand(n, 1);
|
||||
y = m*x + sigma*randn(n, 1);
|
||||
|
||||
% fit:
|
||||
slope = mleslope(x, y);
|
||||
fprintf('slopes:\n');
|
||||
fprintf('original = %.2f\n', m);
|
||||
fprintf(' fit = %.2f\n', slope);
|
||||
|
||||
% lines:
|
||||
xx = 0.0:0.1:5.0; % x-axis values
|
||||
yorg = m*xx;
|
||||
yfit = slope*xx;
|
||||
|
||||
% plot:
|
||||
plot(xx, yorg, '-r', 'linewidth', 5);
|
||||
hold on;
|
||||
plot(xx, yfit, '-g', 'linewidth', 2);
|
||||
plot(x, y, 'ob');
|
||||
hold off;
|
||||
legend('data', 'original', 'fit', 'Location', 'NorthWest');
|
||||
legend('boxoff')
|
||||
xlabel('x');
|
||||
ylabel('y');
|
||||
|
||||
savefigpdf(gcf, 'mlepropfit.pdf', 12, 7);
|
||||
6
statistics/exercises/mleslope.m
Normal file
6
statistics/exercises/mleslope.m
Normal file
@@ -0,0 +1,6 @@
|
||||
function slope = mleslope(x, y )
|
||||
% Compute the maximum likelihood estimate of the slope
|
||||
% of a line through the origin
|
||||
% given the data pairs in the vectors x and y.
|
||||
slope = sum(x.*y)/sum(x.*x);
|
||||
end
|
||||
30
statistics/exercises/mlestd.m
Normal file
30
statistics/exercises/mlestd.m
Normal file
@@ -0,0 +1,30 @@
|
||||
% draw random numbers:
|
||||
n = 50;
|
||||
mu = 3.0;
|
||||
sigma =2.0;
|
||||
x = randn(n,1)*sigma+mu;
|
||||
fprintf(' mean of the data is %.2f\n', mean(x))
|
||||
fprintf('standard deviation of the data is %.2f\n', std(x))
|
||||
|
||||
% standard deviation as parameter:
|
||||
psigs = 1.0:0.01:3.0;
|
||||
% matrix with the probabilities for each x and psigs:
|
||||
lms = zeros(length(x), length(psigs));
|
||||
for i=1:length(psigs)
|
||||
psig = psigs(i);
|
||||
p = exp(-0.5*((x-mu)/psig).^2.0)/sqrt(2.0*pi)/psig;
|
||||
lms(:,i) = p;
|
||||
end
|
||||
lm = prod(lms, 1); % likelihood
|
||||
loglm = sum(log(lms), 1); % log likelihood
|
||||
|
||||
% plot likelihood of standard deviation:
|
||||
subplot(1, 2, 1);
|
||||
plot(psigs, lm );
|
||||
xlabel('standard deviation')
|
||||
ylabel('likelihood')
|
||||
subplot(1, 2, 2);
|
||||
plot(psigs, loglm);
|
||||
xlabel('standard deviation')
|
||||
ylabel('log likelihood')
|
||||
savefigpdf(gcf, 'mlestd.pdf', 12, 5);
|
||||
191
statistics/exercises/statistics04.tex
Normal file
191
statistics/exercises/statistics04.tex
Normal file
@@ -0,0 +1,191 @@
|
||||
\documentclass[12pt,a4paper,pdftex]{exam}
|
||||
|
||||
\usepackage[german]{babel}
|
||||
\usepackage{pslatex}
|
||||
\usepackage[mediumspace,mediumqspace,Gray]{SIunits} % \ohm, \micro
|
||||
\usepackage{xcolor}
|
||||
\usepackage{graphicx}
|
||||
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
|
||||
|
||||
%%%%% layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
|
||||
\pagestyle{headandfoot}
|
||||
\ifprintanswers
|
||||
\newcommand{\stitle}{: L\"osungen}
|
||||
\else
|
||||
\newcommand{\stitle}{}
|
||||
\fi
|
||||
\header{{\bfseries\large \"Ubung 4\stitle}}{{\bfseries\large Statistik}}{{\bfseries\large 26. Oktober, 2015}}
|
||||
\firstpagefooter{Prof. Dr. Jan Benda}{Phone: 29 74573}{Email:
|
||||
jan.benda@uni-tuebingen.de}
|
||||
\runningfooter{}{\thepage}{}
|
||||
|
||||
\setlength{\baselineskip}{15pt}
|
||||
\setlength{\parindent}{0.0cm}
|
||||
\setlength{\parskip}{0.3cm}
|
||||
\renewcommand{\baselinestretch}{1.15}
|
||||
|
||||
%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{listings}
|
||||
\lstset{
|
||||
language=Matlab,
|
||||
basicstyle=\ttfamily\footnotesize,
|
||||
numbers=left,
|
||||
numberstyle=\tiny,
|
||||
title=\lstname,
|
||||
showstringspaces=false,
|
||||
commentstyle=\itshape\color{darkgray},
|
||||
breaklines=true,
|
||||
breakautoindent=true,
|
||||
columns=flexible,
|
||||
frame=single,
|
||||
xleftmargin=1em,
|
||||
xrightmargin=1em,
|
||||
aboveskip=10pt
|
||||
}
|
||||
|
||||
%%%%% math stuff: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amssymb}
|
||||
\usepackage{bm}
|
||||
\usepackage{dsfont}
|
||||
\newcommand{\naZ}{\mathds{N}}
|
||||
\newcommand{\gaZ}{\mathds{Z}}
|
||||
\newcommand{\raZ}{\mathds{Q}}
|
||||
\newcommand{\reZ}{\mathds{R}}
|
||||
\newcommand{\reZp}{\mathds{R^+}}
|
||||
\newcommand{\reZpN}{\mathds{R^+_0}}
|
||||
\newcommand{\koZ}{\mathds{C}}
|
||||
|
||||
%%%%% page breaks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\newcommand{\continue}{\ifprintanswers%
|
||||
\else
|
||||
\vfill\hspace*{\fill}$\rightarrow$\newpage%
|
||||
\fi}
|
||||
\newcommand{\continuepage}{\ifprintanswers%
|
||||
\newpage
|
||||
\else
|
||||
\vfill\hspace*{\fill}$\rightarrow$\newpage%
|
||||
\fi}
|
||||
\newcommand{\newsolutionpage}{\ifprintanswers%
|
||||
\newpage%
|
||||
\else
|
||||
\fi}
|
||||
|
||||
%%%%% new commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\newcommand{\qt}[1]{\textbf{#1}\\}
|
||||
\newcommand{\pref}[1]{(\ref{#1})}
|
||||
\newcommand{\extra}{--- Zusatzaufgabe ---\ \mbox{}}
|
||||
\newcommand{\code}[1]{\texttt{#1}}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\begin{document}
|
||||
|
||||
\input{instructions}
|
||||
|
||||
|
||||
\begin{questions}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\question \qt{Maximum Likelihood der Standardabweichung}
|
||||
Wir wollen uns die Likelihood und die Log-Likelihood am Beispiel der
|
||||
Absch\"atzung der Standardabweichung verdeutlichen.
|
||||
\begin{parts}
|
||||
\part Ziehe $n=50$ normalverteilte Zufallsvariablen mit Mittelwert $\mu=3$
|
||||
und einer Standardabweichung $\sigma=2$.
|
||||
|
||||
\part
|
||||
Plotte die Likelihood (aus dem Produkt der Wahrscheinlichkeiten) und
|
||||
die Log-Likelihood (aus der Summe der logarithmierten
|
||||
Wahrscheinlichkeiten) f\"ur die Standardabweichung als Parameter. Vergleiche die
|
||||
Position der Maxima mit der aus den Daten berechneten Standardabweichung.
|
||||
|
||||
\part
|
||||
Erh\"ohe $n$ auf 1000. Was passiert mit der Likelihood, was mit der Log-Likelihood? Warum?
|
||||
\end{parts}
|
||||
\begin{solution}
|
||||
\lstinputlisting{mlestd.m}
|
||||
\includegraphics[width=1\textwidth]{mlestd}
|
||||
\end{solution}
|
||||
|
||||
\continue
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\question \qt{Maximum-Likelihood-Sch\"atzer einer Ursprungsgeraden}
|
||||
In der Vorlesung haben wir eine Gleichung f\"ur die Maximum-Likelihood
|
||||
Absch\"atzung der Steigung einer Ursprungsgeraden hergeleitet.
|
||||
\begin{parts}
|
||||
\part \label{mleslopefunc} Schreibe eine Funktion, die in einem $x$ und einem
|
||||
$y$ Vektor die Datenpaare \"uberreicht bekommt und die Steigung der
|
||||
Ursprungsgeraden, die die Likelihood maximiert, zur\"uckgibt
|
||||
($\sigma=\text{const}$).
|
||||
|
||||
\part
|
||||
Schreibe ein Skript, das Datenpaare erzeugt, die um eine
|
||||
Ursprungsgerade mit vorgegebener Steigung streuen. Berechne mit der
|
||||
Funktion aus \pref{mleslopefunc} die Steigung aus den Daten,
|
||||
vergleiche mit der wahren Steigung, und plotte die urspr\"ungliche
|
||||
sowie die gefittete Gerade zusammen mit den Daten.
|
||||
|
||||
\part
|
||||
Ver\"andere die Anzahl der Datenpunkte, die Steigung, sowie die
|
||||
Streuung der Daten um die Gerade.
|
||||
\end{parts}
|
||||
\begin{solution}
|
||||
\lstinputlisting{mleslope.m}
|
||||
\lstinputlisting{mlepropfit.m}
|
||||
\includegraphics[width=1\textwidth]{mlepropfit}
|
||||
\end{solution}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\question \qt{Maximum-Likelihood-Sch\"atzer einer Wahrscheinlichkeitsdichtefunktion}
|
||||
Verschiedene Wahrscheinlichkeitsdichtefunktionen haben Parameter, die
|
||||
nicht so einfach wie der Mittelwert und die Standardabweichung einer
|
||||
Normalverteilung direkt aus den Daten berechnet werden k\"onnen. Solche Parameter
|
||||
m\"ussen dann aus den Daten mit der Maximum-Likelihood-Methode gefittet werden.
|
||||
|
||||
Um dies zu veranschaulichen ziehen wir uns diesmal Zufallszahlen, die nicht einer
|
||||
Normalverteilung entstammen, sonder aus der Gamma-Verteilung.
|
||||
\begin{parts}
|
||||
\part
|
||||
Finde heraus welche Funktion die Wahrscheinlichkeitsdichtefunktion
|
||||
(probability density function) der Gamma-Verteilung in \code{matlab}
|
||||
berechnet.
|
||||
|
||||
\part
|
||||
Plotte mit Hilfe dieser Funktion die Wahrscheinlichkeitsdichtefunktion
|
||||
der Gamma-Verteilung f\"ur verschiedene Werte des (positiven) ``shape'' Parameters.
|
||||
Den ``scale'' Parameter setzen wir auf Eins.
|
||||
|
||||
\part
|
||||
Finde heraus mit welcher Funktion Gammaverteilte Zufallszahlen in
|
||||
\code{matlab} gezogen werden k\"onnen. Erzeuge mit dieser Funktion
|
||||
50 Zufallszahlen mit einem der oben geplotteten ``shape'' Parameter.
|
||||
|
||||
\part
|
||||
Berechne und plotte ein normiertes Histogramm dieser Zufallszahlen.
|
||||
|
||||
\part
|
||||
Finde heraus mit welcher \code{matlab}-Funktion eine beliebige
|
||||
Verteilung (``distribution'') und die Gammaverteilung an die
|
||||
Zufallszahlen nach der Maximum-Likelihood Methode gefittet werden
|
||||
kann.
|
||||
|
||||
\part
|
||||
Bestimme mit dieser Funktion die Parameter der
|
||||
Gammaverteilung aus den Zufallszahlen.
|
||||
|
||||
\part
|
||||
Plotte anschlie{\ss}end
|
||||
die Gammaverteilung mit den gefitteten Parametern.
|
||||
\end{parts}
|
||||
\begin{solution}
|
||||
\lstinputlisting{mlepdffit.m}
|
||||
%\includegraphics[width=1\textwidth]{mlepdffit}
|
||||
\end{solution}
|
||||
|
||||
\end{questions}
|
||||
|
||||
\end{document}
|
||||
Reference in New Issue
Block a user