Building time series model
Example 41.1. Building time series model
tableName="stock_market"
targetName="price_change"
timeAttrName="time"
if not tableExists(tableName):
raise "Table '"+tableName+"' does not exists. Please run "+tableName+".py script from data directory first"
pd = PhysicalData(tableName)
ld = LogicalData(pd)
save('ts_pd', pd)
save('ts_ld', ld)
fs = TimeSeriesFunctionSettings()
#----- GARCH algorithm settings
as = GARCHSettings()
as.executeInitTests = TRUE
as.groupStatistics = FALSE
as.preselection = TRUE
opt = OptimizationAlgorithmSettings()
opt.convergenceCriterion = ConvergenceCriterion.likelihood
opt.convergenceThreshold = 0.001
opt.iterMax = 100
vss = VariableSelectionSettings()
vss.variableSelectionMethod = VariableSelectionMethod.full
vss.modelEntryLevel = 0.15
vss.modelLeaveLevel = 0.2
vss.groupMode = FALSE
as.variableSelectionSettings = vss
as.optimizationAlgorithmSettings = opt
fs.algorithmSettings = as
#----- time series function settings
fs.logicalData = ld
fs.targetAttributeName = targetName
fs.timePointAttributeName = timeAttrName
#fs.attributeUsageSet.getAttribute('season').setUsage(UsageOption.inactive)
#fs.attributeUsageSet.getAttribute('seeded').setUsage(UsageOption.inactive)
save('ts_settings', fs)
#----- building time series model
bt = MiningBuildTask('ts_pd', 'ts_settings', 'ts_model')
save('ts_build', bt)
execute('ts_build')
# print model statistics
statModel = load('ts_model').getModelStatistics()
varStat = statModel.variableStatistics
statNames = varStat.varStatNames
varNames=statModel.varNames
print " "*5,
for colName in statNames:
print "%10s"%colName[0:10],
print
for row in range(0,len(varNames)):
print "%5s"%varNames[row][0:20],
for col in range(0,len(statNames)):
value = (str(varStat.getVarStatValue(varNames[row],col)))
if value == "NaN": value = "%10s" % value
else: value = "%10.4f" % value
print value,
print
Output:
Coeff Univariate Lower Conf Upper Conf StdErr Wald Test Wald Pr>Ch alpha_0 -0.0000 0.0000 -0.0000 -0.0000 0.0000 14661.3023 0.0000 alpha_1 0.2157 0.0000 0.2154 0.2160 0.0002 1889852.0919 0.0000 beta_1 0.0653 0.0000 0.0652 0.0654 0.0000 2381730.0596 0.0000 Intercept -0.0001 0.0000 -0.0001 -0.0001 0.0000 72473.1732 0.0000
Time series model testing
Example 41.2. Time series model testing
tsTestTask = TimeSeriesTestTask()
tsTestTask.setTestResultName("tsTestResult")
tsTestTask.setModelName("ts_model")
tsTestTask.setTestDataName("ts_pd")
tsTestTask.setTestDataTargetAttributeName("price_change")
save('tsTestTask',tsTestTask)
execute('tsTestTask')
model = load('tsTestResult')
print "Absolute error:", "%f" % model.meanAbsoluteError
print "Actual value:", "%f" % model.meanActualValue
print "Predicted variance:", "%f" % model.meanPredictedVariance
Output:
Absolute error: 0.000335 Actual value: -0.000311 Predicted variance: 0.000077
Model application
Example 41.3. Time series model application
apply_pd = PhysicalData('stock_market_res')
save('stock_market_res_pd', apply_pd )
tSerAppTask = MiningApplyTask()
tSerAppTask.modelName = 'ts_model'
tSerAppTask.sourceDataName = 'ts_pd'
tSerAppTask.targetDataName = 'stock_market_res_pd'
tSerAppTask.replaceExistingData = TRUE
directMapping = java.util.ArrayList()
directMapping.add( ApplySourceItem('price_change','price_change') )
directMapping.add( ApplySourceItem('time','time') )
tSerAppTask.setDirectMapping(directMapping)
tSerAppOutput = TimeSeriesApplyOutput()
tSerAppTask.applyOutput = tSerAppOutput
save('tSer_apply_task',tSerAppTask)
execute('tSer_apply_task')
print "Print first 10 rows"
print "%-15s%-13s%-13s%-21s%-21s%-13s" % ('price_change', 'time', 'variance__1', 'forecast_lowerbound','forecast_upperbound', 'regressed_mean')
strFormat = "%-15.1f%-13.1d%-13.2f%-21.5s%-21.5s%-13.2f"
counter = 0
trans None<-'stock_market_res':
$counter+=1
if $counter>=10: __exit__=1
print $strFormat % (price_change, time, variance__1, $str(forecast_lowerbound),$str(forecast_upperbound), regressed_mean)
Output:
Print first 10 rows price_change time variance__1 forecast_lowerbound forecast_upperbound regressed_mean 0.0 1 0.00 -0.01 0.018 -0.00 -0.0 2 0.00 -0.00 0.004 -0.00 -0.0 3 0.00 -0.01 0.013 -0.00 -0.0 4 0.00 -0.00 0.006 -0.00 0.0 5 0.00 -0.00 0.002 -0.00 -0.0 6 0.00 -0.00 0.007 -0.00 -0.0 7 0.00 -0.00 0.002 -0.00 -0.0 8 0.00 -0.00 0.008 -0.00 0.0 9 0.00 -0.02 0.023 -0.00 0.0 10 0.00 -0.00 0.009 -0.00