Examples / DLMEXAM3.RPF |
DLMEXAM3.RPF is an example of unconditional simulation of a state-space model. This uses the model for Nile river flow data common to the four simple DLM examples.
We first Kalman filter through the data period, which will give us the end-of-data mean and variance for the state.
dlm(a=1.0,c=1.0,sv=15099.0,sw=1469.1,presample=diffuse,y=nile,$
type=filter) / xstates vstates
We then simulate (10000 times) the model for 50 periods beyond the end of data observed data, keeping track of the maximum value achieved by the flow in each simulation. The DLM instruction that does the simulation uses the X0 and SX0 options for inputting the initial mean and variance as the final values from the Kalman filter. The output is through the YHAT option.
compute ndraws=10000
set maxflow 1 ndraws = 0.0
do draw=1,ndraws
dlm(a=1.0,c=1.0,sv=15099.0,sw=1469.1,$
x0=xstates(1970:1),sx0=vstates(1970:1),$
type=simulate,yhat=yhat) 1971:1 2020:1 xstates
set simflow 1971:1 2020:1 = %scalar(yhat)
ext(noprint) simflow
compute maxflow(draw)=%maximum
end do reps
Full Program
open data nile.dat
calendar 1871
data(format=free,org=columns,skips=1) 1871:1 1970:1 nile
*
* Kalman filter through the data period
*
dlm(a=1.0,c=1.0,sv=15099.0,sw=1469.1,presample=diffuse,y=nile,$
type=filter) / xstates vstates
*
* Simulate 10000 realizations of 50 data points from the end of the
* data, keeping track of the maximum flow for each.
*
compute ndraws=10000
set maxflow 1 ndraws = 0.0
do draw=1,ndraws
dlm(a=1.0,c=1.0,sv=15099.0,sw=1469.1,$
x0=xstates(1970:1),sx0=vstates(1970:1),$
type=simulate,yhat=yhat) 1971:1 2020:1 xstates
set simflow 1971:1 2020:1 = %scalar(yhat)
ext(noprint) simflow
compute maxflow(draw)=%maximum
end do reps
*
* Compute the percentiles of the simulated maximum flows.
*
stats(fractiles,nomoments) maxflow
Output
These use random numbers, so will not be directly reproducible.
Statistics on Series MAXFLOW
Annual Data From 1871:01 To 11870:01
Observations 10000
Minimum 582.448871 Maximum 2136.309977
01-%ile 789.668682 99-%ile 1668.108450
05-%ile 878.492495 95-%ile 1494.057919
10-%ile 932.974550 90-%ile 1407.535441
25-%ile 1022.242511 75-%ile 1272.742814
Median 1134.038069
Copyright © 2025 Thomas A. Doan