One Dimensional Geometric probability and python
Tags: python, probability, maths, dice
Published: 2020-09-03 08:26:57

Recently I published a video on YouTube solving a problem in One Dimensional Probability. The problem goes as follows - **What is the probability that a random number chosen between 0 and 3 will be closer to 0 than it is to 1?** You can watch the video here - 
{% youtube Ld0FHvadecM %}

How do you approach this problem? You cannot use the traditional formula of probability - 

> Probability = (Number of desired events / Number of all possible events)

You might be thinking why can't we use this formula? 

Well, the reason for that is that the above formula can be used only when we have a countable number of events. But in this case, the number of desired as well as all possible events is infinite. Consider this number line - 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p6xj5dx0owfqtzdcbag0.jpg)

There can be infinite numbers on this line segment of 3 units so our above formula for calculating probabilities fails here. The only recourse left before us is to use Geometric Probability.

When the events cannot be counted, we can represent them as geometric figures. If we have 1 independent variable, we use lengths to represent them, if we have 2, we use areas and if we have 3, we use volumes.

Since here the number of independent variables is just 1, we can represent it using lengths. 

Now that we have laid the groundwork, its time to tackle the question. Consider that, on the above-shown number line, any number will be equidistant from 0 and 1 when it is equal to 0.5 since its exactly mid-way between the two. 

If any number is to the left of 0.5, it will closer to 0 and if it is to the right of 0.5 all the way up to 3, it will be closer to 1. Thus we can say that the segment from 0 to 0.5 units represents our desired outcome and the segment from 0 to 3 represents all possible outcomes. The formula for probability now reduces to - 

> Probability = (length of the desired line segment/length of the entire possible line segment)

Putting in the values, we get {% katex inline %}
 0.6/3 = 1/6 = 0.1666....
{% endkatex %}

Well, that's the theory - right! Its time to test it using python. 

## Code

In the first step, import the `random` function from the `random` module because we will need to generate random numbers. 

```python
from random import random
```

Second, make a function that will generate a random number between 0 and 3 and perform the proximity check with zero or 1. 

```python
def one_trial():
    # Code goes here
    # and here
```

We name this function one_trial. Inside this function, we generate a random number x using the random function that we imported earlier. Remember that this function produces values between 0 and 1 so we will need to multiply it with 3 so that it produces random numbers between 0 and 3.

```python
def one_trial():
    x = random() * 3
```

Next, test whether this number x is closer to 0 or 1 by checking if it is less than 0.5. If yes, then return 1, if no, then return 0.

```python
def one_trial():
    x = random() * 3
    
    if x <=0.5:
        return 1
    return 0
```

That's all there is to this function.

Now we just have to run this function numerous times and check whether the result comes the same as we determined using mathematics. 

Let us check for ten thousand iterations. 

```python
n = 10000
```

Next, we make a variable 'success' which will track how many times have we gotten the desired outcome that is the number less than 0.5. We initialize this variable with 0.

```python
success = 0
```

Next, we create a `for` loop which will perform this experiment N times and in every iteration, the result from each trial function will be added to the success variable i.e. 1 if we got success and 0 if we got no success.

```python
for _ in range(n):
    success += one_trial()
```

After the `for` loop ends, its time to print the results in the form of probability i.e. success divided by iterations. 

```python
print(f"The probability is {success/n}")
```

Its time to run this function. Remember that the expected answer is 0.1666... 

I am getting 0.1761 for 10000 iterations and 0.16623 for a million iterations. 

Thus our calculations of geometric probability are indeed correct guys. I hope you enjoyed this article. 

Thanks and bye-bye. I will see you in the next one.