~~ 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