I don't have a sense of "quiz asking investment sense", so I tried to solve it with brute force (Python Monte Carlo simulation)

An interesting quiz was given in the past article of "ROKO HOUSE Siegel style logical investment technique".

[Quiz to ask investment sense ]

The problem is as follows.

In countries that only want boys, every house keeps growing children until a boy is born. When I have a girl, I will have another child. When a boy is born, he will no longer have children. What is the gender ratio in this country?

I intuitively thought, "Every house has one boy. Is there a little more girls because they keep having children until they are born?"

Click here for the answer article. I think it's a good idea to think for yourself before looking at it.

[Quiz to ask investment sense <Answer / Explanation>]

My intuition was wrong, so I actually tried to see if the answer was really correct.

We asked a million couples to make children until a boy was born.

Actually, it is impossible unless it is a dictatorship, so I did it with Python code.

Is this also a kind of Monte Carlo simulation?

Below are the results. I think it's a good idea to think for yourself before looking at it. (Hereafter spoilers)

I confirmed it with the following code.

python


import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

dic =  {'boys':[],
        'girls':[]}

n_couples = 1000000

for i in range(n_couples):
    n_girls = 0
    n_boys = 0
    while True:
        baby = random.choice(['boy','girl'])
        if baby=='boy':
            n_boys += 1
            break
        else:
            n_girls += 1
            
    dic['boys'].append(n_boys)
    dic['girls'].append(n_girls)

df = pd.DataFrame(dic)
df.index.name = 'parent_id'
df['total'] = df.sum(axis = 1)

print("Born from a million couples,")
print("Number of boys:{:>7}".format(df['boys'].sum()))
print("Number of girls:{:>7}".format(df['girls'].sum()))
print("\n Average number of births:{:.0f}Times".format(df['total'].mean()))
print("\n Household distribution by number of girls")
df2 = pd.DataFrame(df['girls'].value_counts())

df2.index.name = 'Number of girls'
df2.columns = ['Number of households']

df2e = df2.copy()
df2e.index.name = 'Number of girls'
df2e.columns = ['number of couples']
df2e.plot(kind = 'bar', figsize = (8,5))

df2

Below are the outputs and comments from the code.

python


Born from a million couples,
Number of boys: 1000000
Number of girls: 999687

The number of boys is, of course, one million. The number of girls is almost one million.

python


Average number of births: 2 times

On average, if you give birth twice, you will have a baby boy. I'm convinced that the probability is 50%, but I didn't know in advance.

** Distribution of households by number of girls **

Number of girls Number of households
0 500580
1 249813
2 124471
3 62512
4 31319
5 15657
6 7718
7 3962
8 1935
9 1041
10 493
11 239
12 141
13 60
14 22
16 14
15 13
17 4
18 3
20 1
19 1
25 1

In about 50,000 households, half of the total, boys are born on the first birth and there are no girls. The probability is 50%.

The point that the total number of boys and girls is equal is that "50% of households have one boy and zero girls". When I thought about it intuitively, I overlooked this point.

And one girl will be about 25,000 households, two girls will be about 12,500 households, and so on. There is a 50% chance that a baby boy will be born per birth.

Finally, there was a couple who gave birth to 25 girls by the time they gave birth to a boy. thank you for your hard work.

The graph above shows this. dist_of_couples.png

I didn't understand this quiz at first, but while writing the code, "When a boy is born, the couple will stop giving birth, and I'm kneading various things, but in the end, there is a 50% chance that which couple will give birth. Isn't it just that boys and girls continue to be born? "

A person with a good sense will immediately notice this fact, and a distribution image like the above graph will come to mind.

So the answer is

** "The male-female ratio in this country is 1: 1 (expected value)" **

Was the correct answer.

Recommended Posts

I don't have a sense of "quiz asking investment sense", so I tried to solve it with brute force (Python Monte Carlo simulation)
"Brute force of MD5 hash value of 6-digit password" I tried it with Python
I tried a stochastic simulation of a bingo game with Python
I tried to create a list of prime numbers with python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to make a calculator with Tkinter so I will write it
I tried to make a simple mail sending application with tkinter of Python
When I tried to create a virtual environment with Python, it didn't work
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to draw a route map with Python
I tried to solve the soma cube with python
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to automatically generate a password with Python3
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to solve the problem with Python Vol.1
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
I tried to solve AOJ's number theory with Python
The 15th offline real-time I tried to solve the problem of how to write with python
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
How to write offline real time I tried to solve the problem of F02 with Python
I made a server with Python socket and ssl and tried to access it from a browser
I made a function to crop the image of python openCV, so please use it.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
I tried to find the entropy of the image with python
A collection of competitive pro techniques to solve with Python
I tried to solve a combination optimization problem with Qiskit
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
[5th] I tried to make a certain authenticator-like tool with python
I tried to solve the ant book beginner's edition with python
(Python) Expected value ・ I tried to understand Monte Carlo sampling carefully
[2nd] I tried to make a certain authenticator-like tool with python
A memorandum when I tried to get it automatically with selenium
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I wanted to solve the ABC164 A ~ D problem with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to improve the efficiency of daily work with Python
I made a segment tree with python, so I will introduce it
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to make a mechanism of exclusive control with Go
[You have to know it! ] I tried to set up a Python environment profitably by making full use of the privileges of university students.
Python: I tried to make a flat / flat_map just right with a generator
I tried to communicate with a remote server by Socket communication with Python.
Have Alexa run Python to give you a sense of the future
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
How to write offline real time I tried to solve E11 with python
I tried to get the authentication code of Qiita API with Python.
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python
A beginner of machine learning tried to predict Arima Kinen with python
I tried to streamline the standard role of new employees with Python
[Outlook] I tried to automatically create a daily report email with Python
I tried to get the movie information of TMDb API with Python