pythonnumpypandas
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

Define a person's Family IQ Score as

F a m i l y I Q = 0.5 ⋅ I Q + 0.5 ⋅ R e l a t i v e s I Q Family IQ = 0.5 \cdot IQ + 0.5 \cdot Relatives IQ FamilyIQ=0.5IQ+0.5RelativesIQ

where R e l a t i v e s I Q Relatives IQ RelativesIQ is the average IQ score of that person's parents, full siblings, and children.

Given a dataset of people and their IQ scores, determine who has the highest F a m i l y I Q Family IQ FamilyIQ.

import numpy as np
import pandas as pd
 
generator = np.random.default_rng(2718)
persons = pd.DataFrame({
    'id':     [ 2,  3,     8, 12, 14, 15,    17,    32,    35,    41,    60, 64, 83, 98],
    'mom_id': [35, 41, pd.NA, 35, 41,  2, pd.NA, pd.NA, pd.NA, pd.NA,     8, 12, 35,  2],
    'dad_id': [17,  8, pd.NA, 17,  8, 32, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, 14, 17, 14],
    'IQ': np.round(generator.normal(loc=100, scale=20, size=14))
})
 
print(persons)
#     id mom_id dad_id     IQ
# 0    2     35     17  106.0
# 1    3     41      8   99.0
# 2    8   <NA>   <NA>   56.0
# 3   12     35     17  110.0
# 4   14     41      8  104.0
# 5   15      2     32  109.0
# 6   17   <NA>   <NA>   99.0
# 7   32   <NA>   <NA>   90.0
# 8   35   <NA>   <NA>   80.0
# 9   41   <NA>   <NA>   52.0
# 10  60      8   <NA>   97.0
# 11  64     12     14   87.0
# 12  83     35     17  138.0
# 13  98      2     14   97.0

Note

A full sibling of a person is someone who shares the same mom and dad.


Solution

This content is gated

Subscribe to one of the products below to gain access