Yoichi Hirai's Log
https://yoichihirai.com/loggy/
Recent content on Yoichi Hirai's Log
Hugo  gohugo.io
enus
Copyright © 2019  Yoichi Hirai
Thu, 19 Dec 2019 08:57:22 +0100

Picking an Activity
https://yoichihirai.com/loggy/posts/pickactivity/
Thu, 19 Dec 2019 08:57:22 +0100
https://yoichihirai.com/loggy/posts/pickactivity/
<p>I have several options to spend 5 minutes. I've tried those options and I've recorded whether each trial was good or bad.</p>
<pre><code>>>> activities = [
{ "name": "duolingo", "good": 7, "bad": 5 },
{ "name": "topics in algebra", "good": 10, "bad": 2 },
{ "name": "byzantine history", "good": 10, "bad": 3 },
{ "name": "journaling", "good": 16, "bad": 2 },
{ "name": "kahn academy", "good": 8, "bad": 5 }
]
</code></pre><p>Here I'm assuming that each activity is a Bernoulli, and their parameters follow Beta distribution of a particular form \(\Beta(g + 1, b + 1)\), where \(g\) and \(b\) denotes the numbers of good and bad experiences.</p>
<p>For each activity, I can pick a parameter randomly from the Beta distribution.</p>
<pre><code>>>> def pick_param(activity):
return beta.rvs(activity["good"] + 1, activity["bad"] + 1)
</code></pre><p>Given a list of activities, I can draw such parameters and pick the best one.</p>
<pre><code>>>> def pick_activity(activities):
params = map(pick_param, activities)
pairs = zip(params, activities)
pairs.sort(key=lambda pair: pair[0])
return pairs[1][1]["name"]
</code></pre><p>With this, I can pick an activity</p>
<pre><code>>>> from scipy.stats import beta
>>> pick_activity(activities)
"kahn academy"
</code></pre>

Picking a value from Beta Distribution
https://yoichihirai.com/loggy/posts/betadistr/
Mon, 16 Dec 2019 19:37:48 +0100
https://yoichihirai.com/loggy/posts/betadistr/
<p>Now I want to pick a random sample from a beta distribution.</p>
<p>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”. <a href="https://eankeen.github.io/blog/posts/renderlatexwithkatexinhugoblog/">This one</a> 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).</p>
<p>I <a href="https://yoichihirai.com/loggy/posts/myfirstpost/">wanted to organize my life somehow</a>. 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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>I install <a href="https://www.scipy.org/install.html#distributions">SciPy</a>.</p>
<pre><code>❯ python m pip install user numpy scipy matplotlib ipython jupyter pandas sympy nosew
</code></pre><p>And then, it's easy to generate random numbers:</p>
<pre><code>python
>>> from scipy.stats import beta
>>> beta.rvs(2,3)
0.5206578473982338
>>> beta.rvs(2,3)
0.1411848825884534
>>> beta.rvs(2,3)
0.35268037095303756
</code></pre><p>Next, I'll play with a real example on the Python console.</p>

Life Organization
https://yoichihirai.com/loggy/posts/myfirstpost/
Tue, 03 Dec 2019 16:36:31 +0100
https://yoichihirai.com/loggy/posts/myfirstpost/
<p>I don't know the title of this post.</p>
<p>Suddenly I have time. I need to structure my life again. I was clever enough to book an intensive German course before suspending my work, so my life has some structure. That's a temporary solution. The course ends this week.</p>
<p>I dig around. When something makes more sense, I dig deeper. I also don't want to miss other possibilities. So I'm facing some exploration vs exploitation problem. I heard there are bunch of algorithms like UCB1.</p>
<p>I can make a special software for managing my life based on UCB1. There is going to be a list of activities, each taking 40 minutes. After each try, I say it was good or bad. The software chooses the next activity, trying to hit the balance between having good time (exploitation) and trying newer activities (exploration).</p>
<p>I'm not sure about one thing. Is UCB1 a good algorithm in my use case? I'd be adding more and more activities while I'm figuring things out. I'd be reading new codebase, so I'd be adding another entry in the list. If I add more and more activities on the go, I might be spending all the time on the newer additions. That's not good. To be honest, I tried organizing my life according to UCB1 a while ago, and I had precisely this problem.</p>
<p>In that case, perhaps “randomized probability matching” might feel fairer.
Assuming each activity is a Bernoulli distribution, how can I learn the parameters? <a href="https://www.projectrhea.org/rhea/index.php/Bayes_Parameter_Estimation_with_examples#Bayesian_Parameter_Estimation:_Bernoulli_Case_with_Beta_distribution_as_prior">It looks like it's doable, assuming that the parameter follows Beta distribution</a>.</p>
<p>In the next post, I'll find a convenient implementation of this parameter estimation. I'd be surprized if there is none.</p>
<p>The title of this post is “life organization.”</p>