33from typing import ClassVar
44
55import pandas as pd
6+ from scipy import stats
67
78from testgen .common .database .database_service import (
89 execute_db_queries ,
@@ -29,13 +30,14 @@ class TestThresholdsPrediction:
2930 "prediction" ,
3031 )
3132 num_forecast = 10
33+ t_distribution_threshold = 20
3234 z_score_map : ClassVar = {
33- ("lower_tolerance" , PredictSensitivity .low ): - 2 .0 , # 2.5th percentile
34- ("lower_tolerance" , PredictSensitivity .medium ): - 1 .5 , # 7th percentile
35- ("lower_tolerance" , PredictSensitivity .high ): - 1 .0 , # 16th percentile
36- ("upper_tolerance" , PredictSensitivity .high ): 1 .0 , # 84th percentile
37- ("upper_tolerance" , PredictSensitivity .medium ): 1 .5 , # 93rd percentile
38- ("upper_tolerance" , PredictSensitivity .low ): 2 .0 , # 97.5th percentile
35+ ("lower_tolerance" , PredictSensitivity .low ): - 3 .0 , # 0.13th percentile
36+ ("lower_tolerance" , PredictSensitivity .medium ): - 2 .5 , # 0.62nd percentile
37+ ("lower_tolerance" , PredictSensitivity .high ): - 2 .0 , # 2.3rd percentile
38+ ("upper_tolerance" , PredictSensitivity .high ): 2 .0 , # 97.7th percentile
39+ ("upper_tolerance" , PredictSensitivity .medium ): 2 .5 , # 99.4th percentile
40+ ("upper_tolerance" , PredictSensitivity .low ): 3 .0 , # 99.87th percentile
3941 }
4042
4143 def __init__ (self , test_suite : TestSuite , run_date : datetime ):
@@ -71,9 +73,15 @@ def run(self) -> None:
7173 ] if self .test_suite .predict_holiday_codes else None ,
7274 )
7375
76+ num_points = len (history )
7477 for key , z_score in self .z_score_map .items ():
78+ if num_points < self .t_distribution_threshold :
79+ percentile = stats .norm .cdf (z_score )
80+ multiplier = stats .t .ppf (percentile , df = num_points - 1 )
81+ else :
82+ multiplier = z_score
7583 column = f"{ key [0 ]} |{ key [1 ].value } "
76- forecast [column ] = forecast ["mean" ] + (z_score * forecast ["se" ])
84+ forecast [column ] = forecast ["mean" ] + (multiplier * forecast ["se" ])
7785
7886 next_date = forecast .index [0 ]
7987 sensitivity = self .test_suite .predict_sensitivity or PredictSensitivity .medium
0 commit comments