66from statsmodels .sandbox .regression .predstd import wls_prediction_std
77
88
9- def check_tolerance (t , y , to_exclude : int = 1 , poly_features : list = [1 , 2 ], alpha : float = 0.05 , forecast : bool = False ) -> pd .DataFrame :
9+ def check_tolerance (t , y , to_exclude : int = 1 , poly_features : list = [1 , 2 ], alpha : float = 0.05 , predict_all : bool = False ) -> pd .DataFrame :
1010 """
1111 Check that some future values are within a weighted least squares confidence interval.
1212
1313 Parameters
1414 ----------
1515 t : np.array
16- N explanatory time bins of shape (N, 1).
16+ N explanatory time points of shape (N, 1).
1717 y : np.array
1818 The corresponding response variable values to X, of shape (N, 1).
1919 to_exclude : int, default = 1
2020 How many of the last y values will have their tolerances checked.
2121 poly_features : list, default = [1, 2]
22- List of degrees of polynomial features to fit to the data. One model will be
22+ List of degrees of polynomial basis to fit to the data. One model will be
2323 produced for each number in the list, eg. the default will fit a linear and
2424 a second degree polynomial to the data and return both sets of results.
2525 alpha : float, default = 0.05
2626 Alpha parameter for the weighted least squares confidence interval.
27- forecast : bool, default = False
28- When set to true, will return model projections for 20% of the data range beyond
29- the current distribution.
27+ predict_all : bool, default = False
28+ Set to true to show predictions for all points of the dataset.
3029
3130
3231 Returns
@@ -59,7 +58,7 @@ def check_tolerance(t, y, to_exclude: int = 1, poly_features: list = [1, 2], alp
5958 assert all (0 <= degree <= 4 for degree in poly_features ), (
6059 "Please ensure all numbers in poly_features are from 0 to 4."
6160 )
62- if not isinstance (alpha , float ) or 0 >= alpha >= 1 :
61+ if not isinstance (alpha , float ) or 0 > alpha >= 1 :
6362 raise ValueError ("Please input a float between 0 and 1 for alpha." )
6463 if not isinstance (to_exclude , int ) or len (t ) <= to_exclude < 1 :
6564 raise ValueError ("Please input an integer between 1 and your sample size for to_exclude." )
@@ -76,16 +75,6 @@ def check_tolerance(t, y, to_exclude: int = 1, poly_features: list = [1, 2], alp
7675 idx = np .argsort (t )
7776 t = t [idx ]
7877 y = y [idx ]
79- forecasts = 5
80-
81- if forecast :
82- # 5 forecast values based on 20% of data range
83- t_range = t [- 1 ] - t [0 ]
84- t_forecast = np .linspace (
85- (t [- 1 ] + t_range * 0.001 ),
86- (t [- 1 ] + t_range * 0.2 ),
87- forecasts ,
88- )
8978
9079 results = pd .DataFrame ()
9180 for degree in poly_features :
@@ -99,24 +88,17 @@ def check_tolerance(t, y, to_exclude: int = 1, poly_features: list = [1, 2], alp
9988 _t = fitted_transforms .transform (t .reshape (- 1 , 1 ))
10089
10190 t_train , y_train = _t [:- to_exclude , :], y [:- to_exclude ]
102- t_predict , y_predict = _t [- to_exclude :, :], y [- to_exclude :]
103-
104- if forecast :
105- # Add forecasts to prediction array
106- t_predict = np .append (
107- t_predict ,
108- fitted_transforms .transform (t_forecast .reshape (- 1 , 1 )),
109- axis = 0
110- )
111- # This will prevent the final dataframe complaining about array lengths.
112- y_predict = np .append (y_predict , np .full (forecasts , np .nan ))
91+ t_predict , y_predict = (
92+ _t if predict_all else _t [- to_exclude :, :],
93+ y if predict_all else y [- to_exclude :]
94+ )
11395
11496 # Fit ordinary least squares model to the training data, then predict for the
11597 # prediction data.
11698 model = sm .OLS (y_train , t_train ).fit ()
11799 yhat = model .predict (t_predict )
118100
119- # Calculate confidence interval of fitted model.
101+ # Calculate prediction intervals of fitted model.
120102 _ , yhat_l , yhat_u = wls_prediction_std (model , t_predict , alpha = alpha )
121103
122104 # Store model results in master frame
0 commit comments