Yoichi Hirai's Log

Picking a value from Beta Distribution

Now I want to pick a random sample from a beta distribution.

Before talking about beta distribution, it's a good idea to make sure that I can write LaTeX equations. How does it work? I search for “Hugo latex”. This one looks promising. So I do that. And I should see a nice \(\Beta(\alpha, \beta)\)$ here. I did this, and already 30 minutes have passed (the linked post had a small mistake in the code).

I wanted to organize my life somehow. I'm thinking about an app “Your 40 minutes”. I could list many kinds of activities that take 40 minutes. The app chooses one of them (in a reasonable way), and start ticking for 40 minutes. After the 40 minutes the app asks me, whether it was good or bad, or I didn't actually do it.

I chose to model each activity as a biased coin flip. I cut corners and just think the bias of the coin follows Beta distribution. I cut corners even more, and say, after \(m\) good trials and \(n\) bad trials, the bias follows a \(\Beta(n+1, m+1)\) distribution.

What do I do with these distributions? I can get a bias number randomly. From \(\Beta(1,1)\) distribution, I get a real number between 0 and 1 uniformly. On another Beta distribution, I get more skewed results. On each activity I pick the lottery and get the bias number. And then, the app is going to present the activity with the highest bias number.

It's possible that any of the activity is chosen, but an activity with better track records is more likely chosen. There is also some beginner's luck involved.

I can start with an interface first and fill in the functionality, but I don't. I just want to make sure that the calculation works. I want to make sure that I can pick samples from \(\Beta(\alpha, \beta)\) distributions.

I install SciPy.

❯ python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nosew

And then, it's easy to generate random numbers:

python
>>> from scipy.stats import beta
>>> beta.rvs(2,3)
0.5206578473982338
>>> beta.rvs(2,3)
0.1411848825884534
>>> beta.rvs(2,3)
0.35268037095303756

Next, I'll play with a real example on the Python console.