cal(d) 1995:01:03
open data "crude_oil.xls"
data(format=xls,org=cols) 1995:01:03 2011:12:30 return
sample(smpl=%valid(return)) return / return_nomissing
compute gend=%nobs
allocate gend
* Extract the mean from the returns.
*
diff(center) return_nomissing
*
@MSSetup(states=3)
*
* Provide guess values for theta
*
input theta
4.0 0.0 -4.0
2.0 2.0 -2.0
*
dec vect sigmas(nstates)
stats return_nomissing
compute sigmas(1)=0.2*%variance
compute sigmas(2)= %variance
compute sigmas(3)=5.0*%variance
*
*********************************************************************
*
* RegimeF returns a vector of likelihoods for the various regimes at
* <<time>>. The likelihoods differ in the regimes based upon the
* values of sigmas.
*
function RegimeF time
type vector RegimeF
type integer time
*
local integer i
*
dim RegimeF(nstates)
do i=1,nstates
compute RegimeF(i)=exp(%logdensity(sigmas(i), return_nomissing(time)))
end do i
end
*********************************************************************
nonlin(parmset=modelparms) sigmas
nonlin(parmset=msparms) theta
*
frml markov = f=RegimeF(t),fpt=%MSProb(t,f),log(fpt)
*
@MSFilterInit
maximize(start=%(p=%mslogisticp(theta),pstar=%MSInit()),$
parmset=msparms+modelparms,$
method=bfgs,iters=400,pmethod=simplex,piters=5) markov
*
@MSSmoothed %regstart() %regend() psmooth
set p1 = psmooth(t)(1)
set p2 = psmooth(t)(2)
set p3 = psmooth(t)(3)
graph(style=stacked,maximum=1.0,picture="##.##",$
header="Smoothed Probabilities of Variance Regimes",key=below,$
klabels=||"Low Variance","Medium Variance","High Variance"||) 3
# p1
# p2
# p3
*
set variance = p1*sigmas(1)+p2*sigmas(2)+p3*sigmas(3)
graph(footer="Figure 4.10 Estimated variance of historical stock returns")
# variance
set stdu = pricesqrt(variance)
graph(footer="Figure 4.11a Plot of standardized stock returns")
# stdusuperper2008 wrote:Hi Tom,
Thanks very much for your code. It works and I got the graph with high volatility state and low volatility state. But I have to obtain the exact dates of each state to perform the future analysis . For example, when does that high volatility state begin and when does it end? Can I get like a table which shows the dates of each state? Thanks very much
Regards
superper2008 wrote:Hi Tom,
I am wondering whether it is possible to pick up the dates with over 0.5 probability if i set up 0.5 as the standard to differentiate high volatility from low volatility..I am looking forward to your reply..Thanks very much
cal(d) 1995:01:03
open data "crude_oil.xls"
data(format=xls,org=cols) 1995:01:03 2011:12:30 return
sample(smpl=%valid(return)) return / return_nomissing
compute gend=%nobs
allocate gend
* Extract the mean from the returns.
*
diff(center) return_nomissing
*
@MSSetup(states=2)
*
* Provide guess values for theta
*
input theta
4.0 -4.0
*
dec vect sigmas(nstates)
stats return_nomissing
compute sigmas(1)=0.2*%variance
compute sigmas(2)=5.0*%variance
*
*********************************************************************
*
* RegimeF returns a vector of likelihoods for the various regimes at
* <<time>>. The likelihoods differ in the regimes based upon the
* values of sigmas.
*
function RegimeF time
type vector RegimeF
type integer time
*
local integer i
*
dim RegimeF(nstates)
do i=1,nstates
compute RegimeF(i)=exp(%logdensity(sigmas(i), return_nomissing(time)))
end do i
end
*********************************************************************
nonlin(parmset=modelparms) sigmas
nonlin(parmset=msparms) theta
*
frml markov = f=RegimeF(t),fpt=%MSProb(t,f),log(fpt)
*
@MSFilterInit
maximize(start=%(p=%mslogisticp(theta),pstar=%MSInit()),$
parmset=msparms+modelparms,$
method=bfgs,iters=400,pmethod=simplex,piters=5) markov
*
@MSSmoothed %regstart() %regend() psmooth
set p1 = psmooth(t)(1)
set p2 = psmooth(t)(2)
set highvar = p2>0.5
graph(style=stacked,maximum=1.0,picture="##.##",$
header="Smoothed Probabilities of Variance Regimes",key=below,$
klabels=||"Low Variance","High Variance"||) 2
# p1
# p2
*
set variance = p1*sigmas(1)+p2*sigmas(2)
graph(footer="Figure 4.10 Estimated variance of historical stock returns")
# variance
set stdu = pricesqrt(variance)
graph(footer="Figure 4.11a Plot of standardized stock returns")
# stduReturn to Structural Breaks and Switching Models
Users browsing this forum: No registered users and 1 guest