Tag Archives: sympy

Binomial Expansions on Python

The other day I was asked to solve a complex probability problem. This probability problem was related to a vocabulary quiz which was being circulated around the web. It is a vocabulary quiz which contains 15 questions with 2 two possible answers to each question. My goal was to simply state the chances of someone (with a terrible vocabulary) answering at least 7/15 questions in this quiz correctly without referring to Google or any other search engine… Their answers to these questions were going to be completely random.

Now, in order solve this probability problem I first had to acknowledge seven essential facts:

1) The person taking this quiz has a terrible vocabulary and just wouldn’t be able to answer any of the questions in this quiz with confidence;

2) The person answering these questions would be providing random answers to them;

3) The person taking this quiz isn’t allowed to use any tools/resources which would help him/her answer these questions correctly;

4) There were only two possible answers to each question (right (R) or wrong (W) answers);

5) Each answer to these questions were mutually exclusive, in other words, selecting the right answer to one question wouldn’t increase your chances of answering any other question correctly ;

6) There were 15 questions in total;

7) A binomial distribution  would be required to solve the problem as there were two mutually exclusive outcomes to each question being answered.

With these facts in mind I knew exactly what I had to do to solve this problem, however, expanding the expression (R+W)^15 was going to be a very tedious task indeed and would require plenty of manual labour.

Was there another way to solve this problem?? Could a computer program help me solve the problem more easily??

Well it turned out that I would be able to solve the problem more easily, but that I’d have to use Python (a high level general purpose programming language) including an “add on” called Sympy to achieve such a feat. So what I essentially did was install both Python(x, y) and Sympy on my Windows laptop.

*You can find out how to download both Python and Sympy by watching the video below:

I then searched “Python IDLE” on my laptop and opened up Python’s shell application. This is what I typed into the application:

(R+W)^15 Expansion - Python

If you look at the image above carefully you will see that this Python shell application (thanks to the help of the “add on” Sympy, including some handy code) was able to expand (R+W)^15 for me… In fact, it did it in a split second.

From the moment (R+W)^15 had been expanded, to solve the problem I had been asked to solve, all I had to do was:

i) Sum up the coefficients of the variables (R, W) included in the expansion;

ii) Place this sum (a) under the value of the sum of coefficients sitting beside R variables with exponentials greater or equal to 7 (b) – in a fraction b/a.

This fraction (b/a) would give me the probability / chances of someone randomly being able to answer at least 7/15 questions correctly in the vocabulary test under the conditions which had been set.

a = 2(1 + 15 + 105 + 455 + 1365 + 3003 + 5005 + 6435) = 32,768

b = 1 + 15 + 105 + 455 + 1365 + 3003 + 5005 + 2(6435) = 22,819

b/a = 22,819/32,768 = 0.70 (to 2 decimal places)

Solution to problem: 70% chance

Now the irony of the story is this… I gave myself this problem to solve because my “posh” vocabulary is horrendous. It turned out that I managed to get 9/15 questions in the vocabulary test correct and all my answers were guesses. 🙂

Thanks for reading!!