{ "nbformat_minor": 2, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Simulation of measures\n", "\n", "Authors \n", "- Vincent Delecroix\n", "- Thierry Monteil\n", "\n", "We assume that the Sage function `random()` returns a real number in $[0,1]$ uniformly. In other words, `random()` is a realisation of uniform random variable (sometimes called *random variates*). We also assume that subsequent calls to `random()` are independent.\n", "\n", "The aim of this worksheet is to learn how to simulate other random variables using only calls to `random()`.\n", "\n", "## Discrete probabilities\n", "\n", "In this section, we consider discrete probability measures on finite sets like $\\{0, 1, 2, \\ldots, n-1\\}$ and countable sets such as $\\NN$ and $\\ZZ$.\n", "\n", "### Coin tossing\n", "\n", "Write a Sage function `coin_toss()` that returns $0$ or $1$, each with probability $1/2$." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "### Biased coin toss (a.k.a. Bernouilli measure)\n", "\n", "Write a Sage function `biased_coin_toss(p)` that, given a number $p\\in\n", "[0,1]$ returns $0$ with probability $p$ and $1$ with probability $1-p$." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "### Any finite probability\n", "\n", "A probability measure on $\\{0, 1, 2, \\ldots, n-1\\}$ is nothig more than a *probability vector* $p = (p_0, p_1, \\ldots, p_{n-1})$. That is each $p_i$ belongs to $[0, 1]$ and\n", "\n", "$$\\sum_{i=0}^{n-1} p_i = 1$$\n", "\n", "The measure $\\mu$ on $\\{0, 1, \\ldots, n-1\\}$ defined by $p$ is then\n", "\n", "$$\\mu(J) = \\sum_{j \\in J} p_j$$\n", "\n", "Write a Sage function `multi_toss(L)` that, given a list `L=[p_0,p_1,...,p_k]` :\n", "\n", "- checks that each $p_i$ belongs to $[0,1]$\n", "- checks that the sum of the $p_i$ equals $1$.\n", "- returns a number in $\\{0, 1, \\ldots, k\\}$ so that $i$ occurs with probability $p_i$ ($0\\leq i\\leq k$)." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "### Uniform discrete variable\n", "\n", "Could you provide a function `random_discrete_uniform(n)` that simulates uniform random variable on $\\{0, 1, \\ldots, n-1\\}$ without writing any loop (`for` or `while`) or conditional statement (`if`, `elif` or `else`).\n", "\n", "*Hint:* look at the `floor` function" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "### Draw your random generators\n", "\n", "In order to check that your random generators are accurrate we will do some graphics. Statistical tests will be introduced later when we consider the `scipy.stats` module.\n", "\n", "Write a function `multiplicities(sample, n)` that given a list `sample` of elements from `0` to `n-1` (where `n` is a positive integer) returns a list of length `n` whose value at position `i` is the number of occurrences of `i` in `sample`. For example, with input `sample = [1, 3, 3, 0, 3, 1, 3]` and `n=5` the output should be `[1, 2, 0, 4, 0]`." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Add an optional argument `normed` to multiplicities that defaults to `False` and so that if `normed=True` the values of the dictionary are frequencies instead of the number of occurrences. For example, with input `sample = [1, 3, 3, 0, 3, 1, 3]` and `normed=True` the output should be `[1/7, 2/7, 0, 4/7, 0]`." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Using your function `multiplicities(sample, n, normed=False)`, write a function `plot_frequencies(sample, n)` that given a list `sample` of elements from `0` to `n-1` return a bar chart of the frequencies. That is for each element `i` from `0` to `n-1` there should be a rectangle of width 1 and height the frequency horizontally centered above the point `(i,0)`.\n", "\n", "*Hint:* have a look at `polygon2d`," ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Add an optional argument `proba` to your function `plot_frequencies(sample, n)` that defaults to `None` so that if this argument is provided it should be a list of positive number of length `n` and sum $1$ that represent a probability on $\\{0, 1, \\ldots, n-1\\}$. The output graphics should then also contain the values of the theoretical probability (it can be either represented by a line or points)." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "For each of your random generators, make samples of size 10, 100, 1000 and 10000 and for each of them plot the frequencies together with the theoretical ones using the function `plot_frequencies(sample, n, proba=None)`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "How do your sample fit with the theoretical values?\n", "\n", "### Binomial\n", "\n", "A binomial random variable $Y$ with parameter $(n,p)$ takes values in $\\{0, 1, \\ldots, n\\}$ and whose law is given by\n", "\n", "$$\\mathbb{P}(Y = k) = \\binom{n}{k} p^k (1 - p)^{n-k}$$\n", "\n", "We note $Y \\sim \\mathcal{B}(n,p)$.\n", "\n", "Using the explicit formula above write a function `random_binomial1(n, p)` that generates random integers in $\\{0, 1, \\ldots, n\\}$ according to the binomial distribution $B(n,p)$.\n", "\n", "*Hint:* use your function `multi_toss(p)`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Let $X_1$, $X_2$, ..., $X_n$ be a a sequence of iid Bernoulli random variables with parameter $p$, that is $\\mathbb{P}(X_0 = 1) = p$ and $\\mathbb{P}(X_0 = 0) = 1 - p$. Let\n", "\n", "$$Y = X_1 + ... + X_n$$\n", "\n", "Show that the law of $Y$ is a binomial random variable with parameters $(n,p)$.\n", "\n", "Using the above description, write an alternative function `random_binomial2(n, p)` that generates random integers in $\\{0, 1, \\ldots, n\\}$ according to the binomial distribution $\\mathcal{B}(n,p)$.\n", "\n", "*Hint:* use your function `biased_coin_toss(p)`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Draw samples of your two random generators using `plot_samples`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Compare your the two implementations `random_binomial1(n, p)` and `random_binomial2` in terms of accuracy and efficiency.\n", "\n", "### Geometric\n", "\n", "A geometric random variable $Y$ with parameter $p$ is a random variable on $\\{0, 1, 2, \\ldots\\}$ which satisfies\n", "\n", "$$\\mathbb{P}(Y = k) = (1 - p)^k p$$\n", "\n", "This is generally denoted $Y \\sim \\mathcal{G}(p)$.\n", "\n", "Using the same strategy as in `multi_toss` write a function `random_exponential1(p)` that return a non-negative integer according to the distribution $\\mathcal{G}(p)$." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Let $X_0$, $X_1$, $X_2$, ... be a a sequence of iid Bernoulli random variables with parameter $p$, that is $\\mathbb{P}(X_0 = 1) = p$ and $\\mathbb{P}(X_0 = 0) = 1 - p$. Let\n", "\n", "$$Y = \\min \\{i \\in \\{0, 1, \\ldots\\}:\\ X_i = 1\\}$$\n", "\n", "Show that the law of $Y$ is a geometric distribution with parameter $p$.\n", "\n", "Using the above description, write an alternative `random_geometric2(p)` that simulates $\\mathcal{G}(p)$" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Explain why the two implementations `random_exponential1(p)` and `random_exponential2(p)` are essentially the same.\n", "\n", "What is the mean number of calls to `random()` that is done in either function? The maximum number of calls?\n", "\n", "### Poisson\n", "\n", "A Poisson random variable $Y$ with parameter $\\lambda$ is a random variable on $\\{0, 1, 2, \\ldots\\}$ which satisfies\n", "\n", "$$\\mathbb{P}(Y = k) = e^{-\\lambda} \\frac{\\lambda^k}{k!}$$\n", "\n", "We note $Y \\sim \\mathcal{P}(\\lambda)$.\n", "\n", "Using the same strategy as in `multi_toss(p)` and `random_exponential1(p)` write a function `random_poisson1(l)` that return a non-negative integer according to the distribution $\\mathcal{P}(\\lambda)$." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "We will see an alternative way to simulate Poisson random variables after you know how to simulate exponential laws (that are laws with densities).\n", "\n", "## Continous random variables\n", "\n", "### Histograms for densities\n", "\n", "In order to guess the distribution of a sample of points (such as the output of the function `random()`) it is useful to plot histograms. A histogram is built from\n", "\n", "- a sample $x_1$, $x_2$, ..., $x_n$ of real numbers\n", "- a finite number of disjoint intervals (called *bins*) $I_1$, $I_2$, ..., $I_k$ of $\\RR$\n", "\n", "To this data we associate the numbers $n_1$, $n_2$, ..., $n_k$ where $n_i$ counts the number of points $x_j$ that belong to $I_i$. The histogram is the graphics that is made of $k$ rectangles with bases $I_i$ and height $n_i$.\n", "\n", "Make three lists `l10`, `l100` and `l1000` made of respectively 10, 100 and 1000 random numbers obtained with the function `random()`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "For each of the list `l10`, `l100` and `l1000` make an histogram using the function `histogram`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Vary the options `bins` to see how it changes the graphics (hint: look at the documentation to see how this option works)." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "It is also possible to make a graphics so that the sum of the areas of the rectangle is 1. In order to do so, you need to use the option `normed=True`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Compute the lists `m10` (respectively `m100` and `m1000`) that contains the data $exp(x_i)$ for each $x_i$ in `l10` (respectively `l100` and `l1000`). And make histograms for them" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "### Exponential law and Poisson process\n", "\n", "In your course, you have seen how to simulate any random variable using the pseudo-inverse of the repartition function of its law (sometimes called *quantile*). We already used this for some of the discrete variables (which ones?). We will now use this strategy to simulate exponential random variables.\n", "\n", "Recall that an exponential random variable $Y$ with parameter $\\lambda$ is a random variable on $\\RR_+$ whose density is $f_Y(x) = \\frac{1}{\\lambda}\n", "e^{-\\lambda x}$. We note $Y \\sim \\mathcal{E}(\\lambda)$.\n", "\n", "Compute the repartition function of $\\mathcal{E}(\\lambda)$ as well as its inverse.\n", "\n", "Write a function `random_exponential(l)` that given a positive real number `l` simulate an exponential random variable" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Make samples of size, 10, 100, 1000 and 10000 and draw the associated histograms (with the option `normed=True`). On each graphics add the graph of the density function" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "A *Poisson process* on $\\mathbb{R}_+$ with intensity $\\lambda$ is a infinite sequence of random variables $\\{Y_1, Y_2, \\ldots\\}$ so that the increment $Z_1 = Y_1$, $Z_2 = Y_2 - Y_1$, $Z_3 = Y_3 - Y_2$, ... are iid with law $\\mathcal{E}(\\lambda)$. Write a function `poisson_process(l, a)` that given a positive real numbers `l` and `a` return the trace of a Poisson process on $[0, a]$, that is to say $\\{Y_i:\\ Y_i \\leq a\\}$ (which is almost surely a finite set)." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Experiment that the length of `poisson_process(l, 1)` is a Poisson random variable with parameter `l`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Could you prove this fact?\n", "\n", "Provide an alternative `random_poisson2(l)` to simulate a Poisson random variable" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Compare your two implementations `random_poisson1(l)` and `random_poisson2(l)`.\n", "\n", "### Statistics of iid uniform variables\n", "\n", "Let $X_1$, $X_2$, ..., $X_n$ be uniform iid variables. Show that $M = \\max(X_1, X_2, ..., X_n)$ has density $f_Y(x) = n x^{n-1}$.\n", "\n", "Implement and compare two functions that simulate `M`\n", "\n", "- a function `random_max1(n)` that uses the explicit formula for the density\n", "- a function `random_max2(n)` that uses the description in terms of `n` uniform variables" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Compute samples of size 10, 100, 1000 and 10000 for each versions, draw the associated histogram together with the graph of the density" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "More generally, given $X_1$, $\\ldots$, $X_n$ as above one can consider the permutation $\\sigma \\in S_n$ so that $X_{\\sigma(1)} \\leq X_{\\sigma(2)} \\leq \\ldots \\leq X_{\\sigma(n)}$. We note by $S_{n,i} = X_{\\sigma(i)}$. For the maximum we have $M = S_{n,n}$.\n", "\n", "Implement a function `random_statistics1(n, i)` that simulates $X_{\\sigma(i)}$ following the strategy of `random_max2(n)`\n", "\n", "*Hint:* You can sort a sample of $n$ uniform and return the $i$-th element." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Draw histograms for sample of size 1000 for $n=3$ and $i=1,2,3$" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Draw histograms for sample of size 1000 for $n=4$ and $i=1,2,3,4$" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "What is the density of $S_{n,1} = \\min(X_1, X_2, \\ldots, X_n)$\n", "\n", "*Hint:* use symetry: $X \\sim \\mathcal{U}([0,1])$ then $1 - X \\sim \\mathcal{U}([0,1])$.\n", "\n", "Show that $S_{3,2}$ has density $6x(1-x)$ and check this result by plotting the density against the histogram" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Could you find a general formula for the density of $S_{n,i}$? Could you find an inverse of the repartition function in order to provide an alternative version of `random_statistics1(n, i)`?\n", "\n", "## Rejection\n", "\n", "The basic method to simulate a random variable is to use the inverse of repartition function. However, as we saw with statistics in the previous section, this method is not always practicable. In this section, we see how to use rejection to avoid computations of the inverse.\n", "\n", "The idea of rejection is to use conditional random variables. In other words we simualte a well known distribution and define a rejection condition so that the result is ignored if this condition is not fullfilled.\n", "\n", "### Uniform density in the disc\n", "\n", "Write a function `uniform_disc()` that simulates a random uniform variable in the disc $D = \\{(x,y):\\ x^2 + y^2 \\leq 1\\}$\n", "\n", "*Hint:* Start from two uniform variables $(X, Y)$ in $[0,1] \\times [0,1]$ rescale them to be uniform variables in $[-1, 1] \\times [-1, 1]$ and reject if they do not belong to $D$" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "By counting the number of tries before a pair of uniform lands in $D$ provides an approximation of the number $\\pi/4 = \\mu(D)$ where $\\mu$ is the uniform probability measure $[-1,1] \\times [-1,1]$ (that is a rescaling of the Lebesgue measure)." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "What can you say about convergence speed of this approximation?\n", "\n", "### Rejection when density is known\n", "\n", "Rejection can be used to avoid inversion of the repartition function when we an explicit formula for the density is available. Given a density function $f: \\RR \\to \\RR_+$ show that if the vector $(X,Y)$ is uniform in the zone between the horizontal axis and the density $\\{(x,y):\\ 0 < y < f(x)\\}$ then $X$ has density $f$.\n", "\n", "Use this method to provide an alternative `random_statistics2(n, i)`" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "What is the mean number of rejects to generate one such statistics?\n", "\n", "### The Box-Muller method for normal variable\n", "\n", "The Box-Muller method provides a change of variable to generate a couple of random independent variables $(X,Y)$ wiht $X \\sim Y \\sim \\mathcal{N}(0,1)$.\n", "\n", "Let $Z = (X,Y)$ as above. Show that the density of $Z$ is given in cartesian coordinates by\n", "\n", "$$f_Z(x,y) = \\frac{1}{2 \\pi} \\exp \\left(- \\frac{x^2 + y^2}{2} \\right)$$\n", "\n", "Show that in polar coordinates $(x,y) = (\\rho \\cos(\\theta), \\rho \\sin(\\theta))$ the density is\n", "\n", "$$\\frac{1}{2\\pi} \\exp(- \\rho^2/2) d\\rho\\, d\\theta$$\n", "\n", "Show that if $(U, R)$ is a couple of independent random variables with $U \\sim \\mathcal{U}([0,1])$ and $R \\sim \\mathcal{E}(1/2)$ then $(\\cos(2\\pi U) \\sqrt{R}, \\sin(2\\pi U) \\sqrt{R}) \\sim (X,Y)$.\n", "\n", "Write a function `pair_of_normals()` that uses this method to simulates a couple of random normal variables" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "Make an histogram of a sample of size 1000 together with the graph of the density" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Using scipy\n", "\n", "The `scipy.stats` module have many predefined densities, random generators, densities, etc. Moreover very efficient sample generators. In order to use this module, the first command that you need to execute is" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from scipy import stats" ], "outputs": [], "metadata": {} }, { "source": [ "Once this is done, you can use the following command to generate a sample of size 100 of $\\mathcal{B}(0.3)$" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "stats.bernoulli(0.3).rvs(100)" ], "outputs": [], "metadata": {} }, { "source": [ "The name `rvs` stands for *random variates* which means realization of a sample of iid variables.\n", "\n", "As you can see, the result is not a Python list but a numpy array. Basic operations on numpy array are the same as with lists (but not all!). One main difference between lists and arrays is that array contain only one kind of data (integers in the above example).\n", "\n", "Using tab completion, which of the random generators evoked in this worksheet are already implemented" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "This module, also gives access to (numerical) densitiy, repartition function and inverse repartition functions\n", "\n", "- `pdf` : probability density function (only for continuous variable)\n", "- `cdf` : cumulative distribution function (ie repartition function)\n", "- `ppf` : percent point function (inverse of cdf)\n", "\n", "All these functions can be used with a number or a list as parameters" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# the value of the density function of a normal N(0,1)\n", "# at 1.1\n", "stats.norm(0,1).pdf(1.1)" ], "outputs": [], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# the value of the density function of a normal N(0,1)\n", "# at -1.3, 0.7, 1.3 and 2.6\n", "stats.norm(0,1).pdf([-1.3, 0.7, 1.3, 2.6])" ], "outputs": [], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# the value of the repartition functions at each\n", "# integer for the binomial B(10, 0.3)\n", "stats.binom(10, 0.3).cdf(range(11))" ], "outputs": [], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# the 0.9-quantile of the chi2(2)\n", "stats.chi2(2).ppf(0.9)" ], "outputs": [], "metadata": {} }, { "source": [ "Note that as before, the result is a numpy array and not a list. All these functions can be called with a numpy array as argument" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# construct an array of 11 floating point values between 1 and 2\n", "import numpy as np\n", "x = np.linspace(1, 2, 11)\n", "x" ], "outputs": [], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# values of the density of the exponential E(1)\n", "# (note that in scipy exponential starts at 1)\n", "stats.expon(1).pdf(x)" ], "outputs": [], "metadata": {} }, { "source": [ "Using `scipy`, perform some Pearson chi-squared tests for your random generators" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } } }