Friday, July 20, 2012

What is randomness?


This post is rather a philosophical opinion than anything else. It's going to be a little bit provocative, so if you don't agree with anything, feel free to comment and share your thoughts about it.

Randomness doesn't exist in a strict way. Randomness is only a way to model problems. There's only cause and effect. Let's look at some examples:

When we cast dice, we say the result is going to be random. Using the classic rule of probability, we would say that we have a probability of 1/6 (16.6667%) of getting a specific result when casting a single die. We've model the problem using probabilities and measuring the uncertainty of getting a specific result.

But remember, that's only a model, a tool that represents the problem and gives us solutions (in this case the solutions are in form of probabilities). But what is actually happening isn't random. Let's simplify the problem.

If I put the die with the 3 face up, and only raise it half a millimetre from the floor, when I release the die I will get a 3 for sure!

That's cheating – you may think. And if we're playing risk or backgammon... you're right, I would be cheating. It's cheating because the experiment of casting a die here is quite controlled. I want to get a 3 and I've set the conditions of the experiment to favour my intention.

Well, the experiment isn't quite different if I cast the die at 1 metre from the floor. The only difference here is that I no longer control the experiment and it seems random. So, probabilities here are a good model for the problem, but they doesn't exist for real. Let's assume that I have a supercomputer that is able to take all the variables into account. They could be: initial height, die and floor surface irregularities, floor elasticity, die elasticity, Coriolis effect, wind speed and direction, rotation given by the hand (taking into account that it might be slightly shaking)... Actually I don't even know all the variables of the problem, and I don't think there exists such a computer, but let's assume. If the computer existed and the variables were known the problem can be solved and I would predict the result of the die perfectly.

The same happens when generating a pseudorandom number in a computer. It uses a deterministic algorithm. It might use a “random” seed, but if we know the seed, we know the result. Sometimes the seed it's taking by the time of the clock in milliseconds, but again if we know exactly when this happens, we predict the result.

When we shuffle a deck of cards its the same. If we were able to keep an eye while shuffling, we would know the order of cards in the deck perfectly.

However, we don't know all the variables of all these problems. That's why there seems to be a random entity. So, the probability of an event X occurring, it's just a measure of our ignorance. The higher the chances, the more certain we are that X is going to happen. When we add knowledge to the system, probability changes. This happens for example in the Monty Hall problem. Here you can see a scene of the film 21 in which the problem is discussed:



Final thought. I think that science is about acquiring knowledge of the universe. It's about identifying the cause that produces the observed effect. In the Renaissance, a physician would observe a group of sick people. He would see that all of them had drunk from the same water. Finally, he would conclude that that water is different from normal water. Doing some research he would find a virus in the water. He would then think: “if I kill the virus, health will return”. And using cause an effect he would pull science forward while healing his patients.

Nowadays, so called scientists often say: “the universe exists because of chance”, and what is worse, they get satisfied with their own answer. Whenever we speak of chance there's still knowledge to be unlocked. If you talk in terms of chances, it means that you've model the problem, but you don't fully understand the nature of if. And being satisfied with the answer means that you're comfortable being ignorant about something which definitely is not a trait of the western scientific tradition.

SUMMARY: I'm not saying that randomness is completely useless. In fact, I use it constantly! I love chance games and I love to calculate probabilities and get the best strategy. Randomness is useful because it provides us a way to manage the lack of knowledge. I accept not knowing the result of the die while playing risk, but I don't accept not knowing more important things about the universe and simplify everything with the sentence: “it's all chance”.

Monday, July 9, 2012

My first program using the Tkinter module


I'm starting to learn how to program with Tkinter (a module for Python). I've done some Graphical User Interface (GUI) programming before. I programmed some games using Pygame in the past. Sadly, those games were lost with all the information in my hard drive a few months ago... I would also need a little bit of time to restore my knowledge in the area...

Nevertheless, I wanted to try something different. I started a Tkinter tutorial, and as soon as I was able to do something I wanted to share it. I've programmed a virus.

Well, it's not exactly a virus. It behaves like a virus, but it doesn't compromise the system's security, it is more like a joke. Maybe this is the right time to say that destroying things doesn't make you a hacker, the same way a bomber isn't an architect and a guy who destroys cars isn't an engineer. This program is intended just for fun. Be responsible.

Being said that, let's see how it works.


As you can see in the video, whenever you close one of the windows, another window pops up and the previous one doesn't close. You can try pressing the red X, Alt+F4, etc. It won't work. In order to close the program, press space (it's a little backdoor, if you will).

Download the source code here (it's in a zip file).

If you're curious about what's the image I've used (the monkey), it's “Suzanne” a Blender model used for tests.

Monday, July 2, 2012

Risk simulation: conquering a territory


In previous posts I've showed the probabilities of succeeding in a single attack. In today's post, we'll see how difficult is to conquer an enemy territory. Just as a reminder, you can download the game instructions here. For this simulation, you can download my Python script here.

The simulation will tell you the chances of conquering a territory given the total amount of attackers and defenders. The defenders will be the number of armies in the enemy territory. The attackers will be a number of armies the attacking player wish to risk. So, if you have 7 armies in a territory that is going to attack an enemy territory but you want to make sure that you leave always 4 armies in your territory, you would set the attackers argument to 3 (i.e. 7 – 4 = 3).

During the simulation, I will assume that the attacker will attack with the maximum amount of armies he's willing to risk. The defender will act in the same way (we've concluded in previous posts that it's better to attack using as many dice as possible).

As I did in a previous post, here the simulation will be repeated a number of times to return more accurate data.

Let's prove our intuition. The probability of succeeding in a single attack with one dice against one defender is 41.6667%. So, if I run the program with one attacker and one defender I should conquer the territory a 41.6667% percent of the times or so.

07-02-2012_1


As you can see, the conquest rate is 41.682% (very close to 41.6667%) in a population of one million simulations. However, we already knew that (the result was predicted). Let's try more complex scenarios:

07-02-2012_2


As one should expect, the more armies defending a territory, the harder is going to be the conquest. I've been puzzled for a minute with the results on the losses. In the second to last simulation on the image above, the probability of conquering the territory is around 29.813%, but the defender expects more losses than the attacker. After thinking about it, I find it logical, because even thought the probability of conquest is below 50%, it is taken into account more cases in which the defender losses more armies (i.e. in case of conquest the defender will lose more armies and even if the attacker doesn't conquer the territory, there is also a case in which the defender loses 6 armies, more than the attacker).

We can also conclude that more armies involved in the attempt of conquest will be translated in more casualties (in both sides).

Finally, we already know that attacking with 3 dices will be a likely victory (whether the defenders uses 1 or 2 dices). In the past, I've experienced situations in which two players put many armies in only one territory menacing the other player but never attacking and expending many reinforcements in the business... They were wrong. Be the first one to attack. Let's see it with big numbers:


07-02-2012_3



It took my computer a while to finish this one! Conclusion: as we wanted to prove, even if the enemy outnumbers (not by a huge amount of armies) you, it's better to start the attack.

Saturday, June 23, 2012

Warehouse simulation


Today I'll share with you a warehouse simulation I had to do a month ago.

Let's consider a warehouse of a specific product whose sale price is 2€ per unit. Customer visits are distributed like a Poisson process (0.5 customers per hour) and the amount of products that each customer wants follows this distribution:

Request 1 unit 2 units 3 units 4 units
Probability 0.3 0.4 0.2 0.1


In order to satisfy the demand, the owner of the warehouse keeps a stock of products. When the inventory is low, he asks the distributor for more units. The orders policy is periodic. When the inventory is fewer than 30 and there aren't any pending orders, the owner asks for more units in a way that the inventory would end up having 100 units.

In each order there's a base price of 10€ (it doesn't matter the amount of units the owner asked). Additionally the cost per unit included in the order will depend on the amount of units asked. If the amount of units requested is less than 50, the price will be 1€/unit. However if the owner asks 50 or more units, the price would be 75 cents per unit.

The delivery time follows a normal distribution (with a mean of 48 hours and a standard deviation of 0.8 hours). The delivery will be paid as soon as it arrives. An agreement with the provider entitles us with a discount of 0.01% for every three hours of delay in the delivery. On the other hand, it will be 0.01% more expensive for every three hours it arrives sooner.

The owner of the warehouse spends 0.1€ per product each hour (due to physical storage, refrigeration, …).

If the customer asks for a greater amount than what is available, the warehouse sells all it has.

a) Simulate the behaviour of the warehouse during 5 months. Estimate the expected profits, the proportion of completely satisfied customers and the percentage of time the inventory remains zero. Let's assume the initial inventory is 70 units of product.

b) Represent graphically the evolution of the inventory level during the mentioned 5 months.

c) Try to identify what changes you could implement to improve the behaviour of the inventory model (get more profits and more satisfied customers).

For the first question, I've programmed a Python script. In order to run it, you need to have installed the NumPy library. I'll show you the output of a few simulations:


This is the graph that represents the evolution of the inventory. For this question, I commented the final part of the code (when the results of the simulation are printed) and put at the end of each iteration a line of code that printed in one line the stock available. Then, I used LibreOffice to plot the graphic. Maybe I should use better tools and techniques the next time.

06-23-2012_2


For the last question. There's an obvious way to reduce the losses: sell all the stock and forget about new orders. That way, we don't have order expenses and we keep maintenance to a minimum. Of course, we'll have a lot of potential losses (a lot of unsatisfied clients), but it's the best way to solve this problem that I've found so far (maintenance is too expensive!). Nevertheless, I challenge the reader to get better results. Mine are these:

06-23-2012_3

Don't change the product sale price, that's cheating.

Sunday, June 17, 2012

Organization and Time Management

This post is a little bit unusual. I won't speak about anything technical today. I've just finished reading "Getting Things Done" by David Allen. I've adopted and modified his system and I want to share my thoughts about it.

Why did I decide to read this book?

I had lots of things to do at the moment and I thought that knowing something about time management could help me. I googled "time management" and found this Wikipedia entry. The book was referenced there in the "Further reading" section.

I kept browsing and I realised that this man had created an organizational system. I thought it was interesting, so I decided to read his book.

First experiences

Once I had the book in my hands I realised I had to make 4 command line programs in C in only 10 days! It was a stressful time because those programs were mandatory if I wanted to pass the exam of that subject.

In those stressful moments, I read the first two chapters and applied their concepts. I used an empty notebook I had. It's not magic, but the system works. I felt less stress and I was starting to program. I didn't know at the time whether or not I was going to finish on time, but I was occupied with the task instead of complaining and feeling like a victim. By the way, I succeeded and believe me, it feels great.

What is the GTD method?

I suggest you read this Wikipedia entry first. The core principle of GTD is that if you put all the "stuff" (as the author writes) in a system out of your mind, your mind will be freer and then you feel less stress and work in a more efficient way.

In order to accomplish that, GTD divides workflow into several stages. The first stage, "collection", deals with this principle. The following stages will tell you how to manage every piece of information that you've introduced into your system.

My implementation

I'm not an executive, I'm just a student. The book is targeted towards executives. The book assumes that the reader receives hundreds of emails a day. I usually receive up to 12 or so. So all the email management thing is useless for me (at least for the time being).

It also assumes everyone has an incredible amount of things that require their attention. It talks about making an "in" tray in which you stack all the stuff for later processing. Until now, I have never needed such an amount of space, and I put everything down in my notebook.

I've really enjoyed applying the "next action" principle. For example, I want to buy a present for someone. In order to accomplish this, the book suggests you have to question yourself "what's the next action?" If X is a girl, maybe I should ask her sister for advice... That's the idea. That way, things that seem too big to be executed in a particular moment are split into several smaller actions and that way the task is more manageable.

I joined two stages of the workflow: "process" and "organize" because my mind thinks that way.

I also have noticed that I have activities that I do everyday, those are out of the system. If you pray, brush your teeth, have a shower and sleep everyday I don't see the point in putting those activities into the organizational system. They're already automatic and thus handled.

I have also applied another principle that I read about in another place. Start every activity you can. It's the best way to avoid procrastination. If you have an activity that lasts 3 hours but you only have 1, it is better to start as soon as you can. That way, the following day when you're going to tackle the activity, there will be only 2 hours left.

I like the approach that the book gives to brainstorming. It says something like "first capture all the ideas without judging them, even the silliest one. Once there aren't more ideas, start judging". Spanish people (me included) definitely try to accomplish every step at once. I hope I'll be able to overcome this bad habit.

Finally, before I read this book, when I was going though a crisis, I started to write all that occurred to me in my whiteboard. I keep doing this even now and I also use it as a "collection" tool. That way, I have the ideas in front of me without forgetting anything.

Saturday, June 9, 2012

Risk: attacker VS defender. Real probabilities.

Quick post.

A couple of days ago I posted a simulation program about Risk. I've made today a script that returns the true probabilities of the simulated events. That way we'll be able to see how close to reality the obtained results are.

The script is based on the classical definition of probability (count favourable cases and divide them by the total possible cases).

Screen capture:

06-09-2012_1


Download the script here.