Better of Two Models
df has columns y, x and z. Fit two models by ordinary least squares: the small one, y ~ x, and the big one, y ~ x + z. Return "small" or "big": the model with the lower AIC, the score that rewards fit and charges for every extra parameter.
Example 1
df {"x":[1,2,3,4,5,6,7,8],"z":[3,1,4,1,5,9,2,6],"y":[7.1,4.9,11.2,5.8,14.9,24.1,10.9,20.2]}
returns "big"
Here y depends strongly on z, so adding it is worth the extra parameter.
Example 2
df {"x":[1,2,3,4,5,6,7,8],"z":[3,1,4,1,5,9,2,6],"y":[2.1,3.9,6.2,7.8,10.1,11.9,14.2,15.8]}
returns "small"
Here y is a line in x alone, and z only costs a parameter.
Constraints
8 <= len(df) <= 40- The two scores are not close
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More statsmodels problems
- Chance of YesMedium
- Does One Change the OtherMedium
- Model for CountsMedium
- Odds of YesMedium
- Pattern in the LeftoversMedium
- Point That Pulls HardestMedium
- Tangled PredictorsMedium
- Top of the CurveMedium