From a70a7a138dd34532ca4374c25cbe7df6a559ad2f Mon Sep 17 00:00:00 2001 From: Taro Yabuki Date: Mon, 3 Jun 2024 11:55:33 +0900 Subject: [PATCH] =?UTF-8?q?Python=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/python/python-binder.ipynb | 2 +- code/python/python.ipynb | 2812 +++++++++---------------------- 2 files changed, 791 insertions(+), 2023 deletions(-) diff --git a/code/python/python-binder.ipynb b/code/python/python-binder.ipynb index 42c8d6f..ca15ed7 100644 --- a/code/python/python-binder.ipynb +++ b/code/python/python-binder.ipynb @@ -2229,7 +2229,7 @@ "metadata": {}, "outputs": [], "source": [ - "n = 15; p0 = 4 / 10; binom_test(2, 15, 4 / 10)" + "n = 15; p0 = 4 / 10; binom_test(2, n, p0)" ] }, { diff --git a/code/python/python.ipynb b/code/python/python.ipynb index 32c7363..81bcfd3 100644 --- a/code/python/python.ipynb +++ b/code/python/python.ipynb @@ -2,44 +2,27 @@ "cells": [ { "cell_type": "markdown", - "source": [ - "[矢吹太朗『コンピュータでとく数学』(オーム社, 2024)](https://github.com/taroyabuki/comath)\n", - "\n", - "\n" - ], - "metadata": { - "id": "fvi1PMzkKZyC" - }, - "id": "fvi1PMzkKZyC" + "source": "[矢吹太朗『コンピュータでとく数学』(オーム社, 2024)](https://github.com/taroyabuki/comath)\n\n\n", + "metadata": {} }, { "cell_type": "markdown", - "source": [ - "# 1 実行環境" - ], - "metadata": { - "id": "P9kjMgzSKZyE" - }, - "id": "P9kjMgzSKZyE" + "source": "# 1 実行環境", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XcO57SowKZyE" - }, + "metadata": {}, "outputs": [], "source": [ "!python -m pip install see" - ], - "id": "XcO57SowKZyE" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "maWqO2TTKZyE" - }, + "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", @@ -60,41 +43,32 @@ "from sympy import *\n", "from sympy.stats import *\n", "from sympy.plotting import plot3d" - ], - "id": "maWqO2TTKZyE" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "EoefwurOKZyF" - }, + "metadata": {}, "outputs": [], "source": [ "np.sqrt(2)" - ], - "id": "EoefwurOKZyF" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "pO2_0OioKZyF" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({'x1': [1, 3, 6, 10], 'y': [7, 1, 6, 14]})\n", "model = smf.ols('y ~ x1', data).fit()\n", "model.params" - ], - "id": "pO2_0OioKZyF" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "uIA-b2zjKZyF" - }, + "metadata": {}, "outputs": [], "source": [ "from see import see\n", @@ -103,728 +77,550 @@ "# .remove_data() .resid .resid_pearson .rsquared\n", "# .rsquared_adj .save() .scale .ssr\n", "# .summary() .summary2() .t_test()" - ], - "id": "uIA-b2zjKZyF" + ] }, { "cell_type": "markdown", - "source": [ - "# 2 数と変数" - ], - "metadata": { - "id": "AOoyW0PEKZyF" - }, - "id": "AOoyW0PEKZyF" + "source": "# 2 数と変数", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_paLAyKZKZyF" - }, + "metadata": {}, "outputs": [], "source": [ "2 * (-3)" - ], - "id": "_paLAyKZKZyF" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "QGlGft7UKZyF" - }, + "metadata": {}, "outputs": [], "source": [ "(1 + 2) * 3" - ], - "id": "QGlGft7UKZyF" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "m5Fvo2f0KZyG" - }, + "metadata": {}, "outputs": [], "source": [ "2**10" - ], - "id": "m5Fvo2f0KZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "iaW_RXGBKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "-2 < -1" - ], - "id": "iaW_RXGBKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DVNK3V5tKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "2 + 2 == 5" - ], - "id": "DVNK3V5tKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2CbX6QGuKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "10 if (7 < 5) else 20" - ], - "id": "2CbX6QGuKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qStQstqBKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "var('x') # xが変数であることの宣言\n", "x < 1" - ], - "id": "qStQstqBKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sFHk2PbiKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "var('x y')\n", "Eq(x, y)" - ], - "id": "sFHk2PbiKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "O2a_Ov5uKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "simplify(Eq((x**2 - 1), (x + 1) * (x - 1)))" - ], - "id": "O2a_Ov5uKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "p-xo1AatKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "not(0<1)" - ], - "id": "p-xo1AatKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AQWwPLzzKZyG" - }, + "metadata": {}, "outputs": [], "source": [ "(0<1)or(2>3)" - ], - "id": "AQWwPLzzKZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "o7jxMNG8KZyG" - }, + "metadata": {}, "outputs": [], "source": [ "(0 < 1) and (2 > 3)" - ], - "id": "o7jxMNG8KZyG" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "bmNcc-oiKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "Not(10 < x)" - ], - "id": "bmNcc-oiKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ppXslHp1KZyH" - }, + "metadata": {}, "outputs": [], "source": [ "x = 5; x == 5" - ], - "id": "ppXslHp1KZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JlTsjbXVKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "a = 1 + 2\n", "b = 9\n", "a * (b + 1)" - ], - "id": "JlTsjbXVKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "r7tsTt7mKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "a = 1 + 2; b = 9; a * (b + 1)" - ], - "id": "r7tsTt7mKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XTxVnysFKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "a = 3\n", "var('a') # 変数を記号にする.\n", "expand((a + 1)**2)" - ], - "id": "XTxVnysFKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_wg0yIuRKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "import keyword\n", "keyword.kwlist" - ], - "id": "_wg0yIuRKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "HJSQSDOjKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "x1 = 2; x2 = 3; x1 + x2" - ], - "id": "HJSQSDOjKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ohe7_kBLKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "x = 1; y = x + 1; x = 2; y" - ], - "id": "Ohe7_kBLKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sSNmg_UMKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "f = 2 * x + 3\n", "f.subs(x, 5)" - ], - "id": "sSNmg_UMKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "eNC0u73ZKZyH" - }, + "metadata": {}, "outputs": [], "source": [ "var('a b x y')\n", "g = a + b\n", "g.subs(((a, x), (b, y)))" - ], - "id": "eNC0u73ZKZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AyI-XQL5KZyH" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: 2 * x + 3\n", "f(5)" - ], - "id": "AyI-XQL5KZyH" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Wdyg8s4EKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "def f(x): return 2 * x + 3\n", "f(5)" - ], - "id": "Wdyg8s4EKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "3_IALsvVKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: 2 * x + 3; var('a')\n", "g = f(a)\n", "f(5), g.subs(a, 5)" - ], - "id": "3_IALsvVKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "FlkQYzqRKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: 1 / x\n", "f(1)" - ], - "id": "FlkQYzqRKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1hQWUrL4KZyI" - }, + "metadata": {}, "outputs": [], "source": [ "(lambda x: 2 * x + 3)(5)" - ], - "id": "1hQWUrL4KZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DgaVof1TKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: x + y\n", "f(2, 3)" - ], - "id": "DgaVof1TKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DKuA05qtKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "g = lambda x: x[0] + x[1]\n", "x = (2, 3); g(x)" - ], - "id": "DKuA05qtKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "t_FLtrzPKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "f(*x)" - ], - "id": "t_FLtrzPKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "noqeps2vKZyI" - }, + "metadata": {}, "outputs": [], "source": [ "g((2, 3))" - ], - "id": "noqeps2vKZyI" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "aawPjPTlKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "expand((x + 1)**2)" - ], - "id": "aawPjPTlKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "H8WBdfvSKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "N(sqrt(2), 30)" - ], - "id": "H8WBdfvSKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "OwS-kXRNKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "import struct\n", "tmp = bin(int(struct.pack('>d', 0.1).hex(), 16))[2:].zfill(64) # ビット列\n", "s, e, f = tmp[0], tmp[1:12], tmp[12:64] # 1, 11, 52桁に分ける.\n", "s, e, f" - ], - "id": "OwS-kXRNKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "bnB7dO9MKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "s, e, f = int(s), sym.S(int(e, 2)), sym.S(int(f, 2)) # 数値に変換する.\n", "(-1)**s * (1 + f / 2**52) * 2**(e - 1023)" - ], - "id": "bnB7dO9MKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "yzF7kXLDKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "import sympy\n", "sympy.sqrt(5)**2 == 5 # True" - ], - "id": "yzF7kXLDKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "x8UdbShkKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "import math\n", "import numpy\n", "numpy.sqrt(5)**2 == 5, math.sqrt(5)**2 == 5 # いずれもFalse" - ], - "id": "x8UdbShkKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Bb23yp6CKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "0.1 + 0.2 == 0.3" - ], - "id": "Bb23yp6CKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "7G0AACu-KZyM" - }, + "metadata": {}, "outputs": [], "source": [ "np.isclose(0.1 + 0.2, 0.3)" - ], - "id": "7G0AACu-KZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "yQoyDNalKZyM" - }, + "metadata": {}, "outputs": [], "source": [ "sym.S(1) / 10 + sym.S(2) / 10 == sym.S(3) / 10" - ], - "id": "yQoyDNalKZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GtGvtRP7KZyM" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "simplify(sin(x)**2 + cos(x)**2)" - ], - "id": "GtGvtRP7KZyM" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "4lBADKuCKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "sqrtdenest(sqrt(5 + 2 * sqrt(6)))" - ], - "id": "4lBADKuCKZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "aLXNVg-oKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "refine(sqrt((x - 1)**2), Q.nonnegative(x - 1))" - ], - "id": "aLXNVg-oKZyN" + ] }, { "cell_type": "markdown", - "source": [ - "# 3 データ構造" - ], - "metadata": { - "id": "jkdszYWgKZyN" - }, - "id": "jkdszYWgKZyN" + "source": "# 3 データ構造", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "c_YyoJF7KZyN" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, 3, 5]; len(v)" - ], - "id": "c_YyoJF7KZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "seJzxF8AKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "v[2] = 0.5; v" - ], - "id": "seJzxF8AKZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "LC7f18zMKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "np.arange(5)" - ], - "id": "LC7f18zMKZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "szL1cqjlKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "np.arange(0, 1.01, 0.1) # 終点(ここでは1)より大きい値を指定する." - ], - "id": "szL1cqjlKZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9Ysh2BuTKZyN" - }, + "metadata": {}, "outputs": [], "source": [ "np.linspace(0, 100, 5)" - ], - "id": "9Ysh2BuTKZyN" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sZMHb_spKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = np.array([2, 3])\n", "1.1 * v" - ], - "id": "sZMHb_spKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XMQQrEwGKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "u = np.array([10, 20]); v = np.array([2, 3])\n", "u + v" - ], - "id": "XMQQrEwGKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "dpvR1-PWKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v + 1" - ], - "id": "dpvR1-PWKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1qNIwzW8KZyO" - }, + "metadata": {}, "outputs": [], "source": [ "u = np.array([10, 20]); v = np.array([2, 3])\n", "u.dot(v)" - ], - "id": "1qNIwzW8KZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "LI-E3b9RKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "a = [2, 3, 4]; b = a.copy(); b[2] = 0.5; a" - ], - "id": "LI-E3b9RKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZbsymWxWKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, -1, 3, -2]\n", "[x for x in v if x > 0] # 内包表記" - ], - "id": "ZbsymWxWKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "18xo7h37KZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, -1, 3, -2]\n", "np.heaviside(v, 0)" - ], - "id": "18xo7h37KZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "eNYeLhJAKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, -1, 3, -2]\n", @@ -832,107 +628,83 @@ "u = [None] * n # Noneは「値がない」ということ.\n", "for i in range(n): u[i] = 0 if v[i] < 0 else 1\n", "u" - ], - "id": "eNYeLhJAKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gQD20L2SKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, -1, 3, -2]\n", "[(0 if x < 0 else 1) for x in v]" - ], - "id": "gQD20L2SKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "IYAWXdZvKZyO" - }, + "metadata": {}, "outputs": [], "source": [ "v = [2, -1, 3, -2]\n", "f = lambda x: 0 if x < 0 else 1\n", "[f(x) for x in v]" - ], - "id": "IYAWXdZvKZyO" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UQkXAcOSKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "f = np.vectorize(lambda x: 0 if x < 0 else 1)\n", "f(v)" - ], - "id": "UQkXAcOSKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KNS6q9EAKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "u = [1, 7, 2, 9]; v = [2, 3, 5, 7]\n", "[(-1 if a < b else 1) for a, b in zip(u, v)]" - ], - "id": "KNS6q9EAKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "xNEPHVyBKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "x = {\"apple\" : \"りんご\", \"orange\" : \"みかん\"}\n", "x[\"orange\"]" - ], - "id": "xNEPHVyBKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "MxOM_NCvKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "x[\"grape\"] = \"ぶどう\"\n", "x[\"grape\"]" - ], - "id": "MxOM_NCvKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "d4q93R2BKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "x.pop(\"apple\")\n", "\"apple\" in x" - ], - "id": "d4q93R2BKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "TER1bvK0KZyP" - }, + "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame({'name': ['A', 'B', 'C'],\n", @@ -940,573 +712,425 @@ " 'math': [70, 80, 90],\n", " 'gender': ['f', 'm', 'm']})\n", "df" - ], - "id": "TER1bvK0KZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "bWz6UHIXKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(({'name': 'A', 'english': 60, 'math': 70, 'gender': 'f'},\n", " {'name': 'B', 'english': 90, 'math': 80, 'gender': 'm'},\n", " {'name': 'C', 'english': 70, 'math': 90, 'gender': 'm'}))\n", "df" - ], - "id": "bWz6UHIXKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "y1yU8qQrKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "df[['english', 'math']]" - ], - "id": "y1yU8qQrKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9_fb1lOWKZyP" - }, + "metadata": {}, "outputs": [], "source": [ "df[\"english\"]" - ], - "id": "9_fb1lOWKZyP" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "VJt0rib2KZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "df.english" - ], - "id": "VJt0rib2KZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "4mrdhtAVKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "m = df.iloc[:, [1, 2]].values; m" - ], - "id": "4mrdhtAVKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-IfIBNSgKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "english, math = m.T; english, math" - ], - "id": "-IfIBNSgKZyQ" + ] }, { "cell_type": "markdown", - "source": [ - "# 4 可視化と方程式" - ], - "metadata": { - "id": "04aQZWgNKZyQ" - }, - "id": "04aQZWgNKZyQ" + "source": "# 4 可視化と方程式", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fWLpu53UKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "plot(x**2 + 2 * x - 4, (x, -5, 3));" - ], - "id": "fWLpu53UKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DShPoHzmKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-5, 3, 101)\n", "y = x**2 + 2 * x - 4\n", "plt.plot(x, y);" - ], - "id": "DShPoHzmKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZKKi6gOXKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "var('x y')\n", "plot3d(x**2 + y**2, (x, -1, 1), (y, -1, 1));" - ], - "id": "ZKKi6gOXKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gNDgLseaKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "plotting.plot_contour(x**2 + y**2, (x, -1, 1), (y, -1, 1));" - ], - "id": "gNDgLseaKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2r9B6bxnKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "plot_implicit(Eq(x**2 + y**2, 1));" - ], - "id": "2r9B6bxnKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "HAaHcm1EKZyQ" - }, + "metadata": {}, "outputs": [], "source": [ "plot_implicit(x**2 + y**2 <= 1);" - ], - "id": "HAaHcm1EKZyQ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1VRGTRfsKZyR" - }, + "metadata": {}, "outputs": [], "source": [ "plot_implicit(Or(Eq(2 * x + 3 * y, 8), Eq(5 * x - 7 * y, -9)));" - ], - "id": "1VRGTRfsKZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KPNtbCtEKZyR" - }, + "metadata": {}, "outputs": [], "source": [ "plot_implicit(And(y <= x, y >= x**2), (x, 0, 2), (y, 0, 2));" - ], - "id": "KPNtbCtEKZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fLLXtOj3KZyR" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "a, b = sorted(solve(Eq(x, x**2), x))\n", "integrate(x - x**2, (x, a, b))" - ], - "id": "fLLXtOj3KZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "P77NbxG3KZyR" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "solve(Eq(x**2 + 2 * x - 4, 0), x)" - ], - "id": "P77NbxG3KZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "K4lwx3VlKZyR" - }, + "metadata": {}, "outputs": [], "source": [ "a, b = solve(x**2 + 2 * x - 4, x)\n", "a + b" - ], - "id": "K4lwx3VlKZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "YhwwxPltKZyR" - }, + "metadata": {}, "outputs": [], "source": [ "n = 3; simplify(sum(solve(x**n + 2 * x - 4, x)))" - ], - "id": "YhwwxPltKZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_kMTMt_YKZyR" - }, + "metadata": {}, "outputs": [], "source": [ "var('x y')\n", "sol = solve([Eq(2 * x + 3 * y, 8), Eq(5 * x - 7 * y, -9)], [x, y]); sol" - ], - "id": "_kMTMt_YKZyR" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "zuTqji7TKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "x1, y1 = sol.values()\n", "x1 + y1" - ], - "id": "zuTqji7TKZyT" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "VqGjh6KpKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); f = 2**x + sin(x)\n", "nsolve(f, x, 0)" - ], - "id": "VqGjh6KpKZyT" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "OKMBkt2qKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "solve(Lt(x**2 + 2 * x - 4, 0), x)" - ], - "id": "OKMBkt2qKZyT" + ] }, { "cell_type": "markdown", - "source": [ - "# 5 論理式\n", - "\n", - "\n" - ], - "metadata": { - "id": "KnECcfSZKZyT" - }, - "id": "KnECcfSZKZyT" + "source": "# 5 論理式\n\n\n", + "metadata": {} }, { "cell_type": "markdown", - "source": [ - "# 6 1次元のデータ" - ], - "metadata": { - "id": "1Kpz7dEVKZyT" - }, - "id": "1Kpz7dEVKZyT" + "source": "# 6 1次元のデータ", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "r7Y3--kQKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "a = pd.Series([36, 43, 53, 55, 56, 56, 57, 60, 61, 73])\n", "b = pd.Series([34, 39, 39, 49, 50, 52, 52, 55, 83, 97])\n", "a.hist();" - ], - "id": "r7Y3--kQKZyT" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "6iy_DBGNKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "a.value_counts(bins=np.arange(20, 81, 20), sort=False)" - ], - "id": "6iy_DBGNKZyT" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "uCYXjYbBKZyT" - }, + "metadata": {}, "outputs": [], "source": [ "x = [7, 3, 1, 3, 4, 7, 7, 7, 10, 3]\n", "f = Counter(x); f" - ], - "id": "uCYXjYbBKZyT" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "5eRUJkruKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "pd.DataFrame({'A':a, 'B':b}).boxplot();" - ], - "id": "5eRUJkruKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fWOpbdK3KZyU" - }, + "metadata": {}, "outputs": [], "source": [ "a = pd.Series([36, 43, 53, 55, 56, 56, 57, 60, 61, 73])\n", "a.mean()" - ], - "id": "fWOpbdK3KZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "8cDDDOlCKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "b = pd.Series([34, 39, 39, 49, 50, 52, 52, 55, 83, 97])\n", "sum(b) / len(b), b.sum() / b.count() # 二つの方法" - ], - "id": "8cDDDOlCKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DkttplGYKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "(a - a.mean()).mean()" - ], - "id": "DkttplGYKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "OSmrelStKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "a.var(ddof=1)" - ], - "id": "OSmrelStKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "J-gfglE3KZyU" - }, + "metadata": {}, "outputs": [], "source": [ "sum((b - b.mean())**2) / (len(b) - 1)" - ], - "id": "J-gfglE3KZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-sojXyZoKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "z = stats.zscore(a, ddof=1); z" - ], - "id": "-sojXyZoKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "a5J4DDK2KZyU" - }, + "metadata": {}, "outputs": [], "source": [ "z.mean(), z.std(ddof=1)" - ], - "id": "a5J4DDK2KZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "VtgtIsmWKZyU" - }, + "metadata": {}, "outputs": [], "source": [ "(a - a.mean()) / a.std(ddof=1)" - ], - "id": "VtgtIsmWKZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "B-f7anH5KZyU" - }, + "metadata": {}, "outputs": [], "source": [ "a.std(ddof=1) * z + a.mean()" - ], - "id": "B-f7anH5KZyU" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KOpkhB_OKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "10 * z + 50" - ], - "id": "KOpkhB_OKZyV" + ] }, { "cell_type": "markdown", - "source": [ - "# 7 2次元のデータ" - ], - "metadata": { - "id": "I9AA2Ja7KZyV" - }, - "id": "I9AA2Ja7KZyV" + "source": "# 7 2次元のデータ", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "wPFvajS8KZyV" - }, + "metadata": {}, "outputs": [], "source": [ "x = pd.Series([35, 45, 55, 65, 75])\n", "y = pd.Series([114, 124, 143, 158, 166])\n", "plt.scatter(x, y);" - ], - "id": "wPFvajS8KZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "R-xl8FtUKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "x = pd.Series([35, 45, 55, 65, 75])\n", "y = pd.Series([114, 124, 143, 158, 166])\n", "x.cov(y, ddof=1), np.cov(x, y, ddof=1)[0, 1] # 二つの方法" - ], - "id": "R-xl8FtUKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "5gW3tJd_KZyV" - }, + "metadata": {}, "outputs": [], "source": [ "np.cov(x, y, ddof=1)" - ], - "id": "5gW3tJd_KZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "05_1J4bKKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "(x - x.mean()) @ (y - y.mean()) / (len(x) - 1)" - ], - "id": "05_1J4bKKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "pHNo9XZoKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "x.corr(y), np.corrcoef(x, y)[0, 1] # 二つの方法" - ], - "id": "pHNo9XZoKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Hkz7WCnaKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "x = pd.Series([35, 45, 55, 65, 75])\n", @@ -1514,80 +1138,62 @@ "data = pd.DataFrame({'x': x, 'y': y})\n", "model = smf.ols('y ~ x', data).fit()\n", "model.params" - ], - "id": "Hkz7WCnaKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "a_DGviJIKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "model.predict({'x': 40})" - ], - "id": "a_DGviJIKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "v46stDXKKZyV" - }, + "metadata": {}, "outputs": [], "source": [ "sns.regplot(x=x, y=y, ci=None)" - ], - "id": "v46stDXKKZyV" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0OIW88xhKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "var('a b'); L = sum((y - (a * x + b))**2); L" - ], - "id": "0OIW88xhKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "QFIDEZCtKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "vars = var('p q r s t u')\n", "sol = solve(Eq(L, p * (a - q)**2 + r * (b - (s* a + t))**2 + u), vars)\n", "print(sol)\n", "q.subs(sol), (s * q + t).subs(sol)" - ], - "id": "QFIDEZCtKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "VNAXAVESKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "a = x.cov(y, ddof=1) / x.var(ddof=1)\n", "b = y.mean() - a * x.mean()\n", "a, b" - ], - "id": "VNAXAVESKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "nREnHSZvKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "anscombe = sns.load_dataset('anscombe')\n", @@ -1595,447 +1201,343 @@ "print(data.x.corr(data.y))\n", "model = smf.ols('y ~ x', data).fit(); print(model.params)\n", "sns.regplot(x=data.x, y=data.y, ci=None);" - ], - "id": "nREnHSZvKZyW" + ] }, { "cell_type": "markdown", - "source": [ - "# 8 確率変数と確率分布" - ], - "metadata": { - "id": "B01Z0HbdKZyW" - }, - "id": "B01Z0HbdKZyW" + "source": "# 8 確率変数と確率分布", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ps062iIzKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "X = DiscreteUniform('X', range(1, 7)) # X = Die('X', 6)でもよい.\n", "density(X)(2)" - ], - "id": "Ps062iIzKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lg6zt1DxKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.randint(1, 7)\n", "rv.pmf(2)" - ], - "id": "lg6zt1DxKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "wJ3NH3v_KZyW" - }, + "metadata": {}, "outputs": [], "source": [ "P(Eq(X, 2)) # P(X == 2)ではない." - ], - "id": "wJ3NH3v_KZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "J0-P9yeTKZyW" - }, + "metadata": {}, "outputs": [], "source": [ "data = list(sample_iter(X, numsamples=1000))\n", "plt.hist(data); # 結果は割愛" - ], - "id": "J0-P9yeTKZyW" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "YL8L9-DQKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "data = rv.rvs(size=1000)\n", "plt.hist(data); # 結果は割愛" - ], - "id": "YL8L9-DQKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XXJdOrLmKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "x = range(1, 7); y = [density(X)(x) for x in x]\n", "_, ax = plt.subplots() # 結果のうち,使わない部分を_とする.\n", "ax.hist(data, bins=np.arange(0.5, 7, 1), density=True, alpha=0.3)\n", "ax.scatter(x, y);" - ], - "id": "XXJdOrLmKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "kXSpMymvKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "x = range(1, 7); y = rv.pmf(x)\n", "_, ax = plt.subplots() # 結果のうち,使わない部分を_とする.\n", "ax.hist(data, bins=np.arange(0.5, 7, 1), density=True, alpha=0.3)\n", "ax.scatter(x, y);" - ], - "id": "kXSpMymvKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DqbDzZ-GKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "X = Bernoulli('X', p=sym.S(3) / 10)\n", "data = list(sample_iter(X, numsamples=1000))\n", "np.bincount(data), Counter(data) # 二つの方法" - ], - "id": "DqbDzZ-GKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "LMBA20f-KZyX" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.bernoulli(3 / 10)\n", "data = rv.rvs(1000)\n", "np.bincount(data), Counter(data) # 二つの方法" - ], - "id": "LMBA20f-KZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GgKEg835KZyX" - }, + "metadata": {}, "outputs": [], "source": [ "X = Binomial('X', 10, sym.S(3) / 10)\n", "density(X)(3)" - ], - "id": "GgKEg835KZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "oG2GW5PUKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.binom(10, 3 / 10)\n", "rv.pmf(3)" - ], - "id": "oG2GW5PUKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "wtJw6MyOKZyX" - }, + "metadata": {}, "outputs": [], "source": [ "P(Eq(X, 3)) # P(X == 3)ではない." - ], - "id": "wtJw6MyOKZyX" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KTbJa-xYKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "var('n p x')\n", "X = Binomial('X', n, p)\n", "density(X)(x)" - ], - "id": "KTbJa-xYKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DVd3kEdGKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "n = 10; p = sym.S(3) / 10; X = Binomial('X', n, p)\n", "data = list(sample_iter(X, numsamples=1000))\n", "plt.hist(data); # 結果は割愛" - ], - "id": "DVd3kEdGKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ntss0NbdKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "n = 10; p = 3 / 10; rv = stats.binom(n, p)\n", "data = rv.rvs(1000)\n", "plt.hist(data); # 結果は割愛" - ], - "id": "ntss0NbdKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ep7IZOgnKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "x = range(0, n + 1); y = [density(X)(x) for x in x]\n", "_, ax = plt.subplots()\n", "ax.hist(data, bins=np.arange(-0.5, n + 1, 1), density=True, alpha=0.5)\n", "ax.scatter(x, y);" - ], - "id": "Ep7IZOgnKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "8l9KDBUAKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "x = range(0, n + 1); y = rv.pmf(x)\n", "_, ax = plt.subplots()\n", "ax.hist(data, bins=np.arange(-0.5, n + 1, 1), density=True, alpha=0.5)\n", "ax.scatter(x, y);" - ], - "id": "8l9KDBUAKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_n7LcXqIKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "X = Binomial('X', 10, S(3) / 10)\n", "cdf(X)[3] # cdf(X)(x)ではない." - ], - "id": "_n7LcXqIKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "a1nONgs_KZyY" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.binom(10, 3 / 10)\n", "rv.cdf(3)" - ], - "id": "a1nONgs_KZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "FM6qE6cHKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "P(X <= 3)" - ], - "id": "FM6qE6cHKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "hqXMukAWKZyY" - }, + "metadata": {}, "outputs": [], "source": [ "sum([density(X)(k) for k in range(4)])" - ], - "id": "hqXMukAWKZyY" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZibbPO0hKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "sum([rv.pmf(k) for k in range(4)])" - ], - "id": "ZibbPO0hKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "jdTxT5xMKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "X = Uniform('X', 0, 360)\n", "cdf(X)(200), cdf(X)(150), cdf(X)(200) - cdf(X)(150)" - ], - "id": "jdTxT5xMKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fIGIXrAxKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.uniform(0, 360)\n", "rv.cdf(200), rv.cdf(150), rv.cdf(200) - rv.cdf(150)" - ], - "id": "fIGIXrAxKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_T6ttqshKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "# P(150 <= X <= 200)やP(150 <= X and X <= 200)ではない.\n", "P(And(150 <= X, X <= 200))" - ], - "id": "_T6ttqshKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "i4-0yJ1LKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); integrate(density(X)(x), (x, 150, 200))" - ], - "id": "i4-0yJ1LKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "DL2kE4mJKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "quad(rv.pdf, 150, 200)" - ], - "id": "DL2kE4mJKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "HSNBIY7nKZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "var('t x'); integrate(density(X)(t), (t, 0, x))" - ], - "id": "HSNBIY7nKZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Szz8cj08KZyZ" - }, + "metadata": {}, "outputs": [], "source": [ "diff(x / 360, x)" - ], - "id": "Szz8cj08KZyZ" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "K-le9oLgKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "data = list(sample_iter(X, numsamples=1000))\n", "plt.hist(data); # 結果は割愛" - ], - "id": "K-le9oLgKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Qe4881VrKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "data = rv.rvs(1000)\n", "plt.hist(data); # 結果は割愛" - ], - "id": "Qe4881VrKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "LW0CDhhJKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "x = np.linspace(0, 360, 100); y = [density(X)(x) for x in x]\n", "_, ax = plt.subplots()\n", "ax.hist(data, bins='sturges', density=True, alpha=0.5)\n", "ax.plot(x, y);" - ], - "id": "LW0CDhhJKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KWEYMCLDKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "data = rv.rvs(1000)\n", @@ -2043,161 +1545,125 @@ "_, ax = plt.subplots()\n", "ax.hist(data, bins='sturges', density=True, alpha=0.5)\n", "ax.plot(x, y);" - ], - "id": "KWEYMCLDKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ODSMrYiuKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "X = Normal('X', 6, 2)\n", "N((cdf(X)(6 + 3 * 2) - cdf(X)(6 - 3 * 2)))" - ], - "id": "ODSMrYiuKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "oLfjkp_PKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "rv = stats.norm(6, 2)\n", "rv.cdf(6 + 3 * 2) - rv.cdf(6 - 3 * 2)" - ], - "id": "oLfjkp_PKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Jib1WxzpKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "#P(6 - 3 * 2 <= X <= 6 + 3 * 2)ではない.\n", "#P(6 - 3 * 2 <= X and X <= 6 + 3 * 2)ではない.\n", "N(P(And(6 - 3 * 2 <= X, X <= 6 + 3 * 2)))" - ], - "id": "Jib1WxzpKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sD-q2u_vKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "N(integrate(density(X)(x), (x, 6 - 3 * 2, 6 + 3 * 2)))" - ], - "id": "sD-q2u_vKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Y_nFxOotKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "quad(rv.pdf, 6 - 3 * 2, 6 + 3 * 2)" - ], - "id": "Y_nFxOotKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "uHaxgQqrKZyb" - }, + "metadata": {}, "outputs": [], "source": [ "var('mu sigma x'); X = Normal('X', mu, sigma)\n", "a, b = mu - 3 * sigma, mu + 3 * sigma\n", "(N((cdf(X)(b) - cdf(X)(a))), # 方法1\n", " N(integrate(density(X)(x), (x, a, b)))) # 方法3" - ], - "id": "uHaxgQqrKZyb" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "esieuADNKZyc" - }, + "metadata": {}, "outputs": [], "source": [ "var('mu sigma x')\n", "X = Normal('X', mu, sigma)\n", "density(X)(x)" - ], - "id": "esieuADNKZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ba0cP2tQKZyc" - }, + "metadata": {}, "outputs": [], "source": [ "X1 = Normal('X1', 0, 1); X2 = Normal('X2', 2, 1); var('x')\n", "plot(density(X1)(x), density(X2)(x), (x, -6, 6));" - ], - "id": "Ba0cP2tQKZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AsYL_hD5KZyc" - }, + "metadata": {}, "outputs": [], "source": [ "rv1 = stats.norm(0, 1); rv2 = stats.norm(2, 1); x = np.linspace(-6, 6, 100)\n", "pd.DataFrame({'x': x, 'X1': rv1.pdf(x), 'X2': rv2.pdf(x)}).plot(x='x');" - ], - "id": "AsYL_hD5KZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sfTIEGhsKZyc" - }, + "metadata": {}, "outputs": [], "source": [ "X3 = Normal('X3', 0, 2); var('x')\n", "plot(density(X1)(x), density(X3)(x), (x, -6, 6));" - ], - "id": "sfTIEGhsKZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GURIuXRqKZyc" - }, + "metadata": {}, "outputs": [], "source": [ "rv3 = stats.norm(0, 2); x = np.linspace(-6, 6, 100)\n", "pd.DataFrame({'x': x, 'X1': rv1.pdf(x), 'X3': rv3.pdf(x)}).plot(x='x');" - ], - "id": "GURIuXRqKZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "nPBuSERcKZyc" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; tmp = [0.9, 0.08, 0.015, 0.005]\n", @@ -2205,15 +1671,12 @@ "X = FiniteRV('X', dict(zip(Xs, Ps))) # 確率分布の定義\n", "data = sample_iter(X, numsamples=1000)\n", "Counter(data)" - ], - "id": "nPBuSERcKZyc" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "kfmtaK-7KZyd" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; tmp = [0.9, 0.08, 0.015, 0.005]\n", @@ -2221,15 +1684,12 @@ "rv = stats.rv_discrete(values=(Xs, Ps)) # 確率分布の定義\n", "data = rv.rvs(size=1000)\n", "Counter(data)" - ], - "id": "kfmtaK-7KZyd" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2ft8NysCKZyd" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", @@ -2240,15 +1700,12 @@ " set=Interval(a, b))\n", "data = list(sample_iter(X, numsamples=1000))\n", "plt.hist(data);" - ], - "id": "2ft8NysCKZyd" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "z0MHsV5bKZyd" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: abs(x); a, b = -1, 1;\n", @@ -2258,341 +1715,261 @@ "rv = MyX(a=a, b=b) # 確率分布の定義\n", "data = rv.rvs(size=1000)\n", "plt.hist(data);" - ], - "id": "z0MHsV5bKZyd" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "iauWR_ivKZyd" - }, + "metadata": {}, "outputs": [], "source": [ "X = DiscreteUniform('X', range(1, 7))\n", "Y = X**3 % 4\n", "data = list(sample_iter(Y, numsamples=1000))\n", "plt.hist(data);" - ], - "id": "iauWR_ivKZyd" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UVP8L8m7KZye" - }, + "metadata": {}, "outputs": [], "source": [ "X = Uniform('X', 0, 1)\n", "Y = X**2\n", "data = list(sample_iter(Y, numsamples=1000))\n", "plt.hist(data);" - ], - "id": "UVP8L8m7KZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "CJ-tyiw6KZye" - }, + "metadata": {}, "outputs": [], "source": [ "simplify(density(Y))" - ], - "id": "CJ-tyiw6KZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "mPBOf-sLKZye" - }, + "metadata": {}, "outputs": [], "source": [ "var('a b mu sigma'); X = Normal('X', mu, sigma); Y = a * X + b\n", "simplify(density(Y))" - ], - "id": "mPBOf-sLKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "7zwBypIQKZye" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; Ps = [0.9, 0.08, 0.015, 0.005]\n", "X = FiniteRV('X', dict(zip(Xs, Ps)))\n", "E(X)" - ], - "id": "7zwBypIQKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "dT7iY-_jKZye" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; Ps = [0.9, 0.08, 0.015, 0.005]\n", "rv = stats.rv_discrete(values=(Xs, Ps))\n", "rv.mean()" - ], - "id": "dT7iY-_jKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "hFPykF6TKZye" - }, + "metadata": {}, "outputs": [], "source": [ "sum(x * density(X)[x] for x in Xs)" - ], - "id": "hFPykF6TKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "q2EnDylyKZye" - }, + "metadata": {}, "outputs": [], "source": [ "sum(x * rv.pmf(x) for x in Xs)" - ], - "id": "q2EnDylyKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GxLXD1k1KZye" - }, + "metadata": {}, "outputs": [], "source": [ "np.dot(Xs, Ps)" - ], - "id": "GxLXD1k1KZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "u7fe2MfCKZye" - }, + "metadata": {}, "outputs": [], "source": [ "np.mean(list(sample_iter(X, numsamples=500000)))" - ], - "id": "u7fe2MfCKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "8ne25PcCKZye" - }, + "metadata": {}, "outputs": [], "source": [ "rv.rvs(size=500000).mean()" - ], - "id": "8ne25PcCKZye" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GHbQDGHEKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "X = ContinuousRV(x, Abs(x), set=Interval(-1, 1))\n", "integrate(x * density(X)(x), (x, -1, 1))" - ], - "id": "GHbQDGHEKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "yFAXwXWIKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "class MyX(stats.rv_continuous):\n", " def _pdf(self, x): return abs(x)\n", "rv = MyX(a=-1, b=1)\n", "quad(lambda x: x * rv.pdf(x), -1, 1)" - ], - "id": "yFAXwXWIKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lJk5J-9VKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; Ps = [0.9, 0.08, 0.015, 0.005]\n", "X = FiniteRV('X', dict(zip(Xs, Ps)))\n", "variance(X)" - ], - "id": "lJk5J-9VKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "TzYfK4B0KZyf" - }, + "metadata": {}, "outputs": [], "source": [ "Xs = [0, 100, 1000, 10000]; Ps = [0.9, 0.08, 0.015, 0.005]\n", "rv = stats.rv_discrete(values=(Xs, Ps))\n", "rv.var()" - ], - "id": "TzYfK4B0KZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "s5swZsM9KZyf" - }, + "metadata": {}, "outputs": [], "source": [ "E((X - E(X))**2)" - ], - "id": "s5swZsM9KZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AlDTrmKTKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "sum((x - E(X))**2 * density(X)[x] for x in Xs)" - ], - "id": "AlDTrmKTKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lRPDPt5vKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "sum((x - rv.mean())**2 * rv.pmf(x) for x in Xs)" - ], - "id": "lRPDPt5vKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "MGCq_jfnKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "np.dot((Xs - np.dot(Xs, Ps))**2, Ps)" - ], - "id": "MGCq_jfnKZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ic_YKo6-KZyf" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "X = ContinuousRV(x, Abs(x), set=Interval(-1, 1))\n", "integrate((x - E(X))**2 * density(X)(x), (x, -1, 1))" - ], - "id": "Ic_YKo6-KZyf" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "QrGh4ozrKZyf" - }, + "metadata": {}, "outputs": [], "source": [ "class MyX(stats.rv_continuous):\n", " def _pdf(self, x): return abs(x)\n", "rv = MyX(a=-1, b=1)\n", "quad(lambda x: (x - rv.mean())**2 * rv.pdf(x), -1, 1)" - ], - "id": "QrGh4ozrKZyf" + ] }, { "cell_type": "markdown", - "source": [ - "# 9 多次元の確率分布" - ], - "metadata": { - "id": "7979gbeXKZyg" - }, - "id": "7979gbeXKZyg" + "source": "# 9 多次元の確率分布", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "dEzYAj_KKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "X1 = DiscreteUniform('X1', range(1, 7))\n", "X2 = DiscreteUniform('X2', range(1, 7))\n", "X, Y = Max(X1, X2), Min(X1, X2)\n", "{(x, y): P(And(Eq(X, x), Eq(Y, y))) for x in range(1, 7) for y in range(1, 7)}" - ], - "id": "dEzYAj_KKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1FupUwRKKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "density(X), density(Y)" - ], - "id": "1FupUwRKKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Pw_J2vgIKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "{(x, y): P(And(Le(X, x), Le(Y, y))) for x in range(1, 7) for y in range(1, 7)}" - ], - "id": "Pw_J2vgIKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ENqwR3UNKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "X1 = DiscreteUniform('X1', range(1, 7))\n", @@ -2604,15 +1981,12 @@ " std(X), std(Y), # 標準偏差\n", " covariance(X, Y), # 共分散\n", " correlation(X, Y)) # 相関係数" - ], - "id": "ENqwR3UNKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "MptfziWjKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "uX, uY = E(X), E(Y); sX, sY = std(X), std(Y)\n", @@ -2620,83 +1994,65 @@ " E((X - uX)**2), E((Y - uY)**2), # 分散\n", " E((X - uX) * (Y - uY)), # 共分散\n", " E((X - uX) * (Y - uY) / sX / sY)) # 相関係数" - ], - "id": "MptfziWjKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "TMiZWitWKZyg" - }, + "metadata": {}, "outputs": [], "source": [ "print(sum((x * P(Eq(X, x)) for x in range(1, 7)))) # 平均\n", "print(sum((x - uX) * (y - uY) * P(And(Eq(X, x), Eq(Y, y))) # 共分散\n", " for x in range(1, 7) for y in range(1, 7)))" - ], - "id": "TMiZWitWKZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JTy8ym6-KZyg" - }, + "metadata": {}, "outputs": [], "source": [ "[P(And(X <= x, Y <= y)) for x in range(1, 7) for y in range(1, 7)] == \\\n", "[P(X <= x) * P(Y <= y) for x in range(1, 7) for y in range(1, 7)]" - ], - "id": "JTy8ym6-KZyg" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "zOoVQ_u3KZyh" - }, + "metadata": {}, "outputs": [], "source": [ "U = DiscreteUniform('X', range(1, 7)); X = U % 2; Y = U % 3\n", "[P(And(X <= x, Y <= y)) for x in range(2) for y in range(3)] == \\\n", "[P(X <= x) * P(Y <= y) for x in range(2) for y in range(3)]" - ], - "id": "zOoVQ_u3KZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Xt-X3wB4KZyh" - }, + "metadata": {}, "outputs": [], "source": [ "X = Binomial('X', 3, sym.S(1) / 2)\n", "Y = Piecewise((1, Or(Eq(X, 0), Eq(X, 3))), (2, True))\n", "covariance(X, Y)" - ], - "id": "Xt-X3wB4KZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2lOYfRJ4KZyh" - }, + "metadata": {}, "outputs": [], "source": [ "[P(And(X <= x, Y <= y)) for x in range(4) for y in (1, 2)] == \\\n", "[P(X <= x) * P(Y <= y) for x in range(4) for y in (1, 2)]" - ], - "id": "2lOYfRJ4KZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "8K1GUc92KZyh" - }, + "metadata": {}, "outputs": [], "source": [ "X1 = DiscreteUniform('X1', range(1, 7))\n", @@ -2705,15 +2061,12 @@ "\n", "(E(X + Y), E(X) + E(Y), # 平均\n", " variance(X + Y), variance(X) + variance(Y) + 2 * covariance(X, Y)) # 分散" - ], - "id": "8K1GUc92KZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "7DAPXq3fKZyh" - }, + "metadata": {}, "outputs": [], "source": [ "n = 15; p = sym.S(4) / 10; Y = Binomial('Y', n, p)\n", @@ -2721,15 +2074,12 @@ "x1 = range(0, n + 1); y1 = [density(Y)(x) for x in x1]\n", "x2 = np.linspace(0, n, 101); y2 = [density(Z)(t) for t in x2]\n", "_, ax = plt.subplots(); ax.scatter(x1, y1); ax.plot(x2, y2);" - ], - "id": "7DAPXq3fKZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qRCXs_S2KZyh" - }, + "metadata": {}, "outputs": [], "source": [ "n = 15; p = 4 / 10; Y = stats.binom(n, p)\n", @@ -2737,45 +2087,36 @@ "x1 = range(0, n + 1); y1 = Y.pmf(x1)\n", "x2 = np.linspace(0, n, 101); y2 = Z.pdf(x2)\n", "_, ax = plt.subplots(); ax.scatter(x1, y1); ax.plot(x2, y2);" - ], - "id": "qRCXs_S2KZyh" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-GrCAMB9KZyi" - }, + "metadata": {}, "outputs": [], "source": [ "X = Uniform('X', 0, 1); Z = Normal('Z', 0, 1)\n", "data = [sum(sample_iter(X, numsamples=12)) - 6 for _ in range(10000)]\n", "x = np.linspace(-4, 4, 101); y = [density(Z)(t) for t in x]\n", "_, ax = plt.subplots(); ax.hist(data, density=True, alpha=0.5); ax.plot(x, y);" - ], - "id": "-GrCAMB9KZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fnDAdtM7KZyi" - }, + "metadata": {}, "outputs": [], "source": [ "X = stats.uniform(); Z = stats.norm()\n", "data = [sum(X.rvs(12)) - 6 for _ in range(10000)]\n", "x = np.linspace(-4, 4, 101); y = Z.pdf(x)\n", "_, ax = plt.subplots(); ax.hist(data, density=True, alpha=0.5); ax.plot(x, y);" - ], - "id": "fnDAdtM7KZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sIDLpO7XKZyi" - }, + "metadata": {}, "outputs": [], "source": [ "mu = [3, 6]; Sigma = [[5, 7], [7, 13]];\n", @@ -2787,91 +2128,68 @@ "ax = fig.add_subplot(111, projection='3d')\n", "ax.plot_surface(Y1, Y2, z, cmap='viridis')\n", "ax.set_xlabel('Y1'); ax.set_ylabel('Y2');" - ], - "id": "sIDLpO7XKZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "TNvhSKo7KZyi" - }, + "metadata": {}, "outputs": [], "source": [ "plt.contourf(Y1, Y2, z, cmap='viridis'); plt.xlabel('Y1'); plt.ylabel('Y2');" - ], - "id": "TNvhSKo7KZyi" + ] }, { "cell_type": "markdown", - "source": [ - "# 10 推測統計" - ], - "metadata": { - "id": "wH2FmTsIKZyi" - }, - "id": "wH2FmTsIKZyi" + "source": "# 10 推測統計", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "dIiZcTenKZyi" - }, + "metadata": {}, "outputs": [], "source": [ "mu = 2; sigma = 3; rv = stats.norm(mu, sigma)\n", "data1 = [rv.rvs(5).mean() for _ in range(10000)]\n", "data2 = [rv.rvs(50).mean() for _ in range(10000)]\n", "((np.mean(data1), np.var(data1)), (np.mean(data2), np.var(data2)))" - ], - "id": "dIiZcTenKZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "kd3t0dvRKZyi" - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(data1, density=True), plt.hist(data2, alpha=0.7, density=True);" - ], - "id": "kd3t0dvRKZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_4186KaYKZyi" - }, + "metadata": {}, "outputs": [], "source": [ "mu = 2; sigma = 3; rv = stats.norm(mu, sigma)\n", "data1 = [rv.rvs(5).var(ddof=1) for _ in range(10000)]\n", "data2 = [rv.rvs(50).var(ddof=1) for _ in range(10000)]\n", "((np.mean(data1), np.var(data1)), (np.mean(data2), np.var(data2)))" - ], - "id": "_4186KaYKZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Nfez8W6WKZyi" - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(data1, density=True), plt.hist(data2, alpha=0.7, density=True);" - ], - "id": "Nfez8W6WKZyi" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "NK3ofcYRKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "n = 4; mu = 5; sigma = 7; rv = stats.norm(mu, sigma)\n", @@ -2881,15 +2199,12 @@ "_, ax = plt.subplots()\n", "ax.hist(data, bins='sturges', density=True, alpha=0.5)\n", "ax.plot(x, y);" - ], - "id": "NK3ofcYRKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "FTkJfrx8KZyj" - }, + "metadata": {}, "outputs": [], "source": [ "n = 4; mu = 5; sigma = 7; rv = stats.norm(mu, sigma)\n", @@ -2899,15 +2214,12 @@ "_, ax = plt.subplots()\n", "ax.hist(data, density=True, bins=np.arange(-5, 5, 0.5), alpha=0.5)\n", "ax.plot(x, y);" - ], - "id": "FTkJfrx8KZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "H609ZDPJKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "m = 5; muX = 2; sigmaX = 3; rvX = stats.norm(muX, sigmaX)\n", @@ -2918,180 +2230,136 @@ "_, ax = plt.subplots()\n", "ax.hist(data, density=True, bins=np.arange(0, 5.2, 0.2), alpha=0.5)\n", "ax.plot(x, y);" - ], - "id": "H609ZDPJKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "_YjJfB3dKZyj" - }, + "metadata": {}, "outputs": [], "source": [ - "n = 15; p0 = 4 / 10; binom_test(2, 15, 4 / 10)" - ], - "id": "_YjJfB3dKZyj" + "n = 15; p0 = 4 / 10; binom_test(2, n, p0)" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "hLUdzYAcKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "binom_test(2, n, p0, alternative=\"smaller\")" - ], - "id": "hLUdzYAcKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "VfCyJ4MwKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "n = 15; p0 = 4 / 10; mu0 = n * p0; sigma0 = np.sqrt((n * p0 * (1 - p0)))\n", "2 * stats.norm.cdf(2, mu0, sigma0)" - ], - "id": "VfCyJ4MwKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UK5Km6wKKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "alpha = 5 / 100; stats.norm.ppf((alpha/2, 1 - alpha/2), mu0, sigma0)" - ], - "id": "UK5Km6wKKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fc5LT7_7KZyj" - }, + "metadata": {}, "outputs": [], "source": [ "x = [24.2, 25.3, 26.2, 25.7, 24.4, 25.1, 25.6]; d = DescrStatsW(x); mu0 = 25\n", "d.ttest_mean(mu0)" - ], - "id": "fc5LT7_7KZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "CU48t8QzKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "m = np.mean(x); s2 = np.var(x, ddof=1); n = len(x);\n", "t = (m - mu0) / np.sqrt(s2 / n); c = stats.t.cdf(t, n - 1)\n", "2 * min(c, 1 - c)" - ], - "id": "CU48t8QzKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KSqEJ0odKZyj" - }, + "metadata": {}, "outputs": [], "source": [ "alpha = 5 / 100\n", "stats.t.ppf((alpha / 2, 1 - alpha / 2), n - 1)" - ], - "id": "KSqEJ0odKZyj" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9KmXA9TmKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "d.tconfint_mean()" - ], - "id": "9KmXA9TmKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "y16SEzw2KZyk" - }, + "metadata": {}, "outputs": [], "source": [ "x = [25, 24, 25, 26]; y = [23, 18, 22, 28, 17, 25, 19, 16]\n", "ttest_ind(x, y, alternative=\"larger\", usevar='unequal')" - ], - "id": "y16SEzw2KZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1vWar-VrKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "tmp = CompareMeans(DescrStatsW(x), DescrStatsW(y))\n", "tmp.tconfint_diff(alternative=\"larger\", usevar='unequal')" - ], - "id": "1vWar-VrKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "pkzq60mTKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "x = [25, 24, 25, 26]; y = [23, 18, 22, 28, 17, 25, 19, 16]\n", "f = np.var(x, ddof=1) / np.var(y, ddof=1); m = len(x); n = len(y);\n", "c = stats.f.cdf(f, m - 1, n - 1)\n", "f, 2 * min(c, 1 - c)" - ], - "id": "pkzq60mTKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0RZmFANtKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "alpha = 0.05\n", "stats.f.ppf((alpha / 2, 1 - alpha / 2), m - 1, n - 1)" - ], - "id": "0RZmFANtKZyk" + ] }, { "cell_type": "markdown", - "source": [ - "# 11 線形回帰分析" - ], - "metadata": { - "id": "U7zLWxwdKZyk" - }, - "id": "U7zLWxwdKZyk" + "source": "# 11 線形回帰分析", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2PBRr8efKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({\n", @@ -3119,27 +2387,21 @@ "# Skew: -0.816 Prob(JB): 0.737\n", "# Kurtosis: 2.000 Condition No.: 35\n", "# ================================================================" - ], - "id": "2PBRr8efKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "r8KLJz9TKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "model.predict({'x1': 1.5, 'x2': 4})" - ], - "id": "r8KLJz9TKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "k0LH7fUFKZyk" - }, + "metadata": {}, "outputs": [], "source": [ "x1 = np.array([1, 3, 6, 10]); y = np.array([7, 1, 6, 14])\n", @@ -3147,69 +2409,54 @@ " e = y - (b[0] + b[1] * x1)\n", " return e @ e # 内積\n", "minimize(L, x0=[0, 0])" - ], - "id": "k0LH7fUFKZyk" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JwFfdygCKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({\n", " 'x1': [1, 1, 2, 3], 'x2': [2, 3, 5, 7], 'y': [3, 6, 3, 6]})\n", "y, X = dmatrices('y ~ x1 + x2', data)\n", "linalg.inv(X.T @ X) @ X.T @ y" - ], - "id": "JwFfdygCKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2oeJ0dT8KZyl" - }, + "metadata": {}, "outputs": [], "source": [ "linalg.pinv(X) @ y" - ], - "id": "2oeJ0dT8KZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "jKFoXWrFKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({\n", " 'x1': [1, 1, 2, 3], 'x2': [2, 3, 5, 7], 'y': [3, 6, 3, 6]})\n", "model = smf.ols('y ~ x1 + x2', data).fit()\n", "model.rsquared" - ], - "id": "jKFoXWrFKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AhfC79gAKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "model.rsquared_adj" - ], - "id": "AhfC79gAKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ii8SlJPWKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "x1 = [1, 3, 6, 10]; y = [7, 1, 6, 14]\n", @@ -3218,15 +2465,12 @@ "yh = X @ linalg.pinv(X) @ y\n", "eh = y - yh; fh = yh - np.mean(y); g = y - np.mean(y)\n", "R2 = 1 - np.dot(eh, eh) / np.dot(g, g); R2" - ], - "id": "Ii8SlJPWKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "MuYj_ogxKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "(np.allclose(eh.mean(), 0), # 特徴1\n", @@ -3236,45 +2480,36 @@ " np.allclose(R2, np.corrcoef(y, yh)[0, 1]**2), # 特徴5\n", " 0 <= R2 <= 1, # 特徴6\n", " np.allclose(np.corrcoef(y, yh), np.corrcoef(y, x1))) # 特徴7" - ], - "id": "MuYj_ogxKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Dxawcvl5KZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({\n", " 'x1': [1, 1, 2, 3], 'x2': [2, 3, 5, 7], 'y': [3, 6, 3, 6]})\n", "model = smf.ols('y ~ x1 + x2', data).fit()\n", "model.scale" - ], - "id": "Dxawcvl5KZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "eBbxfAIRKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({\n", " 'x1': [1, 1, 2, 3], 'x2': [2, 3, 5, 7], 'y': [3, 6, 3, 6]})\n", "model = smf.ols('y ~ x1 + x2', data).fit()\n", "model.f_pvalue" - ], - "id": "eBbxfAIRKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "D4O9xNcnKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({'x1': [35, 45, 55, 65, 75],\n", @@ -3282,15 +2517,12 @@ "model = smf.ols('y ~ x1', data).fit()\n", "print(model.summary2(alpha=0.05)) # 信頼区間はこのレポートにも表示される.\n", "model.conf_int(alpha=0.05)" - ], - "id": "D4O9xNcnKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KqzvJeNvKZyl" - }, + "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame({'x1': [35, 45, 55, 65, 75],\n", @@ -3302,201 +2534,149 @@ "_, ax = plt.subplots()\n", "ax.scatter(data.x1, data.y)\n", "df.plot(x='x1', ax=ax);" - ], - "id": "KqzvJeNvKZyl" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ud5iPiRUKZym" - }, + "metadata": {}, "outputs": [], "source": [ "df = ci[['mean', 'obs_ci_lower', 'obs_ci_upper']].assign(x1=tmp.x1)\n", "_, ax = plt.subplots()\n", "ax.scatter(data.x1, data.y)\n", "df.plot(x='x1', ax=ax);" - ], - "id": "Ud5iPiRUKZym" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "PsdQCzecKZym" - }, + "metadata": {}, "outputs": [], "source": [ "sns.regplot(x=data.x1, y=data.y, ci=95);" - ], - "id": "PsdQCzecKZym" + ] }, { "cell_type": "markdown", - "source": [ - "# 12 関数の極限と連続性" - ], - "metadata": { - "id": "xcZ15GJbKZym" - }, - "id": "xcZ15GJbKZym" + "source": "# 12 関数の極限と連続性", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1RLL3eu8KZym" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: 2 * x - 3; var('x')\n", "limit(f(x), x, 1)" - ], - "id": "1RLL3eu8KZym" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "MNDkBCuRKZym" - }, + "metadata": {}, "outputs": [], "source": [ "limit(2 * x - 3, x, 1)" - ], - "id": "MNDkBCuRKZym" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "osELWNxqKZym" - }, + "metadata": {}, "outputs": [], "source": [ "g = lambda x: (x**2 - 2) / (x - sqrt(2))\n", "limit(g(x), x, sqrt(2))" - ], - "id": "osELWNxqKZym" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "6V_dlJJbKZym" - }, + "metadata": {}, "outputs": [], "source": [ "limit((1 + 1 / x)**x, x, oo)" - ], - "id": "6V_dlJJbKZym" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gnA0F57yKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "limit(1 / x**2, x, 0)" - ], - "id": "gnA0F57yKZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-8tmzXpkKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "limit(abs(x) / x, x, 0, dir='+'), limit(abs(x) / x, x, 0, dir='-')" - ], - "id": "-8tmzXpkKZyn" + ] }, { "cell_type": "markdown", - "source": [ - "# 13 微分" - ], - "metadata": { - "id": "H-U35DnvKZyn" - }, - "id": "H-U35DnvKZyn" + "source": "# 13 微分", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "cDErHUoLKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: x**3; var('h'); a = 1\n", "limit((f(a + h) - f(a)) / h, h, 0)" - ], - "id": "cDErHUoLKZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9c0xsRiuKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: x**3; var('x')\n", "diff(f(x), x)" - ], - "id": "9c0xsRiuKZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "eegQeOX6KZyn" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: x**3; var('x')\n", "f1 = Lambda(x, diff(f(x), x))\n", "f2 = diff(f(x), x)\n", "f1, f2" - ], - "id": "eegQeOX6KZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sSaWATCOKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "f1(1), f2.subs(x, 1)" - ], - "id": "sSaWATCOKZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gpSFlyTpKZyn" - }, + "metadata": {}, "outputs": [], "source": [ "diff(x**3, x, 2)" - ], - "id": "gpSFlyTpKZyn" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "hu-t_NBMKZyo" - }, + "metadata": {}, "outputs": [], "source": [ "var('a b t x')\n", @@ -3507,277 +2687,207 @@ " diff(f(g(x)), x), # ②\n", " diff(f(t), t).subs(t, g(x)) * diff(g(x), x), # ③\n", " fp(g(x)) * gp(x)) # ④" - ], - "id": "hu-t_NBMKZyo" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "58il8yDUKZyo" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); tmp = series(sin(x), x, x0=0, n=6); tmp" - ], - "id": "58il8yDUKZyo" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "SgMVw4CCKZyo" - }, + "metadata": {}, "outputs": [], "source": [ "plot(*(tmp.removeO(), sin(x)), (x, -pi, pi));" - ], - "id": "SgMVw4CCKZyo" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "dS_PR3EFKZyo" - }, + "metadata": {}, "outputs": [], "source": [ "a = 0;\n", "np.sum([diff(sin(x), x, k).subs(x, a) * (x - a)**k / factorial(k)\n", " for k in range(6)])" - ], - "id": "dS_PR3EFKZyo" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "wjyAN033KZyo" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: x**3 - 12 * x; var('x')\n", "sol = solve(diff(f(x), x), x); print(sol)\n", "series(f(x), x0=sol[0], n=3)" - ], - "id": "wjyAN033KZyo" + ] }, { "cell_type": "markdown", - "source": [ - "# 14 積分" - ], - "metadata": { - "id": "rbYbWzaFKZyo" - }, - "id": "rbYbWzaFKZyo" + "source": "# 14 積分", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "x6jvTHJ_KZyp" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "integrate(-x**2 + 4 * x + 1, (x, 1, 4))" - ], - "id": "x6jvTHJ_KZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "oj1EgUykKZyp" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: -x**2 + 4 * x + 1; var('k n')\n", "a = 1; b = 4; h = (b - a) / n\n", "s = simplify(Sum(f(a + h * k) * h, (k, 1, n)).doit()); print(s)\n", "limit(s, n, oo)" - ], - "id": "oj1EgUykKZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "tfm-1Ns8KZyp" - }, + "metadata": {}, "outputs": [], "source": [ "var('a t x'); integrate(-t**2 + 4 * t + 1, (t, a, x))" - ], - "id": "tfm-1Ns8KZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GIrBM8bwKZyp" - }, + "metadata": {}, "outputs": [], "source": [ "integrate(-x**2 + 4 * x + 1, x)" - ], - "id": "GIrBM8bwKZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Pzdp4ITYKZyp" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); y = Function('y')\n", "dsolve(Eq(diff(y(x), x), -x**2 + 4 * x + 1), y(x))" - ], - "id": "Pzdp4ITYKZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "28n3-297KZyp" - }, + "metadata": {}, "outputs": [], "source": [ "dsolve(Eq(diff(y(x), x), -x**2 + 4 * x + 1), y(x), ics={y(0): 1})" - ], - "id": "28n3-297KZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "e1UycdWBKZyp" - }, + "metadata": {}, "outputs": [], "source": [ "tmp = dsolve(Eq(diff(y(x), x), -x * y(x)), y(x)); tmp" - ], - "id": "e1UycdWBKZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "QBl3eePfKZyp" - }, + "metadata": {}, "outputs": [], "source": [ "solve(Eq(integrate(tmp.rhs, (x, -oo, oo)), 1), 'C1')" - ], - "id": "QBl3eePfKZyp" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XNAxWC0UKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "f = Function('f'); var('a t x')\n", "Lambda(x, diff(Lambda(x, integrate(f(t), (t, a, x)))(x), x))" - ], - "id": "XNAxWC0UKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "EZ3MODxOKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "diff(integrate(f(t), (t, a, x)), x)" - ], - "id": "EZ3MODxOKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "te_XDjR5KZyq" - }, + "metadata": {}, "outputs": [], "source": [ "var('x')\n", "F = integrate(-x**2 + 4 * x + 1, x)\n", "F.subs(x, 4) - F.subs(x, 1)" - ], - "id": "te_XDjR5KZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "oTuvQWRoKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); integrate((x**2 + 2 * x + 1 + (3 * x + 1) * sqrt(x + log(x))) /\n", " (x * sqrt(x + log(x)) * (x + sqrt(x + log(x)))), x)" - ], - "id": "oTuvQWRoKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "NPcBpVrpKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "var('a x')\n", "simplify(integrate(1/x**a, (x, 0, 1)))" - ], - "id": "NPcBpVrpKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "jqtWt86RKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "integrate(1/x**a, (x, 1, oo))" - ], - "id": "jqtWt86RKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ng11DlhrKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "var('x'); integrate(exp(-x**2), (x, -oo, oo))" - ], - "id": "Ng11DlhrKZyq" + ] }, { "cell_type": "markdown", - "source": [ - "# 15 多変数関数の微分積分" - ], - "metadata": { - "id": "KF2Y1PrAKZyq" - }, - "id": "KF2Y1PrAKZyq" + "source": "# 15 多変数関数の微分積分", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9MyAsRAZKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: x**2 * y / (x**4 + y**2); var('x y r theta')\n", @@ -3785,53 +2895,41 @@ " limit(limit(f(x, y), y, 0), x, 0), # ②\n", " limit(f(r * cos(theta), r * sin(theta)), r, 0), # ③\n", " limit(f(x, x**2), x, 0)) # ④" - ], - "id": "9MyAsRAZKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "trMNJZe9KZyq" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: 2 - x**2 - y**2; var('x y')\n", "(diff(f(x, y), x), diff(f(x, y), y))" - ], - "id": "trMNJZe9KZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "T0Ke9PgNKZyq" - }, + "metadata": {}, "outputs": [], "source": [ "diff(f(x, y), Matrix([x, y]))" - ], - "id": "T0Ke9PgNKZyq" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "CbdapdZNKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: 2 * x**3 + 5 * x * y + 2 * y**2; var('x y')\n", "hessian(f(x, y), Matrix([x, y]))" - ], - "id": "CbdapdZNKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "v2REAJcCKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: sqrt(x[0]**2 + x[1]**2); var('x1 x2 t');\n", @@ -3839,29 +2937,23 @@ "F = lambda t: f(a + t * h)\n", "expr = series(F(t), t, x0=0, n=3).removeO().subs(t, 1)\n", "simplify(expr)" - ], - "id": "v2REAJcCKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "fldqt4kdKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "gradf = diff(f(x), x).subs(zip(x, a))\n", "H = hessian(f(x), x).subs(zip(x, a))\n", "simplify(f(a) + gradf.dot(x - a) + (x - a).dot(H @ (x - a)) / 2)" - ], - "id": "fldqt4kdKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "OBz4FJEGKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x: 2 * x[0]**3 + x[0] * x[1]**2 + 5 * x[0]**2 + x[1]**2\n", @@ -3875,40 +2967,31 @@ " if h.is_indefinite: return (a, f(a), 0) # 極値ではない\n", " else: return (a, f(a), None) # 不明\n", "[check(H, a) for a in points]" - ], - "id": "OBz4FJEGKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "t0mEbQDdKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: x**2 + y**2; var('x y')\n", "integrate(integrate(f(x, y), (y, 0, x)), (x, 0, 1))" - ], - "id": "t0mEbQDdKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "IBa1h2lpKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "integrate(integrate(f(x, y), (x, y, 1)), (y, 0, 1))" - ], - "id": "IBa1h2lpKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "t97tc2n3KZyr" - }, + "metadata": {}, "outputs": [], "source": [ "f = lambda x, y: x**2 + y**2; var('u v')\n", @@ -3917,505 +3000,378 @@ "detJ = J.det(); print(detJ)\n", "integrate(integrate(f(x, y) * abs(detJ),\n", " (v, 0, 2 * u / 3)), (u, 0, sym.S(1) / 2))" - ], - "id": "t97tc2n3KZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "QMnBy8HtKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "var('r theta'); x, y = r * cos(theta), r * sin(theta)\n", "J = Matrix([x, y]).jacobian((r, theta))\n", "simplify(J.det())" - ], - "id": "QMnBy8HtKZyr" + ] }, { "cell_type": "markdown", - "source": [ - "# 16 ベクトル" - ], - "metadata": { - "id": "TI0cRC0bKZyr" - }, - "id": "TI0cRC0bKZyr" + "source": "# 16 ベクトル", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0PqxqyUVKZyr" - }, + "metadata": {}, "outputs": [], "source": [ "a = Matrix([2, 3, 5]); len(a)" - ], - "id": "0PqxqyUVKZyr" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "9uFH_d0nKZys" - }, + "metadata": {}, "outputs": [], "source": [ "10 * Matrix([2, 3])" - ], - "id": "9uFH_d0nKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "zm3jUZCcKZys" - }, + "metadata": {}, "outputs": [], "source": [ "u = Matrix([10, 20]); v = Matrix([2, 3]); u + v" - ], - "id": "zm3jUZCcKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gbRw1Z1lKZys" - }, + "metadata": {}, "outputs": [], "source": [ "t = sym.S(10)\n", "a = Matrix([1 / t + 2 / t, 1 / t + 2 / t - 3 / t]); b = Matrix([3 / t, 0])\n", "a == b" - ], - "id": "gbRw1Z1lKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JlvYbezJKZys" - }, + "metadata": {}, "outputs": [], "source": [ "a = np.array([1/10 + 2/10, 1/10 + 2/10 - 3/10]); b = np.array([3/10, 0])\n", "np.allclose(a, b)" - ], - "id": "JlvYbezJKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "mrXGT32nKZys" - }, + "metadata": {}, "outputs": [], "source": [ "100 * Matrix([1, 2]) + 10 * Matrix([3, 1])" - ], - "id": "mrXGT32nKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ifu9A5AxKZys" - }, + "metadata": {}, "outputs": [], "source": [ "100 * np.array([1, 2]) + 10 * np.array([3, 1])" - ], - "id": "ifu9A5AxKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "IqQKY_pUKZys" - }, + "metadata": {}, "outputs": [], "source": [ "a = Matrix([3, 4])\n", "a.norm()" - ], - "id": "IqQKY_pUKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "rmTuvn7fKZys" - }, + "metadata": {}, "outputs": [], "source": [ "a = np.array([3, 4])\n", "linalg.norm(a)" - ], - "id": "rmTuvn7fKZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "EI3sglo6KZys" - }, + "metadata": {}, "outputs": [], "source": [ "var('x y'); a = Matrix([x, y]); sqrt(a.dot(a))" - ], - "id": "EI3sglo6KZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AA57i5i5KZys" - }, + "metadata": {}, "outputs": [], "source": [ "var('x y', real=True); a = Matrix([x, y]); a.norm()" - ], - "id": "AA57i5i5KZys" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Yqbzl-DEKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "a = Matrix([3, 4])\n", "a.normalized()" - ], - "id": "Yqbzl-DEKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "PzDhN-xfKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "a = np.array([3, 4])\n", "a / linalg.norm(a)" - ], - "id": "PzDhN-xfKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "BFbdawMkKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "a = Matrix([1, 0]); b = Matrix([1, 1])\n", "acos(a.dot(b) / (a.norm() * b.norm()))" - ], - "id": "BFbdawMkKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qQRs3_SEKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "a = np.array([1, 0]); b = np.array([1, 1])\n", "np.arccos(a @ b / (linalg.norm(a) * linalg.norm(b)))" - ], - "id": "qQRs3_SEKZyt" + ] }, { "cell_type": "markdown", - "source": [ - "# 17 行列" - ], - "metadata": { - "id": "bu-0tst9KZyt" - }, - "id": "bu-0tst9KZyt" + "source": "# 17 行列", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qK5oD_iAKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 2, 0], [0, 3, 4]]); A" - ], - "id": "qK5oD_iAKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Nj5t_aOdKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2, 0], [0, 3, 4]]); A" - ], - "id": "Nj5t_aOdKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "zmaJHndcKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "x = [5, 7]; diag(*x) # diag(x)はNG.diag(5, 7)はOK." - ], - "id": "zmaJHndcKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lvAFde6jKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "x = [5, 7]; np.diag(x) # np.diag(*x)はNG.np.diag([5, 7])はOK." - ], - "id": "lvAFde6jKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "zn9bnxctKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[1, 2], [2, 3]]).is_symmetric()" - ], - "id": "zn9bnxctKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "V6X7HIQJKZyt" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2], [2, 3]])\n", "np.allclose(A.T, A)" - ], - "id": "V6X7HIQJKZyt" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UGvcT4P_KZyu" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[11, 12, 13], [21, 22, 23], [31, 32, 33]]); A" - ], - "id": "UGvcT4P_KZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "yWMGAKIqKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]); An" - ], - "id": "yWMGAKIqKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "SDGaVUlIKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "A[0:2, 0:2], A[:2, :2] # 二つの方法" - ], - "id": "SDGaVUlIKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KKbKTYY3KZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An[0:2, 0:2], An[:2, :2] # 二つの方法" - ], - "id": "KKbKTYY3KZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qeBQHCfHKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "A[:, 2]" - ], - "id": "qeBQHCfHKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "J7XiWv9tKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An[:, 2]" - ], - "id": "J7XiWv9tKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "00xzhyEhKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An[:, [2]]" - ], - "id": "00xzhyEhKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "PqWmjDMsKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "A[1, :]" - ], - "id": "PqWmjDMsKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "wOTHmH9bKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An[1, :]" - ], - "id": "wOTHmH9bKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "V1u1OIYtKZyu" - }, + "metadata": {}, "outputs": [], "source": [ "An[[1], :]" - ], - "id": "V1u1OIYtKZyu" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "uJP9DP_vKZyv" - }, + "metadata": {}, "outputs": [], "source": [ "10 * Matrix([[2, 3], [5, 7]])" - ], - "id": "uJP9DP_vKZyv" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "YbDTL3W5KZyv" - }, + "metadata": {}, "outputs": [], "source": [ "10 * np.array([[2, 3], [5, 7]])" - ], - "id": "YbDTL3W5KZyv" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZXepgCZBKZyw" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[10, 20], [30, 40]]) + Matrix([[2, 3], [4, 5]])" - ], - "id": "ZXepgCZBKZyw" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "M1fyw-iiKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "np.array([[10, 20], [30, 40]]) + np.array([[2, 3], [4, 5]])" - ], - "id": "M1fyw-iiKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "KlBpFxcbKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[2, 3], [5, 7]]); B = Matrix([[1, 2], [3, 4]])\n", "A @ B" - ], - "id": "KlBpFxcbKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ycvU-sSRKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[2, 3], [5, 7]]); B = np.array([[1, 2], [3, 4]])\n", "A @ B" - ], - "id": "ycvU-sSRKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "d4plRUiiKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[2, 3], [5, 7]]); B = Matrix([[1, 2, 3], [4, 5, 6]]); S = A @ B\n", @@ -4425,15 +3381,12 @@ "S3 = Matrix.hstack(*[A @ B[:, j] for j in range(s)]) # ③\n", "S4 = Matrix.vstack(*[A[i, :] @ B for i in range(p)]) # ④\n", "S == S1, S == S2, S == S3, S == S4" - ], - "id": "d4plRUiiKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "RE4-qsemKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[2, 3], [5, 7]]); B = np.array([[1, 2, 3], [4, 5, 6]]); S = A @ B\n", @@ -4444,15 +3397,12 @@ "S3 = np.vstack([A @ B[:, j] for j in range(s)]).T # ③\n", "S4 = np.vstack([A[i, :] @ B for i in range(p)]) # ④\n", "np.allclose(S, S1), np.allclose(S, S2), np.allclose(S, S3), np.allclose(S, S4)" - ], - "id": "RE4-qsemKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Wjz1cXXGKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "var('a1 a2 x1 x2 p q r s')\n", @@ -4461,220 +3411,167 @@ "(Eq(diff(a.dot(x), x), a),\n", " Eq(diff(x.dot(G @ x), x), 2 * G @ x),\n", " Eq(simplify(diff((A @ x).dot(A @ x), x) - 2 * A.T @ A @ x), zeros(2, 1)))" - ], - "id": "Wjz1cXXGKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1Mkun2R-KZyx" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[3, 2], [1, 2]]).det()" - ], - "id": "1Mkun2R-KZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "alGQjg8qKZyx" - }, + "metadata": {}, "outputs": [], "source": [ "linalg.det(np.array([[3, 2], [1, 2]]))" - ], - "id": "alGQjg8qKZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Fc--ZeL8KZyx" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[2, 3], [5, 7]]).inv()" - ], - "id": "Fc--ZeL8KZyx" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "qducT8uRKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "linalg.inv(np.array([[2, 3], [5, 7]]))" - ], - "id": "qducT8uRKZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "LgDNlGETKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[3, 2], [1, 2]]); b = Matrix([8, 4])\n", "A.inv() @ b" - ], - "id": "LgDNlGETKZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JcZcebidKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[3, 2], [1, 2]]); b = np.array([8, 4])\n", "linalg.inv(A) @ b" - ], - "id": "JcZcebidKZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "YvakW0b2KZyy" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[4, 2, 8], [2, 1, 4]]).rref()[0]" - ], - "id": "YvakW0b2KZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "z7L-cnr6KZyy" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[2, 0, 2], [0, 2, -2], [2, 2, 0]])\n", "A.rank()" - ], - "id": "z7L-cnr6KZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "OPrJEHFnKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[2, 0, 2], [0, 2, -2], [2, 2, 0]])\n", "np.linalg.matrix_rank(A)" - ], - "id": "OPrJEHFnKZyy" + ] }, { "cell_type": "markdown", - "source": [ - "# 18 ベクトル空間" - ], - "metadata": { - "id": "HpPX_QVHKZyy" - }, - "id": "HpPX_QVHKZyy" + "source": "# 18 ベクトル空間", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "2H93zb7xKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "a1 = Matrix([3, 1]); a2 = Matrix([2, 2]); var('c1 c2')\n", "solve(c1 * a1 + c2 * a2, (c1, c2))" - ], - "id": "2H93zb7xKZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "EHupducaKZyy" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0, 1], [1, 1, 0], [0, 1, -1]])\n", "A.columnspace()" - ], - "id": "EHupducaKZyy" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "st6ROFo3KZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0, 1], [1, 1, 0], [0, 1, -1]])\n", "tmp = A.columnspace()\n", "basis = GramSchmidt(tmp, orthonormal=True); basis" - ], - "id": "st6ROFo3KZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0sLEYEtsKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "Q = Matrix.hstack(*basis)\n", "Q.T @ Q" - ], - "id": "0sLEYEtsKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "sa22Z2z2KZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 2], [1, 2], [0, 0]]); B = Matrix([[1, 0], [1, 1], [0, 1]])\n", "Qa, Ra = A.QRdecomposition()\n", "Qb, Rb = B.QRdecomposition()\n", "display(Qa, Ra, A == Qa @ Ra, Qb, Rb, B == Qb @ Rb)" - ], - "id": "sa22Z2z2KZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "owSm3gPpKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2], [1, 2], [0, 0]]); B = np.array([[1, 0], [1, 1], [0, 1]])\n", "Qa, Ra = linalg.qr(A)\n", "Qb, Rb = linalg.qr(B)\n", "Qa, Ra, np.allclose(A, Qa @ Ra), Qb, Rb, np.allclose(B, Qb @ Rb)" - ], - "id": "owSm3gPpKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "JHFZBDt0KZyz" - }, + "metadata": {}, "outputs": [], "source": [ "def qrd(A):\n", @@ -4689,15 +3586,12 @@ "\n", "A = Matrix([[1, 2], [1, 2], [0, 0]]); B = Matrix([[1, 0], [1, 1], [0, 1]])\n", "qrd(A), qrd(B) # 動作確認" - ], - "id": "JHFZBDt0KZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "h6CalT_YKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "def qrdn(An):\n", @@ -4713,15 +3607,12 @@ "\n", "A = np.array([[1, 2], [1, 2], [0, 0]]); B = np.array([[1, 0], [1, 1], [0, 1]])\n", "qrdn(A), qrdn(B) # 動作確認" - ], - "id": "h6CalT_YKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Ba3ob96TKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "B = Matrix([[1, 0], [1, 1], [0, 1]])\n", @@ -4729,15 +3620,12 @@ "(Q.T @ Q == eye(Q.shape[1]), # ①(厳密値を想定)\n", " R.is_upper, # ②(厳密値を想定)\n", " Q @ R == B) # ③(厳密値を想定)" - ], - "id": "Ba3ob96TKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "u22TgtbeKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "B = np.array([[1, 0], [1, 1], [0, 1]])\n", @@ -4745,41 +3633,32 @@ "(np.allclose(Q.T @ Q, np.eye(Q.shape[1])), # ①\n", " np.allclose(R, np.triu(R)), # ②\n", " np.allclose(Q @ R, B)) # ③" - ], - "id": "u22TgtbeKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "gEoHrBIoKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0], [1, 1], [0, 1]])\n", "A.T.nullspace()" - ], - "id": "gEoHrBIoKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZrJVRJfrKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 0], [1, 1], [0, 1]])\n", "linalg.null_space(A.T) # 正規直交基底" - ], - "id": "ZrJVRJfrKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0sD-wwnlKZyz" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0], [1, 1], [0, 1]])\n", @@ -4787,15 +3666,12 @@ "basis2 = GramSchmidt(A.T.nullspace(), orthonormal=True) # 直交補空間\n", "Q = Matrix.hstack(basis1, *basis2)\n", "Q, Q.T @ Q == eye(3)" - ], - "id": "0sD-wwnlKZyz" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-gaoY7TpKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 0], [1, 1], [0, 1]])\n", @@ -4803,25 +3679,17 @@ "basis2 = linalg.null_space(A.T) # 直交補空間\n", "Q = np.hstack([basis1, basis2])\n", "Q, np.allclose(Q.T @ Q, np.eye(3))" - ], - "id": "-gaoY7TpKZy0" + ] }, { "cell_type": "markdown", - "source": [ - "# 19 固有値と固有ベクトル" - ], - "metadata": { - "id": "pnP9bk9lKZy0" - }, - "id": "pnP9bk9lKZy0" + "source": "# 19 固有値と固有ベクトル", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "3O4VjmFKKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[5, 6, 3], [0, 9, 2], [0, 6, 8]])\n", @@ -4829,53 +3697,41 @@ "vals = [e[0] for e in eigs for _ in range(e[1])] # 固有値(順序不明)\n", "vecs = [v for e in eigs for v in e[2]] # 固有ベクトル(非正規)\n", "vals, vecs" - ], - "id": "3O4VjmFKKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1yPRi3DeKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "An = np.array([[5, 6, 3], [0, 9, 2], [0, 6, 8]])\n", "valsn, vecsn = linalg.eig(An)\n", "valsn, vecsn # 固有値(順序不明),固有ベクトル(正規)" - ], - "id": "1yPRi3DeKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "pvGXS1JXKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "V = Matrix.hstack(*vecs); A @ V == V @ diag(*vals)" - ], - "id": "pvGXS1JXKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1qusSue_KZy0" - }, + "metadata": {}, "outputs": [], "source": [ "np.allclose(An @ vecsn, vecsn @ np.diag(valsn))" - ], - "id": "1qusSue_KZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "FyP7WXjzKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "B = Matrix([[6, 7, 0], [7, 2, 0], [0, 0, 8]])\n", @@ -4886,15 +3742,12 @@ "vals = [vals[i] for i in idx] # 固有値(絶対値の降順)\n", "vecs = [vecs[i] for i in idx] # 固有ベクトルも並べ替える.\n", "vals # 結果の確認" - ], - "id": "FyP7WXjzKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lOk8N1sGKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "B = np.array([[6, 7, 0], [7, 2, 0], [0, 0, 8]])\n", @@ -4902,68 +3755,53 @@ "idx = np.argsort(-abs(vals)) # 絶対値の降順の位置\n", "vals, vecs = vals[idx], vecs[:, idx] # 絶対値の降順\n", "vals # 結果の確認" - ], - "id": "lOk8N1sGKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "tp6wokSLKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[5, 6, 3], [0, 9, 2], [0, 6, 8]]); n = A.shape[0]\n", "var('x'); solve((x * eye(n) - A).det(), x)" - ], - "id": "tp6wokSLKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "5ryrdkwYKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "(5 * eye(n) - A).nullspace()" - ], - "id": "5ryrdkwYKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "cMRA4mpCKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "S = Matrix([[2, 2, -2], [2, 5, -4], [-2, -4, 5]])\n", "Q, L, V = S.singular_value_decomposition()\n", "Q, L, S == Q @ L @ Q.T == V @ L @ V.T" - ], - "id": "cMRA4mpCKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "kSnv7arhKZy0" - }, + "metadata": {}, "outputs": [], "source": [ "Sn = np.array([[2, 2, -2], [2, 5, -4], [-2, -4, 5]])\n", "Q, lambdas, tV = linalg.svd(Sn); L = np.diag(lambdas)\n", "Q, L, np.allclose(Sn, Q @ L @ Q.T), np.allclose(Sn, tV.T @ L @ tV)" - ], - "id": "kSnv7arhKZy0" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "GJ-EpJq2KZy1" - }, + "metadata": {}, "outputs": [], "source": [ "S = Matrix([[2, 2, -2], [2, 5, -4], [-2, -4, 5]])\n", @@ -4974,15 +3812,12 @@ "Q = Matrix.hstack(*tmp) # ③\n", "L = diag(*vals) # ④\n", "Q, L, np.allclose(np.double(S), np.double(Q @ L @ Q.T)) # 近似的な比較" - ], - "id": "GJ-EpJq2KZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "nYEd75FzKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "Sn = np.array([[2, 2, -2], [2, 5, -4], [-2, -4, 5]])\n", @@ -4990,53 +3825,41 @@ "Q = linalg.qr(vecs)[0] # ②, ③\n", "L = np.diag(vals) # ④\n", "Q, L, np.allclose(Sn, Q @ L @ Q.T)" - ], - "id": "nYEd75FzKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ir3voOORKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "Matrix([[4, 2], [2, 1]]).is_positive_semidefinite" - ], - "id": "ir3voOORKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UZXtHTf_KZy1" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[4, 2], [2, 1]])\n", "all(e >= 0 for e in A.eigenvals(multiple=True))" - ], - "id": "UZXtHTf_KZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "nroXQzFdKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[4, 2], [2, 1]])\n", "np.all(linalg.eigvals(A) >= 0)" - ], - "id": "nroXQzFdKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "n_VonBZKKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "x1 = [1, 3, 6, 10]; y = [7, 1, 6, 14]; X = Matrix([x1, y]).T\n", @@ -5048,15 +3871,12 @@ "vecs = [re(v) for e in eigs for v in e[2]] # 固有ベクトル\n", "N(vecs[np.argmax(vals)].normalized()) # 最大固有値に対応する固有ベクトル\n", "# 最大固有値の位置の探索と,固有ベクトルの正規化が必要" - ], - "id": "n_VonBZKKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "X18vYLhdKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "x1 = [1, 3, 6, 10]; y = [7, 1, 6, 14]; Xn = np.array([x1, y]).T\n", @@ -5066,15 +3886,12 @@ "vals, vecs = linalg.eig(Sn)\n", "vecs[:, np.argmax(vals)] # 最大固有値に対応する固有ベクトル\n", "# 最大固有値の位置の探索が必要" - ], - "id": "X18vYLhdKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-Ier8_NYKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "_, L, V = A.singular_value_decomposition() # 特異値分解\n", @@ -5083,30 +3900,24 @@ "display(N(V[:, idx])) # 対応するVの列ベクトル(求めるもの)\n", "s2 = np.array(sorted([N(x)**2 for x in s], reverse=True)) # 特異値の2乗(降順)\n", "np.cumsum(s2) / sum(s2) # 累積寄与率(後述)" - ], - "id": "-Ier8_NYKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "UF3CbIBlKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "_, s, tV = linalg.svd(An) # 特異値分解\n", "print(tV.T[:, 0]) # Vの第1列(求めるもの)\n", "s2 = s**2 # 特異値の2乗\n", "np.cumsum(s2) / sum(s2) # 累積寄与率(後述)" - ], - "id": "UF3CbIBlKZy1" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "XarvnZaTKZy1" - }, + "metadata": {}, "outputs": [], "source": [ "X = np.array([[1, 3, 6, 10], [7, 1, 6, 14]]).T\n", @@ -5126,54 +3937,40 @@ "print(tV[0]) # 第1主成分(求めるもの)\n", "s2 = pca2.explained_variance_ratio_ # 特異値の2乗\n", "print(np.cumsum(s2) / sum(s2)) # 累積寄与率(後述)" - ], - "id": "XarvnZaTKZy1" + ] }, { "cell_type": "markdown", - "source": [ - "# 20 特異値分解と擬似逆行列" - ], - "metadata": { - "id": "VYw1WFn8KZy1" - }, - "id": "VYw1WFn8KZy1" + "source": "# 20 特異値分解と擬似逆行列", + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "NKSzkqAGKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0], [1, 1], [0, 1]])\n", "Ur, Sr, Vr = A.singular_value_decomposition()\n", "Ur, Sr, Vr.T, A == simplify(Ur @ Sr @ Vr.T)" - ], - "id": "NKSzkqAGKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "SJ2qycBnKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "An = np.array([[1, 0], [1, 1], [0, 1]])\n", "U, s, tV = linalg.svd(An)\n", "S = np.zeros(An.shape); r = len(s); S[:r, :r] = np.diag(s)\n", "U, S, tV, np.allclose(An, U @ S @ tV)" - ], - "id": "SJ2qycBnKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "-o0bCuKDKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "from skimage import io\n", @@ -5187,15 +3984,12 @@ "plt.subplot(1, 2, 1); plt.imshow(A, cmap='gray')\n", "plt.subplot(1, 2, 2); plt.imshow(B, cmap='gray')\n", "plt.show();" - ], - "id": "-o0bCuKDKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "eT3GoJOxKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "def exbasis(A): # tAの零空間(Aの列空間と直交)の基底を列ベクトルとする行列\n", @@ -5220,15 +4014,12 @@ " return Ur, Sr, Vr, U, S, V\n", "\n", "A = Matrix([[1, 0], [1, 1], [0, 1]]); svd2(A) # 動作確認" - ], - "id": "eT3GoJOxKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "AOg4zlVAKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "def svd2n(A, tol=10e-10):\n", @@ -5246,15 +4037,12 @@ " return Ur, Sr, Vr, U, S, V\n", "\n", "A = np.array([[1, 0], [1, 1], [0, 1]]); svd2n(A) # 動作確認" - ], - "id": "AOg4zlVAKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "Eo0wet4oKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "isOrtho = lambda A: np.allclose(\n", @@ -5270,15 +4058,12 @@ " isDiagDesc(Sr), isDiagDesc(S), # ③\n", " np.allclose(np.double(A), np.double(Ur @ Sr @ Vr.T)), # ④-1\n", " np.allclose(np.double(A), np.double(U @ S @ V.T))] # ④-2" - ], - "id": "Eo0wet4oKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "1zLIRsKfKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "isOrthon = lambda A: np.allclose(A.T @ A, np.eye(A.shape[1]))\n", @@ -5293,58 +4078,45 @@ " isDiagDescn(Sr), isDiagDescn(S), # ③\n", " np.allclose(np.double(A), np.double(Ur @ Sr @ Vr.T)), # ④-1\n", " np.allclose(np.double(A), np.double(U @ S @ V.T))] # ④-2" - ], - "id": "1zLIRsKfKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "ZyFok21HKZy2" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0], [1, 1], [0, 1]]); A.pinv()" - ], - "id": "ZyFok21HKZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "K3TwaTv_KZy2" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 0], [1, 1], [0, 1]]); linalg.pinv(A)" - ], - "id": "K3TwaTv_KZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "0l4gSYk5KZy2" - }, + "metadata": {}, "outputs": [], "source": [ "A = Matrix([[1, 0], [1, 1], [0, 1]]); b = Matrix([2, 0, 2])\n", "A.pinv() @ b" - ], - "id": "0l4gSYk5KZy2" + ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "lTAI2YIUKZy3" - }, + "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 0], [1, 1], [0, 1]]); b = np.array([2, 0, 2])\n", "linalg.pinv(A) @ b" - ], - "id": "lTAI2YIUKZy3" + ] } ], "nbformat": 4, @@ -5353,10 +4125,6 @@ "kernelspec": { "name": "python3", "display_name": "Python 3" - }, - "colab": { - "provenance": [], - "toc_visible": true } } -} \ No newline at end of file +}