Our social:

Latest Post

Mostrando entradas con la etiqueta proyectos. Mostrar todas las entradas
Mostrando entradas con la etiqueta proyectos. Mostrar todas las entradas

miércoles, 18 de octubre de 2017

martes, 15 de agosto de 2017

Simulating the Monty Hall Problem with a Monte Carlo method. The paradox is real!

The Monty Hall problem is a brain teaser, in the form of a probability puzzle (Gruber, Krauss and others), loosely based on the American television game show Let's Make a Deal and named after its original host, Monty Hall. The problem was originally posed (and solved) in a letter by Steve Selvin to the American Statistician in 1975 (Selvin 1975a), (Selvin 1975b). It became famous as a question from a reader's letter quoted in Marilyn vos Savant's "Ask Marilyn" column in Parade magazine in 1990 (vos Savant 1990a):

Vos Savant's response was that the contestant should switch to the other door (vos Savant 1990a). Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their initial choice have only a 1/3 chance.
(link: https://en.wikipedia.org/wiki/Monty_Hall_problem)

 Many scientist, including Paul Erdös, refused to accept the paradox, until they were convinced by a simulation.

In this post I want to show a project that I wanted to do for a long time: to test the Monty Hall Problem with a computer simulation to analyze the results. Surprisingly, when the number of simulations was big enough (around a 1000 simulations) the results tended to aproximate to the 1/3 probability when the player decided to stay and tended to 2/3 when the player decided to switched the door. The paradox is real.

What I did is basically a Monte Carlo method, which is a type of stochastic model, to prove the paradox.

You can find the code of the simulation I developed in C language in the following link. And you can download this executable file to test it yourself (30kb). It's not perfect, there's a lot of optimization to be done, but it works. And, surprisingly for me, the paradox is true. I was one of the guys who couldn't believe the paradox and thought there was some kind of gambler's biase. But I was wrong, the simulation doesn't lie. So if you're a non-believer like me, this simulation and the many others around the WWW, should be enough to convince you. Science is a lot of things, but at the end of the day, science is the acceptance of what it works and the rejection of what it doesn't.

Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. Their essential idea is using randomness to solve problems that might be deterministic in principle. They are often used in physical and mathematical problems and are most useful when it is difficult or impossible to use other approaches. Monte Carlo methods are mainly used in three distinct problem classes:[1] optimization, numerical integration, and generating draws from a probability distribution.

The Monte Carlo method was invented by Stanislaw Ulam in 1940 while he was working in Project Manhattan, in Los Alamos, USA (the nuclear bomb project). Ulam was playing a famous card game, the Solitaire, when he wondered what was the best strategy to solve the game.

The best approach, would be to find a deductive probability solution, but he decided to test the game as many times as possible, count the inputs and outputs, and figure out with statistics the best inputs to obtain the best outputs. Of course, in order that the experiment is successful, you need a sample of simulations as big as you can, otherwise the results won't be accurate.

The Monte Carlo method is not a valid method in mathemathics to prove anything. As we know, maths are tricky, inductive solutions are not always true while deductive solutions tend to be much better since it respects the scientific method,  which is based in the hypothetical deductive reasoning principle, which has demonstrated to be the best solution in the modern era to solve problems.

If you are too lazy to test the simulation by yourself, here I'll show you some of the results:

As you can see, the more number of simulations, the more accurate result. For a 100 million simulations, whe get a 33.33206% (good approximation of 1/3) and 66.666794% (good approximation of 2/3).


jueves, 10 de agosto de 2017

Free home VPN server with PiVPN: taking advantage of your old Raspberry Pi.

PiVPN is a project to install a Virtual Private Network in your Raspberry Pi. It's very easy to install, since it only requires to execute a script and follow the instructions in http://www.pivpn.io Use it along a software like OpenVPN to use it everywhere.



Insert this command in the console: curl -L https://install.pivpn.io | bash

In the summer of 2016, I tried several times to install a VPN in my raspberry pi. All the process is a long task, it may cost you several days. With PiVPN you can do the whole process in a few hours. It is recommended to use a (free) DDNS service in the case you have a dynamic IP.

Remember that in order to have the VPN 24/7 you'll have to have the raspberry pi connected 24/7. The good part of embedded systems like raspberry pi is that is has a low power consumption (2W the first model)

It's a great option as a home VPN server and good use for your unused raspberry pi.

For what can a home VPN server be useful?

1) Secure connection between to nodes of the internet. Use to enter a private intranet from the internet. This can be done, for instance, in business environments, or in the case you want to access a local net (intranet) from the internet. I've used it for some university services as well, for instance: access to Sciencedirect paid services from my computer connected to a university computer with the premium subscription to Sciencedirect. With that I can access to all the scientific papers without the requirement to physically be in the university. If you are a worker and you are ill, or you are pregnant, you can use it as well to work from home. By the way, Disney Pixar's Toy Story was deleted by a wrong linux command by a worker, and it was saved thanks to a pregnant woman who worked at home months after his chilbirth through a VPN and had all the data stored in his PC.

2) Connect to some forbidden web services in some countries. e.g. in China Facebook, Google and Twitter are forbidden by the great firewall, so if you own a VPN in your country, you can use it and save a lot of money for paid vpn's. (6$ a month). *

3) Connect to the internet in a safe way in public wifi connections. A very popular man-in-the-middle attack on airports, bars and cafeterias is a free wifi node where the owner sniffs all of your traffic with a software like wireshark, so he can detect all your traffic and inputs like passwords, names etc. With the VPN all the connections through internet are encrypted in a private, secure connection, so it's going to be much harder to steal your data.

Some economic analysis:

Considering a raspberry pi at full work consumes 2W*h, and in my country's kw*h is something like 12 cents of € per kw*h, the electricity cost of the raspberry  (considering is at full work 24h a day, worst case scenario) it's only 10 cents of euro yearly!
Ok, you need to pay off the 35€ cost of the raspberry pi. Let's suppose you use the Raspberry Pi for 5 years as a home VPN server. You have a yearly cost of 7.10€ per year, but remember, your Raspberry Pi could be used for other tasks, like a bittorrent server, an open cloud server, or even a light web server.

Your hoe VPN could be used to host more than one user, so, for instance, you can sell your service to a friend or someone else for a below-market price and get some profits from it. If you already bought a raspberry pi and you aren't giving it any use, it could be a nice way of taking advantage of it. It's a great option if you already have an internet connection at home without a lot of use and you already have a raspberry pi (and it's a fixed cost you can't save).

* For a VPN, is very important the download and upload capacity of your home internet. e.g: 100 Mbit/s download and 10 Mbit/s upload would be bad because it would mean you could only get a 10 Mbit/s download from your vpn node, this is something like 1.25 MBytes/s.

martes, 21 de febrero de 2017