{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5eab6b62-76e1-40d8-9317-d88b6fc3869a", "metadata": {}, "outputs": [], "source": [ "using Distributions\n", "using StatsPlots" ] }, { "cell_type": "markdown", "id": "e51b05fa-2629-4d3c-aeb8-e6bfbc684aeb", "metadata": {}, "source": [ "負の二項分布の確率函数は\n", "\n", "$$\n", "P(k) = \\binom{k+r-1}{k} p^r (1 - p)^k \\quad (k = 0,1,2,\\ldots)\n", "$$\n", "\n", "であり, $k$ は成功確率 $p$ のBernoulli試行で初めて $r$ 回成功するまでの失敗する回数を意味している. \n", "\n", "これのパラメータを $r = \\alpha$, $p = 1/(N\\theta)$ とおくと, $N$ が大きいとき $k/N$ は近似的に分布 $\\operatorname{Gamma}(\\alpha, \\theta)$ に従う." ] }, { "cell_type": "code", "execution_count": 2, "id": "f79a0a32-b9a3-40c9-8d5a-94afa2c9016d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "plot_scaled_negbin (generic function with 1 method)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function plot_scaled_negbin(; α = 3, θ = 5, N = 100)\n", " gamma = Gamma(α, θ)\n", " negbin = LocationScale(0, 1/N, NegativeBinomial(α, 1/(N*θ)))\n", " \n", " xmax = quantile(gamma, 0.995)\n", " plot(; legend=:bottomright)\n", " plot!(x -> cdf(negbin, x), 0, xmax; label=\"NegativeBinomial(r = α, p = 1/(Nθ)) scaled by 1/N\")\n", " plot!(x -> cdf(gamma, x), 0, xmax; label=\"Gamma(α = $α, θ = $θ)\", ls=:dash)\n", " title!(\"cdf of scaled NegativeBinomial distribution for N = $N\"; titlefontsize=10)\n", "end" ] }, { "cell_type": "code", "execution_count": 3, "id": "cce69adf-bf2e-44d4-b797-dbbbf2fcd1e5", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_scaled_negbin(N = 1)" ] }, { "cell_type": "code", "execution_count": 4, "id": "8f65fe50-582f-4f04-bf90-46f5ef593535", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_scaled_negbin(N = 10)" ] }, { "cell_type": "code", "execution_count": 5, "id": "ee5be19d-a81d-4e3f-a49b-09b69b2ff0a1", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_scaled_negbin(N = 100)" ] }, { "cell_type": "code", "execution_count": 6, "id": "fe144d25-7b6a-46ee-bba2-4d296b1bdc9d", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "α = 3\n", "θ = 5\n", "N = 100\n", "gamma = Gamma(α, θ)\n", "negbin = LocationScale(0, 1/N, NegativeBinomial(α, 1/(N*θ)))\n", "X = rand(negbin, 10^5)\n", "\n", "xmax = 60\n", "stephist(X; norm=true, bin=0:xmax, label=\"NegativeBinomial(r = α, p = 1/(Nθ)) scaled by 1/N\")\n", "plot!(gamma, 0, xmax; label=\"Gamma(α = $α, θ = $θ)\", ls=:dash)\n", "title!(\"sample of scaled NegativeBinomial distribution for N = $N\"; titlefontsize=10)" ] }, { "cell_type": "markdown", "id": "5dbe9d40-5993-4524-8210-be6ec220df2b", "metadata": {}, "source": [ "二項分布の確率函数は\n", "\n", "$$\n", "P(k) = \\binom{n}{k} p^k (1 - p)^{n-k} \\quad (k = 0,1,\\ldots,n)\n", "$$\n", "\n", "であり, $k$ は成功確率 $p$ の $n$ 回のBernoulli試行中の成功回数を意味している.\n", "\n", "これのパラメータを $p = \\lambda/n$ とおくと, $n$ が大きいとき $k$ は近似的に分布 $\\operatorname{Poisson}(\\lambda)$ に従う." ] }, { "cell_type": "code", "execution_count": 7, "id": "033591c8-ee93-472e-b5a9-094f283d2a12", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "plot_bin (generic function with 1 method)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function plot_bin(; λ = 5, n = 100)\n", " poisson = Poisson(λ)\n", " bin(n) = Binomial(n, λ/n)\n", " \n", " xmax = quantile(poisson, 0.995)\n", " plot(; legend=:bottomright)\n", " plot!(x -> cdf(bin(n), x), 0, xmax; label=\"Binomial(n, p = λ/n)\")\n", " plot!(x -> cdf(poisson, x), 0, xmax; label=\"Poisson(λ = $λ)\", ls=:dash)\n", " title!(\"cdf of Binomial distribution for n = $n\"; titlefontsize=10)\n", "end" ] }, { "cell_type": "code", "execution_count": 8, "id": "c81fde45-267d-45bc-8167-bd3e154faead", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_bin(n = 5)" ] }, { "cell_type": "code", "execution_count": 9, "id": "d1223ea4-27b3-4974-9066-5274efeac402", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_bin(n = 10)" ] }, { "cell_type": "code", "execution_count": 10, "id": "5c2924cb-3b6d-4ee3-ba3a-10d13e00ac87", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_bin(n = 100)" ] }, { "cell_type": "code", "execution_count": null, "id": "decd35c6-973a-4667-8b51-8c8cfaa98594", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "encoding": "# -*- coding: utf-8 -*-", "formats": "ipynb,jl:hydrogen" }, "kernelspec": { "display_name": "Julia 1.7.0", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.0" } }, "nbformat": 4, "nbformat_minor": 5 }