{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Fundamental Modules for Statistical Modelling\n", "\n", "Feng Li\n", "\n", "School of Statistics and Mathematics\n", "\n", "Central University of Finance and Economics\n", "\n", "[feng.li@cufe.edu.cn](mailto:feng.li@cufe.edu.cn)\n", "\n", "[https://feng.li/python](https://feng.li/python)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Install different version of Python modules \n", "\n", "\n", "- What if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application. Also, what if you can’t install packages into the global site-packages directory, due to not having permissions to change the host python environment?\n", "\n", "- Sometimes it is useful to have different versions of Python modules installed at different places. A quick solution is to install Python packages at current working directory." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://mirrors.163.com/pypi/simple/\n", "Collecting pandas==1.2.1\n", " Using cached https://mirrors.163.com/pypi/packages/c9/56/f415b4148622f469263ad2ece8bdf757972e94ffc97cb750dd8b79b04d43/pandas-1.2.1-cp39-cp39-manylinux1_x86_64.whl (9.7 MB)\n", "Collecting pytz>=2017.3\n", " Using cached https://mirrors.163.com/pypi/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl (510 kB)\n", "Collecting numpy>=1.16.5\n", " Using cached https://mirrors.163.com/pypi/packages/7a/4c/dd00ce768b0f0f7de5c486cbd9f5b922bc3af2f3a5da30121d7f7dc03130/numpy-1.21.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.8 MB)\n", "Collecting python-dateutil>=2.7.3\n", " Using cached https://mirrors.163.com/pypi/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)\n", "Collecting six>=1.5\n", " Using cached https://mirrors.163.com/pypi/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl (10 kB)\n", "Installing collected packages: six, pytz, python-dateutil, numpy, pandas\n", "Successfully installed numpy-1.21.2 pandas-1.2.1 python-dateutil-2.8.1 pytz-2021.1 six-1.15.0\n" ] } ], "source": [ "! pip3 install pandas==1.2.1 -I -t ." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "'1.2.1'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas\n", "pandas.__version__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- In all these cases, [`virtualenv`](https://virtualenv.pypa.io/en/latest/) can also help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python modules for Statistics\n", "\n", "## NumPy\n", "\n", "`NumPy` is short for Numerical Python, is the foundational package for scientific computing in Python. It contains among other things:\n", "\n", "- a powerful N-dimensional array object\n", "- sophisticated (broadcasting) functions\n", "- tools for integrating C/C++ and Fortran code\n", "- useful linear algebra, Fourier transform, and random number capabilities\n", "\n", "Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases. \n", "\n", "- [NumPy Reference](https://numpy.org/doc/stable/reference/)\n", "- [NumPy User Guide](https://numpy.org/doc/stable/user/index.html)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## SciPy \n", "\n", "`SciPy` is a collection of packages addressing a number of different standard problem domains in scientific computing. Here is a sampling of the packages included:\n", "\n", "- `scipy.integrate` : numerical integration routines and differential equation solvers.\n", "- `scipy.linalg` : linear algebra routines and matrix decompositions extending beyond those provided in `numpy.linalg`.\n", "- `scipy.optimize` : function optimizers (minimizers) and root finding algorithms." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- `scipy.signal` : signal processing tools.\n", "- `scipy.sparse` : sparse matrices and sparse linear system solvers.\n", "- `scipy.special` : wrapper around SPECFUN, a Fortran library implementing many common mathematical functions, such as the gamma function.\n", "- `scipy.stats` : standard continuous and discrete probability distributions (density functions, samplers, continuous distribution functions), various statistical tests, and more descriptive statistics.\n", "- ... \n", "\n", "[SciPy Reference Guide](https://docs.scipy.org/doc/scipy/)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Linear Algebra\n", "\n", "Linear algebra can be done conveniently via `scipy.linalg`. When SciPy is built using the optimized ATLAS LAPACK and BLAS libraries, it has very fast linear algebra capabilities. If you dig deep enough, all of the raw lapack and blas libraries are available for your use for even more speed. In this section, some easier-to-use interfaces to these routines are described.\n", "\n", "All of these linear algebra routines expect an object that can be converted into a 2-dimensional array. The output of these routines is also a two-dimensional array." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Matrices and n-dimensional array" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from scipy import linalg\n", "A = np.array([[1,2],[3,4]])\n", "A" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[-2. , 1. ],\n", " [ 1.5, -0.5]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linalg.inv(A) # inverse of a matrix" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[5, 6]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[5,6]]) #2D array\n", "b" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[5],\n", " [6]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.T" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 12],\n", " [15, 24]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A*b #not matrix multiplication!" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[17],\n", " [39]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(b.T) #matrix multiplication" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([5, 6])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([5,6]) #1D array\n", "b" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([5, 6])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.T #not matrix transpose!" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([17, 39])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(b) #does not matter for multiplication" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Numpy array can easily convet to Pandas, and vise versa." ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
0123
1456
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 1 2 3\n", "1 4 5 6" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "Ap = pd.DataFrame(A)\n", "Ap" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2 = Ap.values\n", "A2" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(A2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Solving linear system¶" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from scipy import linalg\n", "A = np.array([[1,2],[3,4]])\n", "A" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[5],\n", " [6]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[5],[6]])\n", "b" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([-4. , 4.5])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linalg.inv(A).dot(b) #slow" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.],\n", " [0.]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(linalg.inv(A).dot(b))-b #check" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[-4. ],\n", " [ 4.5]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.solve(A,b) #fast" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.],\n", " [0.]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(np.linalg.solve(A,b))-b #check" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Determinant" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "-2.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from scipy import linalg\n", "A = np.array([[1,2],[3,4]])\n", "linalg.det(A)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Least-squares problems and pseudo-inverses" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import numpy as np\n", "from scipy import linalg\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "c1, c2 = 5.0, 2.0\n", "i = np.r_[1:11]\n", "xi = 0.1*i\n", "yi = c1*np.exp(-xi) + c2*xi\n", "zi = yi + 0.05 * np.max(yi) * np.random.randn(len(yi))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "A = np.c_[np.exp(-xi)[:, np.newaxis], xi[:, np.newaxis]]\n", "c, resid, rank, sigma = linalg.lstsq(A, zi)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "xi2 = np.r_[0.1:1.0:100j]\n", "yi2 = c[0]*np.exp(-xi2) + c[1]*xi2" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEYCAYAAABMVQ1yAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAiYElEQVR4nO3deZgU5bn+8e8z7AKCwrDoCCgguIHoCIoLiDsajUfy0yBxS0JQiSbGLCa/Y4zJyXZlJSSOuMQYxCWKxgW3HBUXBGVHRRIGAUdRGJVNEIR5zh9vDTTDzNADPV3d1ffnuurq7qrq7qd64O7qt956y9wdERHJf0VxFyAiIpmhQBcRSQgFuohIQijQRUQSQoEuIpIQCnQRkYRQoEtGmdnxZvYfM1tvZl80syfN7NIGvsabZja0cSpM6/1/aGa317P8MjN7uQGvt9TMTk3ntRtYp5tZr9143lAzq8hEDZJbmsZdgGSOmS0FOgNbgK3AW8DdwAR3r0rj+T2Ad4Bm7r5lN8u4GRjv7n+MHj+S8vqXAV9z9xNS5t0FVLj7/6+e5+6H7eZ7Z4S7/7z6foY+k1pfO9eZ2U1AL3cfFXctkh7toSfPF9y9LdAd+CXwfeCOLL5/d+DNLL6fiEQU6Anl7mvc/VHgQuBSMzscwMzONrM5ZrbWzN6N9sKqvRjdro6aTI4zs55m9pyZfWR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lu841AB+Av0R7rFs+xUdsaIs1tTox0ttfdF5rZU8B8oAq43XccoTSvpPk3/ilwl5ktIDRFfN/d83pIXTO7FxgKdDSzCuDHhGtC5GR+6dR/EZGE0JmiIiIJoUAXEUkIBbqISEIo0EVEEkKBLiKSEAp0EZGEUKCLRMzseTM7Lbr/MzMbF3dNIg3R2Kf+i+STHwM3m1knYABwbsz1iDSI9tBFItFQqQZcB1zk7lsBzOynsRYmkiYFukjEzI4AugKb3H1dNK8L+iUreUKBLgJEFyW4h3DBgk/N7Ixo0QDCkLAiOU+BLgUvGg1xMvAdd19IGGTqpmjxkSjQJU9ocC6RepjZHcDXC+DKO5IACnQRkYRQk4uISEIo0EVEEkKBLiKSEAp0EZGEUKCLiCSEAl1EJCEU6CIiCaFAFxFJCAW6iEhC/B8mEtaZ4kDI+wAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "plt.plot(xi,zi,'x',xi2,yi2)\n", "plt.axis([0,1.1,3.0,5.5])\n", "plt.xlabel('$x_i$') # LaTeX symbols are also supported\n", "plt.title('Data fitting with linalg.lstsq')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Eigenvalues and eigenvectors" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-0.3722813232690143+0j) (5.372281323269014+0j)\n", "[-0.82456484 0.56576746]\n", "[-0.41597356 -0.90937671]\n", "[1. 1.]\n", "5.551115123125783e-17\n" ] } ], "source": [ "import numpy as np\n", "from scipy import linalg\n", "A = np.array([[1,2],[3,4]])\n", "la,v = linalg.eig(A)\n", "l1,l2 = la\n", "print(l1, l2) #eigenvalues\n", "\n", "print(v[:,0]) #first eigenvector\n", "\n", "print(v[:,1]) #second eigenvector\n", "\n", "print(np.sum(abs(v**2),axis=0)) #eigenvectors are unitary\n", "\n", "v1 = np.array(v[:,0]).T\n", "print(linalg.norm(A.dot(v1)-l1*v1)) #check the computation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Singular Value Decomposition (SVD)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import numpy as np\n", "from scipy import linalg\n", "A = np.array([[1,2,3],[4,5,6]])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "M,N = A.shape\n", "U,s,Vh = linalg.svd(A)\n", "Sig = linalg.diagsvd(s,M,N)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[-0.3863177 , 0.92236578],\n", " [-0.92236578, -0.3863177 ]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "U, Vh = U, Vh\n", "U" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "scrolled": true, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[9.508032 , 0. , 0. ],\n", " [0. , 0.77286964, 0. ]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sig" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[-0.42866713, -0.56630692, -0.7039467 ],\n", " [-0.80596391, -0.11238241, 0.58119908],\n", " [ 0.40824829, -0.81649658, 0.40824829]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vh" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "scrolled": true, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1., 2., 3.],\n", " [4., 5., 6.]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "U.dot(Sig.dot(Vh)) #check computation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## QR decomposition\n", "\n", "The command for QR decomposition is `linalg.qr`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## LU decomposition\n", " \n", "The SciPy command for this decomposition is `linalg.lu`. If the intent for performing LU decomposition is for solving linear systems then the command `linalg.lu_factor` should be used followed by repeated applications of the command `linalg.lu_solve` to solve the system for each new right-hand-side." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Cholesky decomposition\n", "\n", "The command `linalg.cholesky` computes the cholesky factorization. For using Cholesky factorization to solve systems of equations there are also `linalg.cho_factor` and `linalg.cho_solve` routines that work similarly to their LU decomposition counterparts." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Statistical Distributions\n", "\n", "A large number of probability distributions as well as a growing library of statistical functions are available in `scipy.stats`. See http://docs.scipy.org/doc/scipy/reference/stats.html for a complete list." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Generate random numbers from normal distribution:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "from scipy.stats import norm\n", "r = norm.rvs(loc=0, scale=1, size=1000)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Calculate a few first moments:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "mean, var, skew, kurt = norm.stats(moments='mvsk')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Linear regression model\n", "\n", "This example computes a least-squares regression for two sets of measurements." ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "scrolled": true, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'slope': 0.0016389519609387667, 'intercept': 0.5367248508312181}\n", "{'p_value': 0.9950296225204127, 'r-squared': 0.0}\n" ] } ], "source": [ "from scipy import stats\n", "import numpy as np\n", "x = np.random.random(10)\n", "y = np.random.random(10)\n", "slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)\n", "print({'slope':slope,'intercept':intercept})\n", "print({'p_value':p_value,'r-squared':round(r_value**2,2)})" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Optimization\n", "\n", "The `minimize` function provides a common interface to unconstrained and constrained minimization algorithms for multivariate scalar functions in `scipy.optimize`" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import numpy as np\n", "from scipy.optimize import minimize\n", "\n", "def rosen(x):\n", " \"\"\"The Rosenbrock function\"\"\"\n", " return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully.\n", " Current function value: 0.000000\n", " Iterations: 339\n", " Function evaluations: 571\n", "[1. 1. 1. 1. 1.]\n" ] } ], "source": [ "x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])\n", "\n", "## Calling the minimize() function\n", "res = minimize(rosen, x0, method='nelder-mead',\n", " options={'xtol': 1e-8, 'disp': True})\n", "print(res.x)" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" }, "rise": { "auto_select": "first", "autolaunch": false, "enable_chalkboard": true, "start_slideshow_at": "selected", "theme": "black" } }, "nbformat": 4, "nbformat_minor": 1 }