Tuesday, January 31, 2012

Monte Carlo Simulation in Python

Suppose you flip coins for a certain times, say n. You want to know your chance of getting certain number of heads in a row, say k heads in a row.

Let's start with the function that'll do a certain number of coin flips:
Let's do 10 flips right away:

Let's suppose you get 1 euro if you get the given number of heads in a row while doing a certain number of flips. Here's the function that'll calculate your payoff:
Let's try our payoff function with doing 10 flips and getting payed for 3 heads in a row. sometimes it is 0, sometimes it is 1:
payoff(10, 3)
payoff(10, 3)

Now, what is your chance to get your 1 euro for 3 heads in a row from 10 flips? This brings us to the Monte Carlo simulation, which I'wont describe here at all, just give you a function which answers our question regarding the chance:
Running with a million iteration gives us a good approximation for the chance:
monte_carlo_solve(10, 3, 1000000)

This post was inspired by a similar post by Remis.

Wednesday, January 4, 2012

How to use wildcard in Google search

This example demonstrates how to use a wildcard in Google search.

Lets suppose we have this idiom: "if you have a somebody for a friend, you don't need an enemy", but we do not remember exactly what that  somebody would be.

Let's ask Google: "If you have a * for a friend, you don't need an enemy".

And here we go. For the record, at the time being, Google thinks this somebody is either a Hungarian or a politician. I assume, in fact it is a Hungarian politician :)