## On Chinese Valentine's day, I wrote a matchmaking model in Python

Tianyuan prodigal son 2020-11-13 09:06:15
chinese valentine day wrote matchmaking

Say it first ： This article is purely a work of entertainment on the Double Seventh Festival . If someone follows the mate selection theory proposed in this model and leads to lovelorn or single , Except for compassion , I can't make up for more .

In traditional Chinese festivals , Tanabata may be the most mysterious origin 、 The most profound one . Of course , This is not the point of this article , Our core problem is ： On the special commemorative day of Chinese Valentine's Day , You really want to talk to TA A confession ？TA Is it really the only right choice for you ？ This matchmaking model , Maybe it's enlightening for you .

My matchmaking agency is doing well , Countless single people who want to find their ideal partner come to visit . According to the appearance 、 character 、 Ability 、 Wealth and other factors , I set a quality index for each client （Quality Index）, Shorthand for qidx. Statistical findings ,qidx Show the mean 8.0、 Standard deviation 0.5 Normal distribution .

Here is 1 Million customers qidx Statistical distribution , You can see that most single people have qidx be located 7.0~9.0 Between , Evaluate negative and excellent , All belong to the minority .

``````import numpy as np
import matplotlib.pyplot as plt
singles = np.random.normal(loc=8.0, scale=0.5, size=10000)
plt.hist(singles, bins=8, histtype='step')
plt.show()
``````

In general , My clients pay 1 Time , Will get 10 Second choice opportunity . My strategy for recommending goals to clients is based on “ suitable ”, Always choose with the customer qidx Adaptive opposite sex , Specifically speaking, it is based on the customer's qidx Is the mean , With 0.1 The variance of , Random generation according to normal distribution .

Usually , There are two ways for customers to choose from the goals I recommend to them . The first is based on the traditional concept of mate selection , The specific rules are as follows .

1. Yes 10% 's customers will fall in love with their current recommendation target at first sight , Don't care about both sides qidx match
2. If the current recommended target is qidx Higher than the customer , But not more than 0.2, The probability that the customer chooses the current recommended target , It will increase as the number of remaining options decreases , About from 0.35 Rise to be 0.8
3. If the current recommended target is qidx Higher than the customer 0.2 above , The probability that the customer chooses the current recommended target , It will increase as the number of remaining options decreases , About from 0.55 Rise to be 1.0
4. If the current recommended target is qidx Lower than customers , But not more than 0.2, The probability that the customer chooses the current recommended target , It will increase as the number of remaining options decreases , About from 0.25 Rose to 0.7
5. If the current recommended target is qidx Lower than customers 0.2 above , The probability that the customer chooses the current target , It will increase as the number of remaining options decreases , About from 0 Rose to 0.18

The second way of matching is based on “ Grain theory ”, It sounds very tall . The explanation of wheat ear theory is omitted here , Interested students can search by themselves . specifically , It's the customer that comes first 4 In the second recommendation , No choice , Just write down the tallest of them qidx; From 5 Time to start , As long as you encounter more than or equal to before 4 Second highest qidx Recommended target for , Make a choice .

below , I use two ways to match 1 Ten thousand customers choose their spouses , What will happen ？

``````# -*- encoding: utf-8 -*-
import numpy as np
class Single:
def __init__(self, qidx, times):
self.times = times # The number of matches provided by the matchmaker
self.counter = 0 # The current number of matches
self.qidx = qidx # Customer's qidx
self.spouse = None # Matching a successful spouse qidx
self.histroy = list() # Based on the theory of ear of wheat times/e Second, the recommended object of qidx
def math_classical(self, spouse):
self.counter += 1
if np.random.random() < 0.1:
self.spouse = spouse
if spouse - self.qidx >= 0.2:
if np.random.random() < 1-0.05*(10-self.counter):
self.spouse = spouse
elif spouse - self.qidx > 0:
if np.random.random() < 0.8-0.05*(10-self.counter):
self.spouse = spouse
elif self.qidx - spouse >= 0.2:
if np.random.random() < 0.18-0.02*(10-self.counter):
self.spouse = spouse
elif self.qidx - spouse >= 0:
if np.random.random() < 0.7-0.05*(10-self.counter):
self.spouse = spouse
def match_technical(self, spouse):
self.counter += 1
if self.counter < self.times/np.e:
self.histroy.append(spouse)
elif spouse >= max(self.histroy):
self.spouse = spouse
def main(math_mode, total=10000, times=10):
# The total number of builds is total The customer , Its qidx There is normal random function generation
singles = [Single(np.random.normal(loc=8.0, scale=0.5), times) for i in range(total)]
for p in singles:
for i in range(10):
if p.counter < 10 and not p.spouse:
spouse = np.random.normal(loc=p.qidx, scale=0.1)
getattr(p, math_mode)(spouse)
matched = np.array([(p.qidx, p.spouse) for p in singles if p.spouse])
diff = matched[:,0] - matched[:,1]
print('----------------------------------')
print(' A successful match %d people , The success rate %0.2f%%'%(matched.shape[0], matched.shape[0]*100/total))
print(' Customer qidx mean value %0.2f, Spouse mean %0.2f'%(np.sum(matched[:,0])/matched.shape[0], np.sum(matched[:,1])/matched.shape[0]))
print(' Matching variance %0.2f, Matching standard deviation %0.2f'%(diff.var(), diff.std()))
print()
if __name__ == '__main__':
print(' Based on the statistical results of traditional mate selection ')
main('math_classical')
print(' Statistical results of mate selection based on wheat ear theory ')
main('match_technical')
``````

Compare the matching success rate of the two schemes 、 Match the average of successful customers qidx、 The average spouse of a successful customer qidx、 Clients and spouses qidx And so on , You'll find that , This result is really interesting .

`````` Based on the statistical results of traditional mate selection
----------------------------------
A successful match 10000 people , The success rate 100.00%
Customer qidx mean value 8.00, Spouse mean 8.02
Matching variance 0.01, Matching standard deviation 0.10
Statistical results of mate selection based on wheat ear theory
----------------------------------
A successful match 7138 people , The success rate 71.38%
Customer qidx mean value 8.00, Spouse mean 8.11
Matching variance 0.00, Matching standard deviation 0.07
``````

### Conclusion ：

1. Choosing a mate based on the traditional way , Higher success rate （100% VS 71.38%）;
2. Based on the theory of ear of wheat , Spouse quality index is higher （8.11 VS 8.02）;
3. Based on the theory of ear of wheat , The two sides qidx The standard deviation of the difference is smaller （0.07 VS 0.10）, This means that both sides match better .