How to sample from any probability density function in Python

~~ MCMC should be done! There is no such thing as Tsukkomi.

As you know, scipy has many predefined probability density functions that you can use to easily sample, plot probability density functions, and more.

For example

stats.norm.rvs(loc=50, scale=20, size=1000)

Then you can get 1000 samples from a normal distribution with a mean of 50 and a standard deviation of 20.

x = np.linspace(0, 100, 100)
px = stats.norm.pdf(x, loc=50, scale=20)
plt.plot(x, px)

Then you can also illustrate the normal distribution with $ 0 <x <100 $.

So how do you sample from your own probability density function that is not predefined in scipy? In the case of discrete distribution, it is introduced in article of @ yk-tanigawa, so here we will deal with the case of continuous distribution.

As an example, assuming that the normal distribution is not defined in scipy.stats (it is really defined), here is an example of defining it yourself and sampling it. You just write the function you want to define in the _pdf function, inheriting from rv_continous.

from scipy import stats
import math

class gaussian(stats.rv_continuous):

    def _pdf(self, x, mu, sigma):
        normalize_factor = 1.0/(2.0*math.pi*sigma**2)**(1/2)
        px = normalize_factor * math.exp(-(x-mu)**2/(2*sigma**2))

        return px

gaussian = gaussian(name="gaussian", a=0.0)    
sample_from_gaussian = gaussian.rvs(size=1, mu=10.0, sigma=1.0) 

Please note that the probability density function defined by yourself needs to be standardized. If you haven't standardized it, you can use Reject sampling.

(But if you look at the Documents, there are many reasons why a = 0 is needed. I don't know ...)

Recommended Posts

How to sample from any probability density function in Python
How to develop in Python
How to download files from Selenium in Python in Chrome
Execute Python function from Powershell (how to pass arguments)
[Python] How to call a c function from python (ctypes)
[Python] How to do PCA in Python
How to slice a block multiple array from a multiple array in Python
How to extract any appointment in Google Calendar with Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to access wikipedia from python
How to use python zip function
How to handle Japanese in Python
How to get a string from a command line argument in python
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to update Google Sheets from Python
[Itertools.permutations] How to put permutations in Python
Sample script to trap signals in Python
[python] How to use __command__, function explanation
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to access RDS from Lambda (python)
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
Convert from Markdown to HTML in Python
Get Precipitation Probability from XML in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
[Python] How to put any number of standard inputs in a list
How to get a value from a parameter store in lambda (using python)
How to use the C library in Python
How to receive command line arguments in Python
Automatically register function arguments to argparse in Python
How to open a web browser from python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Study from Python Hour7: How to use classes
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to get results from id in Celery
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
Summary of how to use MNIST in Python
How to specify TLS version in python requests