Fair throw from an unfair coin

Imagine you are given an unfair coin. You don’t know how unfair it is, nor which side (head or tails) is produced more frequently than the other. How do you perform a fair throw with it ?

Enter the von Neumann method. It’s really simple: throw the coin twice. If the results are the same, discard the result; if the results are different, choose the first one.

How does it work ? It’s rather easy. Suppose the coin returns head 90 % of the times, and the remaining 10 % returns tail. We give actual probabilities here for the sake of discussion, but you don’t need to know how unfair is the coin in order to apply this method. If you throw the coin two times

  • the probability of throwing two times head is 0.9 * 0.9 * 100 = 81 %
  • of getting two tails is, of course 0.1 * 0.1 * 100 = 1 %.
  • the probability of throwing tail followed by head is 0.1 * 0.9 * 100 = 9 %
  • and finally, the probability of throwing head followed by tail is 0.9 * 0.1 * 100 = 9 %.

You can see how the probabilities of the mixed events (Tail-Head and Head-Tail) is the same. If you discard homogeneous cases Head-Head and Tail-Tail, accept only the mixed results and pick one of the throws as the right one, you will obtain the same probability of obtaining a head and tail, that is 50/50, a fair coin. The only problem with this method is that you may have to discard a very high number of throws. The method will be slower to yield a result, but the result is guaranteed to be fair. In the limit of a completely unfair coin that returns the same face every time, you will never get a result. I wonder how this can be generalized to an unfair n-faces die.

Posted in Probability. Comments Off »

Random number generator hardware

Suppose you want to generate a large number of random dice rolls for a computer program. How do you do it ? With a robotic dice rolling machine, of course

Why would anyone need such device, I hear you ask? Well, software random number generators are technically not perfectly random, and if you have to reassure a crowd of people complaining about your rolls’ randomness, this solution is straightforward, creative, and a real pleasure to read in its gory and unexpected technical issues.

Posted in Hardware, Probability, Statistics. Comments Off »

Why Gaia?

Choosing the name of my new blog was a problem I worked on for some time. I wanted to stay within my domain, forthescience.org, and I wanted the blog to be mostly about simple living, outdoor activities, beekeeping, and so on, while maintaining a scientific approach to the presentation of these topics. I therefore needed a central theme embracing their broad spectrum.

As humans, we are just the latest gimmick in a big show started billions of years ago. Many species have come and gone during this time, and many more will in the future. We humans have apparently little control on the complexity of our environment, exactly because this complexity emerges from an extremely large set of interactions and feedbacks we have little knowledge or control on. Additionally, these interactions can be extremely non-linear, meaning that they may either resist to changes, or run off wildly for even small perturbations of the dependent factors.

I first heard about the Gaia hypothesis when I was 13, in a PC game called SimEarth. The central idea is that the planet Earth is what can be considered as a self-regulating “superorganism” given by the dependencies among biota (plants, animals, bacteria, fungi, algae) and the inorganic environment in which they live (atmosphere composition, temperature, water level and acidity). This hypothesis, developed mainly by James Lovelock in the 70s, found more and more supporting evidence, and it triggered a better understanding of the complexity of the Earth system. Experiments such as Biosphere 2 made rather clear how a stable, self-sustaining system is hard to establish, and how many subtle interactions are hidden and unexpected. This network of interactions, on a global scale, is what Lovelock calls “Gaia”.

I must admit I read very little of Lovelock, and of what I read, I don’t fully agree with some of his personal opinions, in particular about the importance of computational modeling and the actions that one can take, yet the point of Gaia as a complex system of interactions is well established, factual and verified by experiments both on the field and in computational models.

That said, I decided to explore a very small subsystem of these interactions at a personal level: bees are important pollinators, producers of useful substances such as honey, wax, and propolis, but also allowing plants to reproduce; propolis and wax are used in woodworking, another dreamy interest of mine (dreamy because I lack the tools and the space); growing an extremely tiny but pleasant part of my diet is a rewarding and relaxing activity, where I learned the importance of soil characteristics, the different chemical and physical requirements for different species of plants, techniques such as hydroponics and aquaponics, mushroom growing, and integrated cultivars to take advantage of dependencies among species and improve production. I took great interest in Jared Diamond’s Collapse, and its reporting of Tikopia as a long established self-sustained islet in the pacific.

Keeping bees, growing plants, building gardens, working wood are difficult tasks that require knowledge of biology, mathematics, chemistry and physics, in addition to artistic and practical skills. This blog aims at exploring some of these topics, disentangling a tiny section of the interactions’ snarl for fun and cultural profit.

Posted in Opinion, Personal. Comments Off »

The insane clauses of a university’s contract for a researcher position

Despite having left academia for good, and for all the best reasons, all my circle of friends and professional contacts is mostly in academia. Some time ago a friend forwarded me the following contract for a researcher position in a university of a non-European country.

It should not come as a surprise that, in some countries, what we consider slavery is still a thing, but you would not expect to be a thing in written form. I report here only the paragraphs that are important, removing the boilerplate stuff. I also render the amounts in euros for ease of comparison. I leave the introductory opening, because it makes a nice (read: creepy) contrast. I will put my comments directly following the relevant fragments.

The two parties, in a spirit of valuable consideration and friendly cooperation, agree to sign this Contract and pledge to fulfill conscientiously all the duties and obligations stipulated in it.

Yeah, sure.

Employee’s annual salary will be 44.000 EUR (3683 EUR/mo). Social insurances are included and all kinds of taxes and fees have to be paid by Employee. 90% of the annual salary will be paid as follows: Employee will be paid 1900 EUR monthly and the rest, which is set as the annual research reward, will not be paid until Employee has passed the annual evaluation. And the other 10% of the annual salary will be paid by the end of the next year when Employee has passed the annual evaluation.

Meaning that, depending on the actual cost of living, one might be forced to work at a loss until the annual research award is given (if it’s given). Note, additionally, that these amounts are before taxes, that there’s no pension contribute, nor any entitled vacation.

Employee shall observe the laws, decrees and relevant regulations enacted by the country’s government and shall not interfere in its internal affairs.

Good morning fascism, it’s been a while. The definition of “interfere” is up to free interpretation, of course, especially when scientific research interferes with political decisions. We’ve seen this already, and it didn’t turn out well. I will also add that the research topic is potentially source of political distress.

Employee shall be evaluated and supervised by Employer according to the duties and obligations stipulated in the Contract

The obligations (which I am not showing here) are extremely detailed requirements for number of publications, quality of journals, teaching duties, all with undefined collaborators at discretion of the Employer.

In the annual evaluation of Employee, if Employee publishes one research paper fewer than the required number, EUR 5700 shall be deducted from his/her annual salary during the 1st and 2nd years of the term of this contract and EUR 4300 shall be deducted from his/her annual salary during the 3rd and 5th years of the term of this contract. But all the deducted money will be returned to Employee if he manages to complete all the duties and obligations by the end of the employment period.

Yeah, I am sure anybody would be glad to work like that, under the whims of referees. And not let’s even consider what happens if you get sick, or have any problem preventing you to publish due to either a collaborator’s resignation, a broken instrument, or any other unforeseen circumstance like it always happens in scientific research, in particular of the experimental on-the-field kind (which is the one we have here).

We talked about resignation. Well, that’s another “funny” point. Read this:

VI. Revision, Cancellation and Termination of the Contract
1. Both parties should abide by the Contract and should refrain from revising, canceling, or terminating the Contract without mutual consent.

Meaning you can’t quit for the duration of the contract (5 years).

4. Under the following circumstances, Employer may terminate this Contract in the form of written notice to Employee:
(1) Where Employee does not perform his/her duty or is incompetent to perform his/her duty
and remains to be incompetent after Employer’s warning.
(2) Where Employee has been proven to use fraud and deception information in the applying materials such as health, education, political performance, academic position and other documents.
(3) Employee has serious discipline breach behaviors or involvement in criminal activities.
(4) Employee unauthorized departure from office prior to the end of his term without consent.

Except for some reasonable points, point 4 restates that you can’t quit. Unless there’s mutual consent, you are not authorized to depart from your office before the end. It’s debatable if, in this case, the word “office” is meant as “work room” or “duty”, but considering what comes next…

VII. Breach Penalty
1. Employee shall pay liquidated damages to Employer if the Contract is terminated due to the occurrence of circumstances described above. The liquidated damages amount for 3 times of the monthly wages Employee has received.
2. Employee shall notify Employer three months in advance if Employee asks to cancel the Contract before the term of this Contract expires. After Employer’s consent, Employee shall pay liquidated damages to Employer and the liquidated damages amount for 3 times of the monthly wages Employer has received.

So if you are fired, or if you quit (assuming they let you go through mutual consent) you have to repay three times what you got until then.

What. The. Hell!

Needless to say, the offered position was refused. I can’t imagine anybody sane in his mind accepting it.

Posted in Academia. Comments Off »

What happened to ForTheScience?

There have been quite some changes in the past 24 hours, and a few more will follow:

  • I changed the theme to Garland. I kept the green theme because I like it and it’s the traditional color of the chemistry degree in my native city. The theme is not perfect, and I am tweaking it as I write. I will also have to check past posts to see if they behave correctly.
  • As a passionate (but inexperienced) beekeeper and gardener, I decided to start a new blog at Gaia.ForTheScience.org. It will mainly focus on my personal adventures in outdoor and non-computer-related activities, such as gardening, beekeeping, and cooking (Italian cooking, specifically), with a scientific eye and strong accent on discovery and experimentation that can be useful to others. I decided for the split because I expect the audience to be different: I already have plenty of movies and pictures to post, but I felt this blog as not appropriate for this content.
  • I removed the failed experiment of wavemol.org, which I started during a relatively well-received, but not financed application for a Marie Curie grant I applied for. After the grant was declined, I kept the domain trying to reinvent its purpose, but I found myself not particularly driven to do any more work on it. For a while, I helped a friend and hosted the p4vasp code, and received good traffic, but after the code was moved to a dedicated site, the traffic declined to spambots and I decided to terminate it for good. The URL now redirects to this blog.
  • I will soon remove WPML translations. The plugin has gone commercial, and I am not posting a lot in Italian to justify the transition. I don’t know how the current Italian posts will be handled after the removal, but my guess is that they will collapse into the current blog timeline. I might still post in Italian and provide translation posts, but they will appear directly in the current progression. I doubt this will be excessively annoying for current readers, as I normally post translations when the English post is no longer on the main page or the RSS scope.
  • I removed the redundant twitter lateral pane, leaving more screen for code and scripts that occasionally ended up being hard to read or to copy/paste. I will keep posting updates on twitter every time there’s a new post (it’s automatic)
Posted in Administrative, Personal. Comments Off »

Git stash size in the command line prompt

Too often I get confused with git when it comes to stashes. I tend to stash often, as I jump from a task to another or from a branch to another, but it already happened  I forgot I stashed something. The stash grows and I don’t remember what each patch contains. Fortunately, I never really end up doing duplicate work, but this is doomed to happen if I don’t take appropriate measures.

I concocted this function and bash prompt to present (in proper color no less) the current amount of stashed items. This way I always know if I have stashes around

function git_stash_size {
 lines=$(git stash list -n 100 2> /dev/null) || return
 if [ "${#lines}" -gt 0 ]
   count=$(echo "$lines" | wc -l | sed 's/^[ \t]*//') # strip tabs
   echo " ["${count#} "stash] "
# Comment in the above and uncomment this below for a color prompt
$(__git_ps1 " (%s)")\[\033[00m\]\[\033[01;31m\]$(git_stash_size)\[\033[00m\]\$ '

Reformat properly as a single line.

Posted in git. Comments Off »

Can’t add widgets in Qt Designer? Don’t use Unity

I got around a problem in Qt designer that didn’t allow me to drop any widget. When I tried, I obtained the barred circle mouse pointer, instead of the plus. When dropped, the drop operation did not complete, and the receiving widget remained empty. Apparently, it’s due to some form of collision between designer and Unity (the graphic environment on Ubuntu). I switched to Gnome and now designer works. Note that I got it to work occasionally on Unity as well, but I was never able to reproduce the exact conditions that allowed it.


Posted in Qt, Ubuntu. Comments Off »

A good lesson in python and unicode

Ever wanted to understand more about unicode in python? This talk is a good explanation on how to deal with it properly

Posted in Python, Unicode. Comments Off »

The modem dialup handshake analyzed

Those of you old enough to get internet connection with an empty can of beans connected to a string will probably remembered a coal-powered tool of the time, the modem. Its shrieks filled the beginning of an evening of bad GIFs, short web pages with <blink> tags, and plenty of telnet and IRC sessions. The memorable combination of sounds was a bit of a mystery to me, but I found this extremely interesting blog post detailing the negotiation phase, step by step, directly on the spectrogram. Worth checking out if you ever wondered what your modem was saying.

Posted in Hardware. Tags: . Comments Off »

Lack of usability in Gnome/Ubuntu

Maybe I should file a bug report on this, but it’s a nice example of lack of usability due to colliding needs: the need for a scrollbar in the terminal, and the need for window resizing.

As you can see, the popup scrollbar always follows the mouse pointer, making it impossible to get the resize operation. This happens in the bottom-right corner as well. The only way to perform resizing is to either use the top right corner, or to “trick” the scrollbar by moving the mouse pointer in a particular way, something made even more difficult by the fact that this area is one or two pixels wide.

This is so wrong from the usability point of view that I am surprised nobody caught it.

Posted in Usability. Tags: , . Comments Off »