I implemented the inverse gamma function in python

Introduction

I want to sample from the inverse gamma function in Python, but it is implemented in scipy invgamma [ ^ 1] was designed so that one of the parameters could not be tampered with (the parameter β of the function that appears in the next section is fixed at β = 1: please let me know if anyone knows the reason). I made it after practice (both posting to python and Qiita) (including memorandum).

What is the inverse gamma function?

The inverse gamma function is a continuous probability distribution expressed in the following form.

f(x,α,β) = \frac{β^α}{Γ(α)}x^{-α-1}e^{\frac{-β}{x}}   (x>0)\\
(However, Γ(α) = \int_{0}^{∞}x^{α-1}e^{-x}Gamma function represented by dx)

I'm not sure why scipy's invgamma is fixed at β = 1 (I don't know much about anything other than my field ...), but I use MCMC (Markov Chain Monte Carlo) to priori-distribute the variance of the normal distribution. I wrote this article this time because I want to use other than β = 1 when using it. If you want to know more about MCMC, please see @ pynomi's article [^ 2].

I actually wrote it

from scipy import stats
from scipy.special import gamma
import numpy as np

###Probability density function of inverse gamma distribution###
class invgamma(stats.rv_continuous):

    def _pdf(self, x,alpha,beta):
        
        px = (beta**alpha)/gamma(alpha)*x**(-alpha-1)*np.exp(-beta/x)
        
        return px

###Sampling from inverse gamma function###
invgamma = invgamma(name="invgamma", a=0.0)    
sample_from_invgamma = invgamma.rvs(size=1, alpha = 1, beta = 1.0) 

Like this. (OS: Windows10, Python3.7, Development environment: Operation confirmed with Spyder, when the random number is fixed with β = 1, the sampling value matches the invgamma of scipy, so I think it is probably correct.)

Quote

In writing this article and code, I also referred to @ physics303's article [^ 3]. Thank you very much.

Recommended Posts

I implemented the inverse gamma function in python
I wrote the stack in Python
I tried to implement the mail sending function in Python
OR the List in Python (zip function)
I implemented Cousera's logistic regression in Python
[Python] I tried substituting the function name for the function name
I implemented Robinson's Bayesian Spam Filter in python
Implemented SimRank in Python
I tried the least squares method in Python
I implemented Python Logging
Implemented Shiritori in Python
I implemented Human In The Loop ― Part ① Dashboard ―
I want to display the progress in Python!
[Python] I want to know the variables in the function when an error occurs!
I want to get the file name, line number, and function name in Python 3.4
I implemented breadth-first search in python (queue, drawing self-made)
I tried to graph the packages installed in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
I want to use the R dataset in python
I implemented a Vim-like replacement command in Slackbot #Python
What does the last () in a function mean in Python?
I wrote python in Japanese
Create a function in Python
Download the file in Python
Use callback function in Python
Find the difference in Python
ntile (decile) function in python
About the enumerate function (python)
Nonlinear function modeling in Python
Draw implicit function in python
Sudoku solver implemented in Python 3
Immediate function in python (lie)
I understand Python in Japanese!
What I learned in Python
6 Ball puzzle implemented in python
I downloaded the python source
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
I tried "gamma correction" of the image with Python + OpenCV
I checked the Python package pre-installed in Google Cloud Dataflow
I tried the accuracy of three Stirling's approximations in python
Have the equation graph of the linear function drawn in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
I tried programming the chi-square test in Python and Java.
I implemented Donald Knuth's unbiased sequential calculation algorithm in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
I implemented N-Queen in various languages and measured the speed
I also tried to imitate the function monad and State monad with a generator in Python
Implement R's power.prop.test function in python
sort warning in the pd.concat function
Getting the arXiv API in Python
Function argument type definition in python
I got lost in the maze
Implemented image segmentation in python (Union-Find)
Included notation in Python function arguments
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Write AWS Lambda function in Python
I participated in the ISUCON10 qualifying!
[Python] I implemented peripheral Gibbs sampling
Measure function execution time in Python