My first time

This is my first time using Jupyter and coding in Python. Jupyter notebook appears to be a possible alternative to RStudio and allows one to also code in R and other languages. We can use type text as Markdown and also use Latex.

Please continue to do your homework and ICAs and projects in R and Rstudio as we have throughout the term. Do not submit work in Python or with Jupyter.

Euler's method

Recall that in the movie Hidden Figures, the ancient method is used to solve an important problem at NASA.

We are concerned with giving a numerical solution the initial-value problem (IVP) given by \begin{equation*} y' = f(t,y) \text{ and } y(t_0) = y_0. \end{equation*}

Suppose we are looking for a solution $y$ to the IVP. We know that $y(t_0) = y_0$, but we can also approximate using tangent lines for $t_1$ near $t_0$ so that $$ y(t_1) \approx y_1:= y(t_0) + y'(t_0) (t_1 - t_0) = y_0 + f(t_0, y_0) (t_1-t_0).$$ Now, if we want another $t_2$ near $t_1$, we can repeat the same reasoning to obtain $$y(t_2) \approx y_2 := y_1+ f(t_1, y_1) (t_2-t_1);$$ in general for the times $t_0 < t_1 < t_2 < \cdots < t_n < \cdots t_N$, we obtain the iteration: $$ y_{n+1} = y_n + f(t_n, y_n)(t_{n+1} - t_n).$$ Typically, one might choose a uniform mesh size, $h = y_{i+1} - y_i$, where $h$ is small. Consider the function $\phi$ given by interpolating the points $y_0, y_1, \ldots, y_n$ linearly. The claim is the function $\phi$ approximates a solution to the initial value problem, under some regularity condition, and as the mesh size $h$ decreases the function $\phi$ will converge to a solution of the initial value problem. The issue here is that each time we make an approximation $y_n$ it is based on $y_{n-1}$ which is itself an approximation, one needs to make sure these errors do not add up too fast, and get out of hand.

We will now introduce enough basic Python to code Euler's method, to solve the initial value problem $y' = y$ with $y(0) = 1$ on the interval $[0,1]$ and as a result approximate the value of $e$.

Our code will illustrate some mathematical operations in Python using the numpy package, and also the use of a while loop.

Probability and statistics in Python

In theory, we should be able to write equivalent code that we produced to illustrate various stochastic systems. Here, we will start with an illustration of the concept of a confidence interval.

The following code will illustrate the concept of what exactly does $90$ percent refer to in a $t$-distribution based confidence interval. We consider the case, where we have a sample of five independent normal random variables, where both the mean and variance are unknown. With the standard notation, we have $\alpha=0.1$, $n=5$, and $n-1=4$; the critical value is given by $t(0.05,4)=2.132$. Given a random sample $x= (x_1, \ldots, x_5)$, we know that the confidence interval is given by

$$ \bar{x} \pm 2.132 \cdot \frac{s(x)}{ \sqrt{5}}.$$

The true mean either lies in this interval or not; once we are given $x$, in the classical sense, there is no probability involved. However, if we repeat this experiment many times, roughly $90$ percent of those confidence intervals will contain the true mean. We will illustrate this by sampling from standard normals, repeatedly, so that $0$ will be contained in $90$ percent of the confidence intervals.

The code will illustrate the use a function and a for loop.

Other resources