Python mathematical modeling 1 - model statistics and analysis of how many people will be known in a simulated life

Hello everyone, I am Weixue AI, and today I will bring you Python Mathematical Modeling 1 - model statistics and analysis of how many people a human will know in a lifetime. Have you counted how many people you have come into contact with from birth to the present, how many people you have known, maybe you just know each other, but now you basically don’t contact them, maybe you only contact single digits now, in this busy modern life , the space distance between people has been pulled far. Today I will count and build a model to see how many people we will know in our lifetime?

Contents
1. Background introduction
2. Mathematical model description
3. Mathematical model construction and code implementation
4. Running results
5. Summary

1. Background introduction

We will meet all kinds of people in our life. Have you ever counted how many people we will know in this life? There are 1.4 billion people in China, but the people we know around us may be very few of them. The people we really know The exact number may not be calculated because everyone's social circle and relationships are different. The number of people we know can vary from tens to thousands, depending on our occupation, living environment and daily social activities.

Among so many people we know, the ones who can really accompany us to the end usually refer to close partners, family members or very close friends. This person may be just one or a handful in our lifetime. They share our joys and sorrows, support our dreams, and give support and encouragement in difficult times. These people are precious to us, and it is worth cherishing and maintaining this relationship.

2. Mathematical model description

Now there is a model of a virtual society. In this mathematical model, 10,000 people are sample individuals: Everyone may know various people in their lifetime, and the ranking of the number of people they know and the average number of people they know after 80 years are counted. .

Model condition 1 : In the student stage: everyone will go through the stages of kindergarten, elementary school, junior high school, high school, and university, and some people are still studying for graduate students and doctors. We divide them into 8:2, and 20% of them are studying For postgraduates and doctors, each person has 60 classmates and 10 teachers of various subjects in each section, 50% of each person will have 10 other classmates and good friends, and 50% will have 20 other friends and friends.
During the student learning stage, each person will randomly have 0-20 people they know unexpectedly.

Model condition 2 : In working stage: 40% of people have 3 units, 20% of people have 4 units, 20% of people have 2 units, 10% of people have 1 unit, and 5 % of people have more than 5 units (respectively 5-8 units), 5% of people have no unit, and each unit will randomly know 40-180 friends and partners at work (this is a regular state distribution).

Model condition 3 : In the retirement stage: everyone will randomly know 30-70 old friends, relatives, and young people in retirement life (this is a normal distribution).

3. Mathematical model construction and code implementation

We will build a model with 10,000 people, and then predict the number of people each person has met, rank the number of people they have known in their lifetime, and count the average number of people they know.

The normal distribution is used in the model, which is an important continuous probability distribution in probability theory and statistics, and is often called the Gaussian distribution. Its mathematical principle can be expressed in the following way:

The probability density function of the normal distribution can be expressed as:

f ( x ) = 1 σ 2 π e − ( x − μ ) 2 2 σ 2 f(x) = \frac{1}{ {\ sigma \sqrt{2\pi}}} e^{-\frac{ {(x-\mu)^2}}{ {2\sigma^2}}}f(x)=p2 p.m 1e2 p2( x μ )2

Among them, xxx is the value of the random variable,μ \muμ is the mean,σ \sigmaσ is the standard deviation.

The cumulative distribution function of the normal distribution can be expressed as:

F ( x ) = ∫ − ∞ x f ( t )   d t = 1 2 [ 1 + erf ( x − μ σ 2 ) ] F(x) = \int_{-\infty}^{x} f(t) \, dt = \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}} \right) \right] F(x)=xf(t)dt=21[1+erf(p2 xm)]

Among them, erf ( z ) \text{erf}(z)erf ( z ) is the error function.

According to the properties of the normal distribution, about 68% of the data fall on the mean value μ ± 1 \mu \pm 1m±1 standard deviationσ \sigmaIn the range of σ , about 95% of the data fall within μ ± 2 σ \mu \pm 2\sigmam±In the range of 2 σ , about 99.7% of the data fall withinμ ± 3 σ \mu \pm 3\sigmam±range . This is known as the 68-95-99.7 rule for the normal distribution.

Here is the implemented code:

import numpy as np
import matplotlib.pyplot as plt

# 初始化
num_people = 10000
people = np.zeros(num_people)

# 生成一个服从正态分布的随机数字
def generate_random_number(mean,mins,maxs,sigma = 5):
    # 使用numpy库生成一个服从正态分布的随机数
    number = np.random.normal(mean, sigma)
    # 对结果取整并限制在0到10之间
    number = int(round(number))
    number = max(mins, min(number, maxs))
    return number

# 学生阶段
for i in range(num_people):
    friends = 60*5 + 10  # 同学和老师
    if np.random.rand() < 0.5:
        friends += 20  # 其他同学好朋友
    else:
        friends += 30  # 其他发小和朋友
    if np.random.rand() < 0.2:
        friends += (60 + 10) * 2  # 研究生和博士阶段
    # 0-10随机生成数字
    friends += np.random.randint(0, 20)
    people[i] += friends

# 工作阶段
for i in range(num_people):
    num_jobs = np.random.choice([3, 4, 2, 1, np.random.randint(5, 9), 0], p=[0.4, 0.2, 0.2, 0.1, 0.05, 0.05])

    for m in range(num_jobs):
        friends =generate_random_number(120,40,180)
        #friends = np.random.randint(40, 161)  # 工作上的朋友、邻居、亲戚
        people[i] += friends

# 退休阶段
for i in range(num_people):
    friends = generate_random_number(60,30,70)
    #friends = np.random.randint(30, 71)  # 退休生活上的老朋友,亲戚,年轻人
    people[i] += friends

# 排名和平均数
#rank = np.argsort(-people) + 1  # 按照从高到低排序,并将索引转换为排名(从1开始)
rank = np.argsort(people)[::-1]
average = np.mean(people)

for i in rank:
    print("Rank {}: {}".format(i + 1, people[i]))

# 打印结果
print("Average number of people known:", average)

# 画图
plt.figure()
plt.hist(people, bins=20)
plt.title('Distribution of Number of People Known')
plt.xlabel('Number of People Known')
plt.ylabel('Frequency')
plt.show()

4. Running results

We see the ranking of the number of encounters:

Rank 4263: 1551.0
Rank 2190: 1545.0
Rank 5038: 1533.0
Rank 9674: 1517.0
Rank 7863: 1515.0
Rank 5988: 1514.0
Rank 569: 1514.0
Rank 973: 1512.0
Rank 9469: 1512.0
Rank 6025: 1511.0
Rank 2429: 1506.0
Rank 9857: 1504.0
Rank 4073: 1501.0
Rank 4721: 1500.0
Rank 9604: 1500.0
Rank 8893: 1497.0
Rank 6530: 1497.0
Rank 7289: 1496.0
Rank 6069: 1490.0
Rank 5171: 1488.0
Rank 1414: 1469.0
Rank 4347: 1420.0
Rank 1091: 1408.0
Rank 9573: 1406.0
Rank 1321: 1402.0
Rank 7111: 1401.0
Rank 5332: 1401.0
Rank 5681: 1400.0
Rank 4972: 1399.0
Rank 2777: 1397.0
Rank 1433: 1396.0
Rank 686: 1396.0
Rank 7207: 1396.0
Rank 7374: 1395.0
Rank 7938: 1395.0
Rank 3329: 1394.0
Rank 9229: 1392.0
Rank 7709: 1391.0
Rank 3458: 1390.0
Rank 5074: 1390.0
Rank 9244: 1386.0
Rank 727: 1386.0
Rank 7791: 1385.0
Rank 1668: 1384.0
Rank 5889: 1384.0
Rank 9352: 1383.0
...

The following is the distribution of the number of people who know each other:
insert image description here
According to the above results, each person will know about 775 people in their lifetime on average, but this is just a simple model prediction. In real life, they may know more people, and some relatively introverted people may know them There are very few people.
The largest number of acquaintances in the model is 1551 people, which means that there are people who really have contacts, not just a one-sided relationship.

5. Summary

In our lifetime, we will meet many people, and we should treat everyone we meet with heart, because they all have the potential to become that special person in our life. While the odds of that special someone appearing are low, we have a better chance of meeting them if we keep an open and kind heart.

But people who can really accompany us to the end are very rare. This person could be our partner, our true love. The data tells us to know how to cherish the people around us and build deep relationships with them. We must learn to be grateful, and try our best to maintain and cultivate those relationships that are connected with our hearts and can provide support and understanding in difficult times.

Guess you like

Origin blog.csdn.net/weixin_42878111/article/details/132580057