Write beta distribution in Python

I feel like I missed the wave of DBDA, so I've been reading it by myself recently. DBDA uses R as an example, but I wrote it in Python. I will omit the explanation of the feelings of the ceremony because I think there is information on the textbook and the Web.

β distribution

figure_1.png

code

beta.py


#!/usr/bin/python
#-*- coding:utf8 -*-
"""
DBDA FIGURE 5.1
"""
import math
import numpy as np
from pylab import *

def beta(theta, a, b):
    """
    beta distribution

    theta^{alpha-1} * (1-theta)^{beta-1}
    ------------------------------------
               B(alpha, beta)

    B(alpha, beta)
      gamma(a) * gamma(b)
    = -------------------
        gamma(a + b)
    """
    B = math.gamma(a) * math.gamma(b) / math.gamma(a + b)
    return (theta ** (a - 1)) * ((1 - theta) ** (b - 1)) / B

if __name__ == '__main__':
    thetas = np.arange(0, 1, 0.01)
    params = [(0.5, 0.5), (1., 0.5), (2., 0.5), (3., 0.5), (4., 0.5),
              (0.5, 1.), (1., 1.), (2., 1.), (3., 1.), (4., 1.),
              (0.5, 2.), (1., 2.), (2., 2.), (3., 2.), (4., 2.),
              (0.5, 3.), (1., 3.), (2., 3.), (3., 3.), (4., 3.),
              (0.5, 4.), (1., 4.), (3., 4.), (3., 4.), (4., 4.)
             ]

    axisnum = 0
    for (a, b) in params:
        axisnum += 1
        subplot(5, 5, axisnum)
        betas = [beta(theta, a, b) for theta in thetas]
        plot(thetas, betas)
        xlim(0, 1.)
        ylim(0, 3.)
        text(0.5, 2.7, 'a=%.1f b=%.1f' % (a, b),
                horizontalalignment='center', verticalalignment='center')
        xlabel('theta')
        ylabel('p(theta)')
    show()

Recommended Posts

Write beta distribution in Python
Logistic distribution in Python
Write Python in MySQL
Write Pandoc filters in Python
Write python in Rstudio (reticulate)
Generate U distribution in Python
Write a binary search in Python
Write JSON Schema in Python DSL
LightGBM scores fitted in beta distribution
Mixed normal distribution implementation in python
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Write A * (A-star) algorithm in Python
Write selenium test code in python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Write C unit tests in Python
Write documentation in Sphinx with Python Livereload
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Write the test in a python docstring
Epoch in Python
Discord in Python
Write a short property definition in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Write O_SYNC file in C and Python
N-Gram in Python
Programming in python
Write a Caesar cipher program in Python
Plink in Python
Constant in python
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Write python modules in fortran using f2py
Write a simple Vim Plugin in Python 3
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python