% \documentclass[serif]{beamer} % Serif for Computer Modern math font. \documentclass[serif, handout]{beamer} % Handout to ignore pause statements \hypersetup{colorlinks,linkcolor=,urlcolor=red} \usefonttheme{serif} % Looks like Computer Modern for non-math text -- nice! \setbeamertemplate{navigation symbols}{} % Suppress navigation symbols \usetheme{AnnArbor} % CambridgeUS % \usetheme{Frankfurt} % Displays section titles on top: Fairly thin but still swallows some material at bottom of crowded slides % \usetheme{Berlin} % Displays sections on top % \usetheme{Berkeley} \usepackage[english]{babel} \usepackage{amsmath} % for binom % \usepackage{graphicx} % To include pdf files! % \definecolor{links}{HTML}{2A1B81} % \definecolor{links}{red} \setbeamertemplate{footline}[frame number] \mode \title{\texttt{SageMath}: Open source software for symbolic mathematics\footnote{ This slide show is an open-source document. See last slide for copyright information.}} \subtitle{Jerry Brunner, Department of Statistical Sciences} \date{} % To suppress date \begin{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \titlepage \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Symbolic calculation} %\framesubtitle{} \begin{itemize} \item Lots of software can carry out numerical calculations, and so can \texttt{SageMath}. \pause \item What makes \texttt{SageMath} special is that it can also do \emph{symbolic} computation. \pause \item That is, it is able to manipulate symbols as well as numbers. \pause \item[] \item How much of the ``math" we do is actually a set of clerical tasks? \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Free and open source} \pause %\framesubtitle{} The \texttt{SageMath} website is \href{http://www.sagemath.org}{\texttt{http://www.sagemath.org}}. \pause \begin{itemize} \item There are commercial versions like \begin{itemize} \item Mathematica (\href{http://www.wolfram.com}{http://www.wolfram.com}) \item Maple (\href{http://www.maplesoft.com} {http://www.maplesoft.com}) \end{itemize}\pause \item \texttt{SageMath} is not the only free alternative. \pause \item It includes quite a few of the others. \pause \item Python based. \pause \item On your computer, or use \texttt{SageMathCloud}. \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{\texttt{SageMath} on your computer: A free download} \pause %\framesubtitle{} \begin{itemize} \item It's a big download. \pause \item Native versions for Mac and linux. \pause \item Windows version lives in a virtual linux machine. \pause \item First download Oracle's VirtualBox. \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Web browser interface} \pause %\framesubtitle{} When you start up for the first time you will see something like this: \pause \begin{center} \includegraphics[width=4in]{Graphics/shot1} \end{center} \pause \vspace{4mm} Click on New Worksheet. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Give it a name} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot2} \end{center} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Check Typeset} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot3} \end{center} \pause Click in the box. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Click in the box: \underline{evaluate} appears} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot4} \end{center} \pause \vspace{3mm} Type code in the box and click \underline{evaluate}. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Click \underline{evaluate}} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot5} \end{center} \pause %\vspace{3mm} Type more code in the next box. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Keep going} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot6} \end{center} \vspace{3mm} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Integer arithmetic} %\framesubtitle{} \begin{center} \includegraphics[width=4in]{Graphics/shot7} \end{center} \vspace{3mm} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Same calculation with R} \framesubtitle{\texttt{SageMath}'s answer was 11652140094042840181048771286026412160} \pause {\color{blue} \begin{verbatim} > prod(1:100)/(prod(1:60)*prod(1:30)*prod(1:10)) \end{verbatim} } % End color \begin{verbatim} [1] 1.165214e+37 \end{verbatim} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Charlotte's homework problem} \pause %\framesubtitle{} {\LARGE \begin{displaymath} \frac{4x}{4x^2-8x+7} + \frac{3x}{4x^2-10x+7} = 1 \end{displaymath} \pause {\footnotesize Note $16 \times 7 = 112$.} \pause \begin{displaymath} 16x^4 - 100x^3 + 200x^2 - 175x + 49 = 0 \end{displaymath} } % End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Use \texttt{SageMath}} \framesubtitle{Solve $16x^4 - 100x^3 + 200x^2 - 175x + 49 = 0$} \pause %{\small % or footnotesize or scriptsize \begin{tabular}{|l|} \hline \begin{minipage}{4in} \begin{verbatim} f = 16*x^4 - 100*x^3 + 200*x^2 - 175*x + 49 factor(f) \end{verbatim} \end{minipage} \\ \hline \end{tabular} \vspace{3mm} % \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue} ${\left(4 \, x^{2} - 9 \, x + 7\right)} {\left(2 \, x - 1\right)} {\left(2 \, x - 7\right)}$ } % End colour %} % End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Solve directly} \framesubtitle{$ \frac{4x}{4x^2-8x+7} + \frac{3x}{4x^2-10x+7} = 1$} \pause %{\small % or footnotesize or scriptsize \begin{tabular}{|l|} \hline \begin{minipage}{4in} \begin{verbatim} eq = 4*x/(4*x^2-8*x+7) + 3*x/(4*x^2-10*x+7) == 1 solve(eq,x) # Solve eq for x \end{verbatim} \end{minipage} \\ \hline \end{tabular} \vspace{3mm} % \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue} $\left[x = \left(\frac{1}{2}\right), x = \left(\frac{7}{2}\right), x = -\frac{1}{8} i \, \sqrt{31} + \frac{9}{8}, x = \frac{1}{8} i \, \sqrt{31} + \frac{9}{8}\right]$ } % End colour %} % End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Derivatives} \framesubtitle{} \pause Calculate $\frac{\partial^3}{\partial x^3} \left(\frac{e^{4x}}{1+e^{4x}}\right)$. \pause This is something you could do by hand, but would you want to? \pause {\small % or footnotesize or scriptsize \begin{tabular}{|l|} \hline \begin{minipage}{4.5in} \begin{verbatim} f(x) = exp(4*x)/(1+exp(4*x)) derivative(f(x),x,3) # Derivative with respect to x, 3 times \end{verbatim} \end{minipage} \\ \hline \end{tabular} \vspace{3mm} % \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue} $\frac{64 \, e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1} - \frac{448 \, e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}} + \frac{768 \, e^{\left(12 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{3}} - \frac{384 \, e^{\left(16 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{4}}$ } % End colour \vspace{3mm} \pause % Another box \begin{tabular}{|l|} \hline \begin{minipage}{4in} \begin{verbatim} factor(_) # Factor the preceding expression \end{verbatim} \end{minipage} \\ \hline \end{tabular} \vspace{3mm} % \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue} $\frac{64 \, {\left(e^{\left(8 \, x\right)} - 4 \, e^{\left(4 \, x\right)} + 1\right)} e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{4}}$ } % End colour } % End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] % A SageMath frame \frametitle{Look at $f(x)$ again} \pause % \framesubtitle{} \pause %{\small % or footnotesize or scriptsize \begin{tabular}{|l|} \hline \begin{minipage}{4in} \begin{verbatim} f(x) \end{verbatim} \end{minipage} \\ \hline \end{tabular} \vspace{3mm} % \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue} $ \frac{e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1}$ \pause } % End colour %} % End size \vspace{5mm} It looks like a cumulative distribution function. Is it? \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] % A SageMath frame \frametitle{Check $\displaystyle{\lim_{x \rightarrow -\infty}f(x)}$ and $\displaystyle{\lim_{x \rightarrow \infty}f(x)}$}\pause % \framesubtitle{} \begin{tabular}{|l|} \hline \verb:limit(f(x),x=-Infinity);limit(f(x),x=Infinity) : \\ \hline \end{tabular} \vspace{1mm} {\color{blue}\underline{evaluate}} \pause {\color{blue} \noindent \begin{tabular}{l} $0$ \\ $1$ \end{tabular} \\ } \pause \vspace{3mm} Or, \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:show(limit(f(x),x=-Infinity)) : \\ \verb:show(limit(f(x),x=Infinity)) : \\ \hline \end{tabular} \vspace{1mm} {\color{blue}\underline{evaluate} \vspace{1mm} \pause \noindent \begin{tabular}{l} $0$ \\ \\ $1$ \end{tabular} \\ } \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{The (single) derivative of $f(x)$ is a density. } \framesubtitle{Since $f(x)$ is a cumulative distribution function} \pause \begin{tabular}{|l|} \hline \verb:derivative(f(x),x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{3mm} $ 4 \, \frac{e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}} - 4 \, \frac{e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}} $ } \pause \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:# Another way : \\ \verb:f(x).derivative(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\frac{4 \, e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1} - \frac{4 \, e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}}$} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Simplify and save the density} \pause %\framesubtitle{} \begin{tabular}{|l|} \hline \verb:g(x) = factor(f(x).derivative(x)); g(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$ \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}}$ } \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Plot $g(x) = \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}}$} \pause %\framesubtitle{} % kurve = plot(g(x),x,-5,5); % kurve.save(filename='kurve.pdf') \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:plot(g(x),x,-5,5) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \begin{center} \includegraphics[width=3.5in]{Graphics/logisticdensity} \end{center} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Is $g(x)$ symmetric around zero?} \pause %\framesubtitle{} If so, then expected value = median = 0. \pause \vspace{5mm} \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:f(0) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{3mm} $\frac{1}{2}$} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Symmetry about zero means $g(x)=g(-x)$} \pause %\framesubtitle{} \begin{tabular}{|l|} \hline \verb:g(x)-g(-x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{3mm} $  \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{2}} - \frac{4 \, e^{\left(-4 \, x\right)}}{{\left(e^{\left(-4 \, x\right)} + 1\right)}^{2}}$ } \pause \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:factor(g(x)-g(-x)) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{3mm} $0$ } \vspace{3mm} \pause \noindent Is this right? \pause Yes. To see it, \pause just multiply numerator and denominator of $g(-x) = \frac{4 \, e^{-4 \, x}}{{\left(e^{-4 \, x} + 1\right)}^{2}}$ by $e^{8x}$\pause, obtaining $g(x) = \frac{4 \, e^{4 \, x}}{{\left(e^{4 \, x} + 1\right)}^{2}}$. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Replace 4 with $\theta$ in $f(x) = \frac{e^{4 \, x}}{e^{4 \, x} + 1}$} \pause %\framesubtitle{} Need to declare any symbolic variable other than $x$. \pause \vspace{12mm} \noindent \begin{tabular}{|l|} \hline \verb:var('theta') : \\ \verb:F(x) = exp(theta*x)/(1+exp(theta*x)); F(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\frac{e^{\left(\theta x\right)}}{e^{\left(\theta x\right)} + 1}$} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Is $F(x) = \frac{e^{\theta \, x}}{e^{\theta \, x} + 1}$ a distribution function?} \pause %\framesubtitle{$F(x) = $} \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:limit(F(x),x=-Infinity) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \begin{verbatim} Traceback (click to the left of this block for traceback) ... Is theta positive, negative, or zero? \end{verbatim} } \pause \begin{itemize} \item Good question! \pause \item Actually, the question is asked by the excellent open-source calculus program \texttt{Maxima}\pause, and \texttt{Sage} relays the question. \pause \item In \texttt{Maxima}, you could answer the question interactively. \pause \item In \texttt{SageMath}, click in the box and edit the code. \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Assume $\theta>0$} \pause %\framesubtitle{} \begin{tabular}{|l|} \hline \verb:assume(theta>0) : \\ \verb:F(x).limit(x=-oo); F(x).limit(x=oo) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \noindent \begin{tabular}{l} $0$ \\ $1$ \end{tabular} } \vspace{5mm} \pause Differentiate to get the density. \pause \vspace{3mm} \begin{tabular}{|l|} \hline \verb:# New f(x) will replace the old one. : \\ \verb:f(x) = factor(F(x).derivative(x)); f(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} } \pause \vspace{3mm} {\color{blue}$\frac{\theta e^{\left(\theta x\right)}}{{\left(e^{\left(\theta x\right)} + 1\right)}^{2}}$} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{$f(x)$ is symmetric about zero.} \framesubtitle{So the expected value must be zero.} \pause \begin{tabular}{|l|} \hline \verb:factor(f(x)-f(-x)) # Checking symmetry : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause $0$} \vspace{6mm} \pause \begin{tabular}{|l|} \hline \verb:# Expected value : \\ \verb:integrate(x*f(x),x,-oo,oo) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause $0$ } \vspace{3mm} \pause The variance emerges in terms of an obscure function called the \texttt{polylog}. The calculation will not be shown. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Add a location parameter} \pause %\framesubtitle{} \begin{tabular}{|l|} \hline \verb:var('mu') : \\ \verb:F(x) = exp(theta*(x-mu))/(1+exp(theta*(x-mu))); F(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\frac{e^{\left(-{\left(\mu - x\right)} \theta\right)}}{e^{\left(-{\left(\mu - x\right)} \theta\right)} + 1}$} \pause \vspace{3mm} I can't control the order of variables in \texttt{SageMath} output. It looks alphabetical, with the \texttt{m} in \texttt{mu} coming before $x$. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{The density is tasteless} \pause \framesubtitle{As \texttt{SageMath} writes it} \begin{tabular}{|l|} \hline \verb:f(x) = factor( F(x).derivative(x) ); f(x) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\frac{\theta e^{\left(\mu \theta + \theta x\right)}}{{\left(e^{\left(\mu \theta\right)} + e^{\left(\theta x\right)}\right)}^{2}}$} \pause \vspace{5mm} By hand, \pause \begin{eqnarray*} f(x) & = & \frac{\theta e^{\left(\mu \theta + \theta x\right)}} {{\left(e^{\mu\theta} + e^{\theta x}\right)}^{2}} \pause ~ \cdot ~ \frac{e^{-2\mu\theta}}{e^{-2\mu\theta}} \\ \pause & = & \cdots \\ \pause & = & \frac{\theta e^{\theta\left(x-\mu\right)}} {{\left(e^{\theta\left(x-\mu\right)} + 1\right)}^{2}} \end{eqnarray*} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Simulating from the distribution} \framesubtitle{} \begin{itemize} \item A good source of cute homework problems. \pause \item Give students the density $f(x) = \frac{\theta e^{\theta\left(x+\mu\right)}} {{\left(e^{\theta x} + e^{\theta \mu}\right)}^{2}} $. \pause %\item Even showing it's a density is no joke. \pause \item $\mu$ can be estimated with $\overline{x}$ but it's not obvious. \pause \item $\theta$ has to be estimated numerically. \pause \item Need to generate some data from this distribution. \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} \frametitle{Simulation by the inverse CDF method} \framesubtitle{A well-known rule} \pause \begin{itemize} \item Let $F(x)$ be the CDF of a continuous random variable \pause with $F \uparrow$ on its support\pause, so that $F^{-1}$ exists. \pause \item If $U \sim U(0,1)$\pause, then $X = F^{-1}(U) \sim F(x)$. \pause \item In words, \pause if $U$ is a random variable with a uniform density on the interval $(0,1)$ \pause and $F(x)$ is the cumulative distribution function of a continuous random variable\pause, then if you transform $U$ with the \emph{inverse} of $F(x)$\pause, the result is a random variable with distribution function $F(x)$. \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Of course you could do this by hand, but \ldots} \pause %\framesubtitle{} \begin{tabular}{|l|} \hline \verb:# Inverse of cdf : \\ \verb:var('X U') : \\ \verb:solve(F(X)==U,X) # Solve F(X)=U for X : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\left[X = \frac{\mu \theta + \log\left(-\frac{U}{U - 1}\right)}{\theta}\right]$} \pause \vspace{5mm} It might be a bit better to write this as \pause \begin{displaymath} X = \mu + \frac{1}{\theta} \log \left(\frac{U}{1-U}\right), \end{displaymath} \pause but what \texttt{Sage} gives us is quite nice. \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Simulate data from $F(x)$ using \texttt{R}} \framesubtitle{$X = \mu + \frac{1}{\theta} \log \left(\frac{U}{1-U}\right)$} \pause {\footnotesize % or scriptsize {\color{blue} \begin{verbatim} > n = 20; mu = -2; theta = 4 > U = runif(n) > X = mu + log(U/(1-U))/theta; X \end{verbatim} \pause } % End color \begin{verbatim} [1] -1.994528 -2.455775 -2.389822 -2.996261 -1.477381 -2.422011 -1.855653 [8] -2.855570 -2.358733 -1.712423 -2.075641 -1.908347 -2.018621 -2.019441 [15] -1.956178 -2.015682 -2.846583 -1.727180 -1.726458 -2.207717 \end{verbatim} } % End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Some symbolic variables do not need to be declared} \pause %\framesubtitle{} {\footnotesize \noindent \begin{tabular}{|l|} \hline \verb:pi : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{2mm} $\pi$ } \pause \vspace{2mm} Is that really the ratio of a circle's circumference to its diameter, or just the Greek letter? \pause \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:cos(pi) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{2mm} $-1$ } \vspace{2mm} That's pretty promising. Evaluate it numerically. \pause \vspace{3mm} \begin{tabular}{|l|} \hline \verb:n(pi) # Could also say pi.n() : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate} \pause \vspace{2mm} $3.14159265358979$ } \vspace{2mm} }% End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Try using \texttt{pi} in the normal distribution} \pause %\framesubtitle{} {\footnotesize \begin{tabular}{|l|} \hline \verb:# Normal density : \\ \verb:var('mu, sigma') : \\ \pause \verb:assume(sigma>0) : \\ \pause \verb:f(x) = 1/(sigma*sqrt(2*pi)) * exp(-(x-mu)^2/(2*sigma^2)); f(x): \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\frac{\sqrt{2} e^{\left(-\frac{{\left(\mu - x\right)}^{2}}{2 \, \sigma^{2}}\right)}}{2 \, \sqrt{\pi} \sigma}$} \vspace{3mm} \pause \begin{tabular}{|l|} \hline \verb:# Integrate the density : \\ \verb:integrate(f(x),x,-oo,oo) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue} \underline{evaluate} \pause \vspace{2mm} $1$ } }% End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Expected value and variance of the normal} \pause \framesubtitle{Direct calculation} {\footnotesize \begin{tabular}{|l|} \hline \verb:# E(X) : \\ \verb:integrate(x*f(x),x,-oo,oo) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue} \underline{evaluate} \pause \vspace{2mm} $\mu$ } \pause \vspace{2mm} \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:# E(X-mu)^2 is the variance : \\ \verb:integrate((x-mu)^2*f(x),x,-oo,oo) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue} \underline{evaluate} \pause \vspace{2mm} $\sigma^2$ } }% End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Moment-generating function} \pause %\framesubtitle{} {\footnotesize \begin{tabular}{|l|} \hline \verb:# Moment-generating function M(t) = E(e^{Xt}) : \pause \\ \verb:var('t') : \pause \\ \verb:M(t) = integrate(exp(x*t)*f(x),x,-oo,oo); M(t) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$e^{\left(\frac{1}{2} \, \sigma^{2} t^{2} + \mu t\right)}$} \pause \vspace{3mm} \vspace{3mm} \noindent \begin{tabular}{|l|} \hline \verb:# Differentiate four times, set t=0 to get E(X^4) : \pause \\ \verb:derivative(M(t),t,4)(t=0) : \\ \hline \end{tabular} \vspace{3mm} \noindent {\color{blue}\underline{evaluate}} \pause \vspace{3mm} {\color{blue}$\mu^{4} + 6 \, \mu^{2} \sigma^{2} + 3 \, \sigma^{4}$} }% End size \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[fragile] \frametitle{Geometric distribution} \pause %\framesubtitle{} A coin with $Pr\{\mbox{Head}\} = \theta$ is tossed repeatedly, and $X$ is the number of tosses required to get the first head. \pause \vspace{5mm} {\footnotesize \begin{tabular}{|l|} \hline \verb:# Geometric : \\ \verb:var('theta') : \\ \verb:assume(0