Utility Package
Example 33.2 Computing Prediction Standard Errors and Confidence Limits for an Ad Hoc External Forecast
This example uses the TSMODEL procedure to compute the prediction standard errors and confidence limits of an ad hoc forecast that includes an actual and predicted time series. The example starts by using a DATA step to create a synthetic data set called ExternalModel. The synthetic data set contains three time series that make up an ad hoc external model (that is, a user-defined model) forecast: an actual series called Air, a simulated forecast error series called Error, and a simulated predicted series called Predict. This synthetic, ad hoc external forecast lacks the prediction standard errors and confidence limits, which will be computed by the CLIMITS object. Notice from the DATA step code that the actual time series is simply a copy of the Air series taken from the Sashelp.Air data set. The predicted time series is generated by simply adding a small amount of noise to the actual Air series. The added noise corresponds to random samples taken of the uniform distribution (which ranges from 0.0 to 1.0) multiplied by a factor of 10. Thus, the added noise samples range from 0.0 to 10.0 and have an expected value of 5.0. This expected value is important because it corresponds to the average forecast error of the simulated predicted series, a value that over many samples should approximate the prediction standard errors that will be computed by the CLIMITS object for this synthetic, ad hoc external forecast. Next, the synthetic data set is uploaded to a table and the TSMODEL procedure is invoked. The actual and predicted time series are input to the CLIMITS object’s Compute method to compute the prediction standard errors and confidence limits of the ad hoc external forecast. The CLIMITS object’s GetForecast method is then used to retrieve the resulting three forecast series into numeric arrays that are defined in the user program. Finally, the OUTNVP collector object collects a total of six series into a table: the actual Air series, the simulated predicted and forecast error series, and the three forecast series that were computed by the CLIMITS object. This was similarly done in Example 33.1 to store into a table the actual series plus the five forecast series that were retrieved from the TSM object via its own GetForecast method. Finally, some results are retrieved from tables, sorted, and printed for further inspection:
The following DATA step creates a synthetic data set that contains actual and simulated predicted time series:
data ExternalModel (replace=yes);
set Sashelp.Air; /* The actual series: Sashelp.Air */
error = floor(10*ranuni(246)); /* Simulated forecast error series */
predict = air + error; /* Simulated predicted series */
run;
The following DATA step loads the ExternalModel data set onto the CAS server. This DATA step assumes that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
data mylib.ExternalModel;
set ExternalModel;
run;
The following statements use the TSMODEL procedure to compute the prediction standard errors and confidence limits of the ad hoc external forecast that resides in the synthetic data set:
proc tsmodel data = mylib.externalmodel
outarray = mylib.outarray (replace=yes)
outscalar = mylib.outscalar (replace=yes)
outlog = mylib.outlog (replace=yes)
outobj=( outnvpmodelobj = mylib.outnvpmodel (replace = YES) );
id date interval=month start='01jan1949'd end='01dec1960'd;
var air predict error;
outarrays stderr lcl ucl;
outscalar rc1 rc2 rc3;
require utl;
submit;
/* Compute the prediction standard errors and confidence limits */
declare object clim(CLIMITS);
rc1 = clim.Compute(air,predict,0.05); /* Confidence level: 0.05 */
if rc1 < 0 then do; stop; end;
/* Retrieve the forecast series stored internally in the CLIMITS object */
rc2 = clim.GetForecast('stderr',stderr); /* Prediction sdt. errors series */
if rc2 < 0 then do; stop; end;
rc2 = clim.GetForecast('lower',lcl); /* Lower conf. limits series */
if rc2 < 0 then do; stop; end;
rc2 = clim.GetForecast('upper',ucl); /* Upper conf. limits series */
if rc2 < 0 then do; stop; end;
/* Collect the actual, predicted, and forecast error series, in addition */
/* to the forecast series computed by the CLIMITS object into a table*/
declare object outnvpmodelobj(outnvp);
rc3 = outnvpmodelobj.Collect(air,_SERIES_);
if rc3 < 0 then do; stop; end;
rc3 = outnvpmodelobj.Collect(predict,_SERIES_);
if rc3 < 0 then do; stop; end;
rc3 = outnvpmodelobj.Collect(error,_SERIES_);
if rc3 < 0 then do; stop; end;
rc3 = outnvpmodelobj.Collect(stderr,_SERIES_);
if rc3 < 0 then do; stop; end;
rc3 = outnvpmodelobj.Collect(lcl,_SERIES_);
if rc3 < 0 then do; stop; end;
rc3 = outnvpmodelobj.Collect(ucl,_SERIES_);
if rc3 < 0 then do; stop; end;
endsubmit;
run;
Following what was done in Example 33.1, you can use the PRINT procedure to display a small subset of observations from the following six different series that were collected by the OUTNVP object into the mylib.outnvpmodel table:
The actual series (
Airseries) of the ad hoc external forecast.The simulated predicted series of the ad hoc external forecast.
The simulated forecast error series of the ad hoc external forecast.
The prediction standard errors series that were computed by the CLIMITS object for the ad hoc external forecast.
The lower confidence limits series that were computed by the CLIMITS object for the ad hoc external forecast.
The upper confidence limits series that were computed by the CLIMITS object for the ad hoc external forecast.
You can print in sequence the values of the first three observations in each of these six series, for a total of 18 rows. To accomplish this, you must sort the mylib.outnvpmodel table in a manner that sequentially aligns all rows that correspond to each unique observation index in all collected series (all rows that correspond to the first observation in all six series, followed by all rows that correspond to the second observation in all six series, and so on). You can use the SORT procedure to simultaneously sort a table and store the results in a local data set as follows:
/* Sort OUTNVP table by "row index" and "BY group Collect() call count" */
/* (that is, _VIX_ and _CALL_ columns). */
proc sort data=mylib.outnvpmodel out=outnvpmodel;
by _VIX_ _CALL_;
run;
/* Print the value of the first 18 rows, which corresponds to the values */
/* of the first 3 observations in the six collected series. These 18 rows */
/* correspond to the condition "1 <= _VIX_ <= 3" in the table called */
/* "mylib.OUTNVPMODEL". */
proc print data=outnvpmodel(obs=18);
run;
Output 33.2.1: Sample of the Six Ad Hoc Series Collected by the OUTNVP Object
| Obs | _VAR_ | _CALL_ | _UTAG_ | _VIX_ | _VALUE_ |
|---|---|---|---|---|---|
| 1 | AIR | 1 | 0 | 1 | 112 |
| 2 | predict | 2 | 0 | 1 | 117 |
| 3 | error | 3 | 0 | 1 | 5 |
| 4 | stderr | 4 | 0 | 1 | 5.0408057116 |
| 5 | lcl | 5 | 0 | 1 | 107.12020235 |
| 6 | ucl | 6 | 0 | 1 | 126.87979765 |
| 7 | AIR | 1 | 0 | 2 | 118 |
| 8 | predict | 2 | 0 | 2 | 124 |
| 9 | error | 3 | 0 | 2 | 6 |
| 10 | stderr | 4 | 0 | 2 | 5.0408057116 |
| 11 | lcl | 5 | 0 | 2 | 114.12020235 |
| 12 | ucl | 6 | 0 | 2 | 133.87979765 |
| 13 | AIR | 1 | 0 | 3 | 132 |
| 14 | predict | 2 | 0 | 3 | 135 |
| 15 | error | 3 | 0 | 3 | 3 |
| 16 | stderr | 4 | 0 | 3 | 5.0408057116 |
| 17 | lcl | 5 | 0 | 3 | 125.12020235 |
| 18 | ucl | 6 | 0 | 3 | 144.87979765 |
The sequence displayed in Output 33.2.1 was obtained by using the SORT procedure to sort the data in the OUTNVP table in increasing order of the columns _VIX_ (that is, the series row index) and _CALL_ (that is, the OUTNVP object’s Collect method call count within BY group). For example, rows 1–6 contain the values of the first observation in each of the six collected series, which correspond to the rows in the mylib.outnvpmodel table where column _VIX_ = 1: row 1 is the value of the first observation in the actual Air series (that is, column _VAR_ = AIR), rows 2–3 are the values of the first observations in the simulated Predict and Error series of the ad hoc external forecast, and rows 4–6 are the values of the first observations in the StdErr, LCL, and UCL forecast series that were computed by the CLIMITS object for the ad hoc external forecast and retrieved via its GetForecast method. The same applies for the second observation in each of the six collected series as described by rows 7–12 (that is, rows where column _VIX_ = 2) and for the third observation in each of the six collected series as described by rows 13–18 (rows where column _VIX_ = 3).
Notice also in Output 33.2.1 the value of 5.0408057116, which is reported for the first three observations of the prediction standard errors series, as shown by rows where column _VAR_ = stderr. This reported value should be close to the expected value of the simulated forecast error of the synthetic external forecast (5.0).