Skip to content

Commit 8be258e

Browse files
committed
Replace parameter forecast with predict_all
1 parent 07bf6a6 commit 8be258e

2 files changed

Lines changed: 15 additions & 51 deletions

File tree

codonPython/tests/tolerance_test.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import pytest
66

77

8-
@pytest.mark.parametrize("t, y, to_exclude, poly_features, alpha, forecast, expected", [
8+
@pytest.mark.parametrize("t, y, to_exclude, poly_features, alpha, expected", [
99
(
1010
np.array([1234,1235,1236,1237,1238,1239,1240,1241,1242]),
1111
np.array([1,2,3,4,5,5.5,6,6.5,7]),
1212
2,
1313
[1, 2],
1414
0.05,
15-
False,
1615
pd.DataFrame({
1716
'yhat_u': [
1817
8.11380197739608,
@@ -42,48 +41,31 @@
4241
2,
4342
[3],
4443
0.05,
45-
True,
4644
pd.DataFrame({
4745
'yhat_u': [
4846
6.753927165005773,
4947
7.214574732953706,
50-
7.218216279340497,
51-
7.3980478897523225,
52-
7.575355558900247,
53-
7.750571186675458,
54-
7.924372393971117
5548
],
56-
'yobs': [6.5, 7.0, np.nan, np.nan, np.nan, np.nan, np.nan],
49+
'yobs': [6.5, 7.0],
5750
'yhat': [
5851
6.0000000000000036,
5952
5.571428571428576,
60-
5.5659511039999785,
61-
5.249235468428623,
62-
4.842301211809431,
63-
4.339894601476199,
64-
3.7367619047620617
6553
],
6654
'yhat_l': [
6755
5.2460728349942345,
6856
3.928282409903445,
69-
3.9136859286594596,
70-
3.100423047104923,
71-
2.1092468647186156,
72-
0.929218016276939,
73-
-0.45084858444699405
7457
],
75-
'polynomial': [3, 3, 3, 3, 3, 3, 3]
58+
'polynomial': [3, 3]
7659
})
7760
),
7861
])
79-
def test_tolerance_checking_BAU(t, y, to_exclude, poly_features, alpha, forecast, expected):
62+
def test_tolerance_checking_BAU(t, y, to_exclude, poly_features, alpha, expected):
8063
obtained = check_tolerance(
8164
t,
8265
y,
8366
to_exclude=to_exclude,
8467
poly_features=poly_features,
8568
alpha=alpha,
86-
forecast=forecast,
8769
)
8870
assert expected.equals(obtained)
8971

codonPython/tolerance.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@
66
from 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

Comments
 (0)