66from statsmodels .sandbox .regression .predstd import wls_prediction_std
77
88
9- def check_tolerance (X , y , to_exclude : int = 1 , poly_features : int = 2 , alpha : float = 0.05 ) -> pd .DataFrame :
9+ def check_tolerance (t , y , to_exclude : int = 1 , poly_features : int = 2 , alpha : float = 0.05 ) -> pd .DataFrame :
1010 """
1111 Check that some future values are within a weighted least squares confidence interval.
1212
1313 Parameters
1414 ----------
15- X : np.array
16- N independent variables of shape (N, 1).
15+ t : np.array
16+ N explanatory time bins of shape (N, 1).
1717 y : np.array
18- The corresponding dependent variable values to X, of shape (N, 1).
18+ 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 : int, default = 2
@@ -37,7 +37,7 @@ def check_tolerance(X, y, to_exclude: int = 1, poly_features: int = 2, alpha: fl
3737 Examples
3838 --------
3939 >>> check_tolerance(
40- ... X = np.array([1001,1002,1003,1004,1005,1006]),
40+ ... t = np.array([1001,1002,1003,1004,1005,1006]),
4141 ... y = np.array([2,3,4,4.5,5,5.1]),
4242 ... ).round(3).to_dict()
4343 {'yhat_u': {0: 6.061}, 'yobs': {0: 5.1}, 'yhat': {0: 5.2}, 'yhat_l': {0: 4.339}}
@@ -48,16 +48,16 @@ def check_tolerance(X, y, to_exclude: int = 1, poly_features: int = 2, alpha: fl
4848 if not isinstance (alpha , float ) or 0 >= alpha >= 1 :
4949 raise ValueError ("Please input a float between 0 and 1 for alpha." )
5050
51- N = len (X )
51+ N = len (t )
5252
5353 if not isinstance (to_exclude , int ) or N <= to_exclude < 1 :
5454 raise ValueError ("Please input an integer between 1 and your sample size to exclude." )
5555 if N < 4 :
5656 raise ValueError ("Your sample size is smaller than 4. This will not produce a good model." )
5757
5858 # Sort data by X increasing
59- idx = np .argsort (X )
60- X = X [idx ]
59+ idx = np .argsort (t )
60+ t = t [idx ]
6161 y = y [idx ]
6262
6363 transforms = make_pipeline (
@@ -66,19 +66,19 @@ def check_tolerance(X, y, to_exclude: int = 1, poly_features: int = 2, alpha: fl
6666 )
6767
6868 # Fit transforms to train data, apply them to all data
69- fitted_transforms = transforms .fit (X [:- to_exclude ].reshape (- 1 , 1 ))
70- X = fitted_transforms .transform (X .reshape (- 1 , 1 ))
69+ fitted_transforms = transforms .fit (t [:- to_exclude ].reshape (- 1 , 1 ))
70+ t = fitted_transforms .transform (t .reshape (- 1 , 1 ))
7171
72- X_train , y_train = X [:- to_exclude , :], y [:- to_exclude ]
73- X_predict , y_predict = X [- to_exclude :, :], y [- to_exclude :]
72+ t_train , y_train = t [:- to_exclude , :], y [:- to_exclude ]
73+ t_predict , y_predict = t [- to_exclude :, :], y [- to_exclude :]
7474
7575 # Fit ordinary least squares model to the training data, then predict for the
7676 # prediction data.
77- model = sm .OLS (y_train , X_train ).fit ()
78- yhat = model .predict (X_predict )
77+ model = sm .OLS (y_train , t_train ).fit ()
78+ yhat = model .predict (t_predict )
7979
8080 # Calculate confidence interval of fitted model.
81- _ , yhat_l , yhat_u = wls_prediction_std (model , X_predict , alpha = alpha )
81+ _ , yhat_l , yhat_u = wls_prediction_std (model , t_predict , alpha = alpha )
8282
8383 return pd .DataFrame ({
8484 "yhat_u" : yhat_u ,
0 commit comments