gpt4 book ai didi

r - 在 R 中使用 optim 与 GARCH 收敛

转载 作者:行者123 更新时间:2023-12-03 17:17:51 27 4
gpt4 key购买 nike

我试图通过使用 optim 函数在 R 中找到最佳 GARCH 模型的参数。但是,我的值(value)观会变得很高,这是没有意义的。我在 MATLAB 中使用 fminsearch 实现了类似的算法,效果更好。但是,我想在 R 中工作。

这是我的代码:

    lklh.garch = function(theta){
value = 0
value = log(dnorm(returns[1], 0, theta[4]))
sigma = theta[4]^2

for(i in 2:1001) {
variance = (1-theta[1]-theta[2])*theta[3]+theta[1]*returns[i-1]^2+theta[2]*sigma
value = value + log(dnorm(returns[1], 0, sqrt(variance)))
sigma = variance
}
return(-value)
}
result = optim(c(.03, .9, sd(returns), sd(returns[980:1001])), fn=lklh.garch, method="BFGS", lower=c(0,0,0,0))
returns 中的值为:
> dput(returns)
c(-0.014786688, -0.003869546, 0.007998939, 0.036151765, -0.059780384,
0.003369843, 0.019021747, 0.017586167, -0.002841872, -0.022060222,
0.000415541, -0.021840431, 0.014892099, 0.058497791, 0.031970626,
-0.016569642, 0.026583035, 0.049456573, -0.013413288, -0.002558013,
0.014890415, 0.077210238, -0.005353571, -0.02433761, -0.034862172,
-0.020255981, -0.003153047, 0.043262837, -0.010523951, 0.051545995,
-0.03325884, -0.002195391, 0.003003005, 0.018397365, 0.009690217,
-0.036542088, -0.03768665, 0.031620369, -0.106974007, 0.009977407,
-0.026257142, -0.033239957, 0.015478924, 0.030126358, 0.030514115,
0.001520527, -0.007498288, 0.006358109, 0.003921822, 0.031200095,
0.00573557, -0.010028212, -0.001599115, -0.003700054, 0.001851738,
0.014934786, -0.027594754, 0.004610887, -0.020474244, -0.014959046,
0.021914497, 0.027470499, -0.012833345, 0.007547206, 0.039869982,
0.019470472, 0.006368342, -0.006484506, 0.023649363, 0.016093983,
0.005677722, -0.0062361, 0.011881768, 0.012505648, -0.000436157,
-0.003934001, -0.008356288, 0.027121414, 0.021894806, -0.010037593,
0.000530814, 0.002332239, 0.018152747, -0.002707207, -0.007850566,
-0.017277445, -0.021943268, -0.007019112, 0.00406393, 0.009056748,
0.011985243, -0.005488901, -0.013691402, 0.000765571, -0.009446467,
-0.035840493, 0.000571788, -0.004353811, 0.021132347, -0.005976226,
-0.001584607, -0.01173602, -0.002754507, -0.013654459, -0.002683313,
0.016452694, -0.017036977, 0.028122171, 0.012087128, -0.029744088,
0.002310804, 0.00265084, 0.01235431, -0.005814964, -0.010807187,
0.029948153, 0.002464987, 0.020710636, -0.002744088, 0.010713998,
-0.011593699, -0.016751182, -0.003249485, -0.023851216, -0.020906685,
0.002227564, 0.018564172, 0.00493092, -0.002061148, -0.016410862,
0.009394096, -0.004164263, -0.019784081, -0.007000088, 0.031179272,
-0.009741466, 0.021672318, -0.004801107, -0.000573099, 0.022671567,
0.01169995, -0.000221582, 0.004091342, 0.021182144, 0.00764143,
-0.017412434, -0.006018511, 0.00208322, 0.005134659, 0.005650346,
-0.021244776, 0.009034879, -0.010142298, -0.009239226, -0.001678886,
-0.027367858, 0.040057254, 0.016128497, 0.017050159, 0.003631319,
-0.011472811, -0.02877188, -0.002110995, 0.034220338, 0.0365135,
0.027292541, -0.004649291, -0.003755396, -0.013616796, -0.010570047,
-0.001250912, 0.002292145, 0.004257756, 0.012563238, -0.026336411,
0.021412314, -0.010960714, -0.001977416, -0.012685603, -0.011353509,
-0.010405028, 0.006984394, -0.001500215, -0.000107244, -0.017963907,
-0.026892563, 0.006261901, -0.003014236, -0.001230632, 0.022141943,
-0.003619814, -0.012717893, -0.010067199, -0.014380591, 0.016065511,
0.002801907, 0.022684237, 0.00719506, -0.009934037, 0.006560986,
-0.015930027, -0.002439296, -0.024612132, 0.003180737, -0.00854268,
-0.010579675, 0.00230947, 0.014769288, 0.017462281, -0.010779354,
-0.003732403, -0.019451414, 0.002653899, -0.023434474, 0.004237793,
0.005155255, -0.008331915, -0.028689512, 0.006527281, -0.017501662,
-0.000490557, -0.017073284, -0.000499251, -0.018013966, 0.007219333,
0.000126191, -0.014233307, 0.004979263, -0.000254745, 0.025034328,
-0.020712471, 0.014606152, -0.017654935, 0.009748764, -0.009367157,
0.009745062, -0.014206226, -0.012339488, 0.012339488, 0.016344995,
0.006638717, 0.014870162, -0.000615195, -0.023536358, 0.001385129,
-0.028462134, 0.006581093, 0.002569044, 0.029452928, -0.014554837,
-0.010418084, 0.04836436, 0.000608236, 0.000121603, -0.009898046,
-0.020847014, 0.001002632, -0.024088386, 0.008815132, -0.016285543,
0.009905529, -0.005519557, -0.02159855, -0.006730477, -0.003847434,
-0.00840958, 0.014637653, 0.00395518, -0.019934215, 0.00828771,
0.000266205, -0.00734659, -0.009158313, -0.007469309, -0.003687269,
-0.007691289, 0.011923666, -0.007246904, -0.013400767, 0.006239188,
0.021197375, -0.005291377, -0.011080067, -0.009675266, 0.004572862,
-0.000691515, 0.000829761, 0.005239223, -0.017898501, 0.003772798,
-0.010233495, -0.013904878, 0.001285072, 0.005407726, 0.000851185,
-0.012844475, -0.003309117, -0.007667307, -0.019059366, -0.001777778,
-0.013735659, -0.010882816, 0.004094948, -0.007291541, 0.00592842,
0.001968356, -0.012174857, 0.001377095, -0.005058645, 0.000768108,
-0.001690617, -0.00508907, 0.022170467, 0.002416555, -0.000452659,
0.004667629, -0.016814757, 0.019814609, -0.004352727, 0.000601504,
0.002552362, -0.012068332, 0.000606888, -0.002125722, -0.000304044,
-0.005488655, 0.003662449, 0.018861366, 0.008484088, 0, 0.001629027,
-0.008470219, -0.005837029, -0.013145162, 0.001064072, 0.003185441,
-0.000151458, -0.008824034, 0.002594431, -0.003511722, -0.006444705,
-0.008347552, 0.004955105, 0.004315666, -0.011601955, -0.000155606,
-0.00922386, -0.000785608, 0.012651477, 0.005417551, -0.00340189,
0.002320725, 0.011829003, -0.008127009, -0.001540832, -0.0038625,
-0.001084179, -0.006685868, 0.009316838, -0.007446513, 0.016064603,
0.017468356, -0.0006025, 0.002558123, -0.001503986, -0.013638641,
-0.012590371, 0.005393341, -0.001999847, -0.002930066, 0.012126943,
-0.005201174, 0.007182731, -0.002286761, -0.007045518, 0.008265775,
0.014227575, -0.003915078, 0.002561593, -0.006795043, 0.021731841,
0.006354858, 0.005435994, -0.000879508, -0.010318488, -0.012374366,
0.000449944, 0.005979985, 0.00698109, -0.010265656, 0.015138312,
-0.002359535, 0.00779473, 0.006425256, -0.017177256, 0.012508441,
-0.005572679, 0.005865119, -0.008220833, -0.012608637, 0.001342582,
0.001191895, -0.009424858, 0.004947907, -0.000748111, -0.014777108,
0.000303767, -0.00167186, -0.001369967, 0.00607443, -0.005313105,
0.002128631, -0.010535253, 0.009319447, 0.000911993, -0.008391231,
-0.008153264, -0.001855288, -0.009953426, -0.000625391, -0.003446659,
0.010149201, -0.001865962, 0.003573374, -0.002795467, -0.00233554,
-0.019201094, 0.012318539, 0.032582207, 0.009977407, -0.016225999,
0.040302061, -0.005595656, -0.008452634, 0.00297398, -0.030757739,
0.015949301, 0.000753182, -0.0006025, -0.008017591, -0.002585356,
0.006829073, -0.009115833, -0.01367674, 0.002472571, -0.00743613,
-0.000466599, 0.014517629, -0.005997711, -0.009453771, -0.007031828,
0.006252951, -0.001715668, 0.004205941, -0.001555694, -0.023790015,
0.00445435, -0.011815561, 0.006084887, -0.010753655, 0.003222171,
-0.025082748, -0.000659957, 0.004281956, -0.006099088, -0.005138843,
0.02429934, 0.010167117, 0.01482217, -0.004916353, -0.010869672,
-0.003864739, -0.005338523, -0.00438918, 0.003902444, 0.002916871,
-0.012373977, -0.000655523, 0.018194134, 0.005778507, 0.00064,
0.000799424, -0.008345418, 0.01089929, -0.000637857, 0.017707973,
0.00484641, 0, 0.004202014, -0.019287152, 0.006470472, 0.002042902,
-0.003144657, -0.015873349, -0.003686788, -0.008061959, 0.000161878,
-0.007636726, 0.008607443, -0.003401639, 0.00904545, 0.004812329,
0.007334217, -0.029665242, 0.015103819, -0.002420723, 0.00660281,
-0.006118197, -0.009085072, -0.018421574, -0.005660078, -0.008382279,
-0.001853569, -0.008979306, -0.010091593, -0.007073263, 0.017505166,
0.010660901, -0.013898529, 0.001023542, 0.008827082, -0.010192037,
-0.0186116, -0.010667234, 0.002107482, 0.003502631, -0.003678085,
-0.001229041, -0.009709671, 0.013042131, 0.035097358, 0.005395393,
0.005366439, 0.000334448, -0.018051945, -0.004094172, 0, -0.009446184,
-0.01197827, -0.005955526, 0.022753913, -0.014356383, -0.003665245,
0.024696877, -0.005130848, 0.003081667, 0.035927987, 0.010009108,
0.025946182, 0.007922716, -0.019442844, 0.002410995, -0.014065384,
-0.007845741, 0.000984091, 0.012543946, -0.000323834, 0.001294708,
0.003551828, 0.006265584, -0.022676709, -0.00016384, 0.023640997,
-0.007710882, 0.003862873, 0.007362389, 0.008574203, 0.021583572,
-0.010264476, 0.008251, -0.003416683, -0.002492213, 0.009777373,
-0.006663076, -0.018199435, -0.000791954, -0.003333072, -0.014411779,
-0.007284532, -0.006683536, 0.009279676, 0.01336036, 0.023545372,
-0.01780556, 0.016555364, 0.0292779, -0.004566218, -0.009657468,
-0.017245825, -0.028453004, 0.009787483, 0.00620774, -0.00828558,
-0.005133152, -0.015396139, -0.028324875, -0.033832032, 0.002430135,
0.010348488, -0.004988399, -0.004493613, 0.010339572, -0.017815917,
-0.016540937, 0.023150806, -0.00626089, -0.030106754, 0.005379249,
0.070922339, 0.00697214, 0.007580788, 0.022242982, 0.00432589,
0.011761102, 0.003312567, -0.01491381, 0.026344295, 0.013607756,
-0.000153598, -0.006163348, -0.009941057, 0.035575662, -0.01640377,
0.004278735, 0.028264058, 0.010176329, 0.006727139, -0.017644922,
0.053575216, -0.001266268, -0.009193191, -0.009995799, -0.030897241,
0.006344544, -0.007529378, -0.015230995, 0.001353282, -0.021107761,
-0.001074856, -0.033271323, 0.010899721, -0.013762774, -0.018163322,
0.024831072, -0.03608639, 0.044751273, -0.030257085, -0.012363912,
-0.026371874, -0.001177163, -0.007092228, 0.009948653, -0.017091541,
0.011707947, -0.010513917, -0.005641523, 0.002397261, -0.006348138,
0.008569033, -0.026456112, -0.017856039, 0.005869292, -0.012132174,
-0.018295955, 0.000913659, 0.001460121, 0.006181838, -0.000906701,
0.003983348, -0.003983348, -0.004545876, -0.010258379, -0.008877439,
0.007587711, -0.004990307, 0.017995344, 0.005082603, 0.018832948,
0.008140197, -0.011343617, -0.000534902, -0.015275704, 0.007577161,
-0.018136152, 0.012729759, -0.003983348, -0.002906979, -0.008038041,
-0.014037911, 0.013487502, -0.014231827, 0.019722324, 0.025053126,
-0.024323332, -0.004203607, 0.038092999, -0.003886244, 0.008459691,
-0.004926118, 0, -0.000352796, -0.018698791, 0.00448391, 0.004642037,
-0.007329969, 0.000717489, -0.010273137, 0.007040376, -0.027723826,
0.021769816, 0.003252035, -0.018385882, 0.001835536, -0.008656468,
0.009939342, -0.016248511, -0.026024475, 0.018175434, -0.003382826,
-0.029419505, -0.002134887, 0, -0.016257336, -0.010321648, 0.000398963,
-0.01547614, -0.006706661, 0.023777062, -0.002591966, 0.002990135,
0.002187097, 0.025491576, 0.017465179, -0.003048783, -0.009587801,
-0.01123825, -0.008414099, -0.002163864, -0.009099964, -0.002985374,
-0.021558204, 0.014155949, -0.001205546, -0.006454238, -0.005478354,
0.003452832, -0.006713483, -0.01087748, 0.000412626, -0.0145442,
-0.023719875, -0.000857633, 0.003853569, -0.00686109, 0.001719691,
-0.014058845, -0.003054104, -0.005696773, 0.009839371, 0.003692045,
-0.00870896, 0.014544922, 0.017518072, -0.000847458, -0.005312944,
0.002766253, -0.014339465, 0.002798409, -0.011675808, 0.007799,
0.014142083, 0.00551667, -0.009781069, 0.00468984, -0.003622034,
0.011460227, -0.000422119, -0.000211126, -0.003172256, -0.018168716,
0.001939447, -0.000646064, 0.006013764, 0.008528836, -0.006389798,
-0.011821739, 0.006895093, -0.006246655, 0.001727116, 0.014986365,
-0.017794445, 0.000432526, -0.009340787, -0.00876814, -0.010179331,
-0.00893661, 0.007378457, -0.001114455, -0.005366739, -0.00607221,
-0.001354402, -0.000451875, -0.000678196, -0.018717734, -0.009026794,
0.012936193, 0.000917642, 0.002747884, -0.003206599, 0.003892392,
-0.001372056, -0.002290952, 0.005261364, 0.00182357, 0.014470069,
-0.009472331, 0.008124622, -0.000899483, -0.01816168, -0.007127286,
0.000230707, 0.005750447, 0.001833181, -0.003439976, -0.008999715,
-0.00023183, -0.007213527, -0.001636088, -0.007277882, -0.00448696,
0.006134989, -0.00141243, 0.000235544, -0.006142235, -0.007373086,
0.00286055, 0.000951701, -0.003573559, 0.010682594, 0.006824359,
-0.00234797, -0.006603798, 0.002363509, -0.002600167, 0.000709807,
0.009884759, 0.017181755, 0.004364312, -0.004134135, 0.010531233,
-0.000227764, 0.002729756, -0.006838413, 0.002056439, -0.003429751,
-0.000687364, -0.003904012, -0.016705725, -0.003280997, 0.00421645,
-0.003747077, -0.008246012, 0.001654651, 0.000944287, 0.002356824,
-0.007324309, -0.001661327, -0.013631683, 0.002885311, 0.005507017,
-0.004547094, 0.004308288, -0.003349285, -0.002639474, 0.000720548,
0.007891946, 0.001666072, -0.004528669, 0.000477669, 0.001670047,
0.004993471, 0.012492797, -0.001171921, -0.002112925, -0.000705302,
0.009130346, -0.000233073, -0.00326874, 0.002802431, 0.010901186,
-0.008106588, 0.00464038, 0.010133666, -0.004593485, -0.001151676,
-0.004620013, 0.004850454, -0.002768807, -0.013725934, -0.006816339,
-0.002124898, 0.013848361, -0.00139958, -0.001401542, -0.002106003,
-0.002814921, 0.001408451, -0.004231318, -0.001178551, 0.005175264,
0.004681656, -0.004916316, -0.001174122, -0.000470035, -0.003532324,
0.000707464, 0.005407325, 0.010263678, 0.018395552, -0.00159617,
0.011120051, -0.005658044, -0.000681122, 0.009043697, -0.005190128,
-0.004080713, 0.000227144, -0.012800175, -0.00207254, -0.009496308,
0.006495036, -0.000693882, 0.006917251, -0.000459664, -0.003915702,
0.002074929, 0.003448676, -0.018999645, -0.000467946, -0.004926696,
0.001410106, -0.000704804, -0.011107287, -0.007873117, -0.01059485,
-0.001211094, 0.000242336, 0.00483443, 0.005051123, 0.011450507,
-0.001424164, 0.002846302, 0.011070658, -0.001641074, 0.001641074,
-0.010597061, 0.012469283, -0.000701672, -0.011530896, 0.000236658,
0.001418775, 0, 0.006594466, 0, -0.009197093, -0.006178727, -0.002625613,
0.007381865, 0.012261413, 0.001639153, 0.006529874, 0.017968694,
0.000684697, -0.003886163, 0.016131214, 0.002476085, -0.004732403,
-0.005663171, 0.004533099, 0.002710029, -0.003388685, -0.011608188,
0.007072004, -0.010052632, 0.008232381, -0.010761409, 0.007796416,
0.005239789, 0.024687071, 0.001329198, 0.009474567, -0.014356956,
0.009520718, -0.005745872, 0.006186498, -0.002425847, -0.005313274,
-0.006681539, 0.004459316, -0.00378915, -0.008972694)

我得到的警告和错误是: Error in optim(c(0.03, 0.9, sd(returns), sd(returns[980:1001])), fn = lklh.garch, :
L-BFGS-B needs finite values of 'fn'

最佳答案

有一个警告告诉你 BFGS不使用您提供的下限
并建议L-BFGS反而。但是L-BFGS也需要有限值。

错误是关于非有限值( NaN ),
来自负面variance ,可能在 theta[1] + theta[2] > 1 时发生--
你需要theta[1] + theta[2] < 1使过程平稳。

您可以返回 Inf在这些情况下和
使用允许无限值的优化算法。

lklh.garch = function(theta){
value <- log(dnorm(returns[1], 0, theta[4]))
sigma <- theta[4]^2
for(i in seq_along(returns)[-1]) {
variance <- (1-theta[1]-theta[2])*theta[3]+theta[1]*returns[i-1]^2+theta[2]*sigma
value <- value + log(dnorm(returns[i], 0, sqrt(variance))) # Was: returns[1]
sigma <- variance
}
if( !is.finite(value) )
value <- -Inf
print(-value)
return(-value)
}
result <- optim(
c(.03, .9, sd(returns), sd(returns[980:1001])),
fn=lklh.garch,
method="Nelder-Mead"
)
result$par
# [1] 0.0494979031 0.9415393270 0.0001211441 0.0345273354

如果您想手动拟合模型(而不是通过 rugarch 等软件包),
您可能会发现以下相关实现说明:
Practical Issues in Univariate GARCH Modelling (Y. Chalabi and D. Würtz, 2008) .

关于r - 在 R 中使用 optim 与 GARCH 收敛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18542737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com