# Copyright (c) 2016 Johan Dahlin [ johan.dahlin (at) liu.se ] # Distributed under the MIT license. # ############################################################################## ############################################################################## import numpy as np from state import smc from para import pmh_correlatedRVs from models import normalIID_2parameters ############################################################################## # Arrange the data structures ############################################################################## sm = smc.smcSampler(); pmh = pmh_correlatedRVs.stcPMH(); ############################################################################## # Setup the system ############################################################################## sys = normalIID_2parameters.ssm() sys.par = np.zeros((sys.nPar,1)) sys.par[0] = 0.50; sys.par[1] = 0.30; sys.par[2] = 0.10; sys.T = 10; sys.xo = 0.0; ##############################################################################
# # (c) 2016 Johan Dahlin # liu (at) johandahlin.com # ############################################################################## ############################################################################## import numpy as np from state import smc from para import gpo_gpy from models import hwsv_4parameters ############################################################################## # Arrange the data structures ############################################################################## sm = smc.smcSampler() gpo = gpo_gpy.stGPO() ############################################################################## # Setup the system ############################################################################## sys = hwsv_4parameters.ssm() sys.par = np.zeros((sys.nPar, 1)) sys.par[0] = 0.20 sys.par[1] = 0.90 sys.par[2] = 0.15 sys.par[3] = -0.70 sys.T = 1000 sys.xo = 0.0 sys.version = "standard"
def estVol(version, data, theta, settings): # Arrange the data structures sm = smc.smcSampler() #========================================================================= # Setup the system model and load data #========================================================================= if (version == 'GSV'): sys = hwsv_4parameters.ssm() else: sys = hwsvalpha_4parameters.ssm() sys.par = np.zeros((sys.nPar, 1)) sys.xo = 0.0 sys.T = len(data) sys.version = "standard" sys.transformY = "none" # Load data if (version == 'GSV'): sys.generateData() else: sys.par[3] = 2.0 sys.generateData() sys.ynoiseless = np.array(data).reshape((sys.T, 1)) sys.y = np.array(data).reshape((sys.T, 1)) #========================================================================= # Setup the model for inference #========================================================================= if (version == 'GSV'): th = hwsv_4parameters.ssm() th.nParInference = 3 th.transformY = "none" else: th = hwsvalpha_4parameters.ssm() th.nParInference = 4 th.transformY = "arctan" th.version = "standard" th.copyData(sys) #========================================================================= # Setup the SMC algorithm #========================================================================= if (version == 'GSV'): sm.filter = sm.bPF else: sm.filter = sm.bPFabc sm.weightdist = settings['smc_weightdist'] sm.tolLevel = settings['smc_tolLevel'] # Add noise to data for noisy ABC th.makeNoisy(sm) sm.nPart = settings['smc_nPart'] sm.genInitialState = True # Estimate the log-volatility th.storeParameters(theta, sys) sm.filter(th) # Return the log-volatility estimate return sm.xhatf[:, 0]
def estModel(version, data, settings): # Arrange the data structures gpo = gpo_gpy.stGPO() sm = smc.smcSampler() #========================================================================= # Setup the system model and load data #========================================================================= if (version == 'GSV'): sys = hwsv_4parameters.ssm() else: sys = hwsvalpha_4parameters.ssm() sys.par = np.zeros((sys.nPar, 1)) sys.xo = 0.0 sys.T = len(data) sys.version = "standard" sys.transformY = "none" # Load data if (version == 'GSV'): sys.generateData() else: sys.par[3] = 2.0 sys.generateData() sys.ynoiseless = np.array(data).reshape((sys.T, 1)) sys.y = np.array(data).reshape((sys.T, 1)) #========================================================================= # Setup the model for inference #========================================================================= if (version == 'GSV'): th = hwsv_4parameters.ssm() th.transformY = "none" th.nParInference = 3 else: th = hwsvalpha_4parameters.ssm() th.transformY = "arctan" th.nParInference = 4 th.version = "standard" th.copyData(sys) #========================================================================= # Setup the SMC algorithm #========================================================================= if (version == 'GSV'): sm.filter = sm.bPF else: sm.filter = sm.bPFabc sm.weightdist = settings['smc_weightdist'] sm.tolLevel = settings['smc_tolLevel'] # Add noise to data for noisy ABC th.makeNoisy(sm) sm.nPart = settings['smc_nPart'] sm.genInitialState = True #========================================================================= # Setup the GPO algorithm #========================================================================= gpo.verbose = True gpo.initPar = settings['gpo_initPar'][0:th.nParInference] gpo.upperBounds = settings['gpo_upperBounds'][0:th.nParInference] gpo.lowerBounds = settings['gpo_lowerBounds'][0:th.nParInference] gpo.preIter = settings['gpo_preIter'] gpo.maxIter = settings['gpo_maxIter'] gpo.jitteringCovariance = 0.01 * np.diag(np.ones(th.nParInference)) gpo.preSamplingMethod = "latinHyperCube" gpo.EstimateHyperparametersInterval = settings['gpo_estHypParInterval'] gpo.EstimateThHatEveryIteration = False gpo.EstimateHessianEveryIteration = False # Estimate parameters gpo.bayes(sm, sys, th) gpo.estimateHessian() # Estimate the log-volatility th.storeParameters(gpo.thhat, sys) sm.filter(th) return sm.xhatf[:, 0], gpo.thhat, np.diag(-gpo.invHessianEstimate)