0

我想知道是否有人可以在我的 for 循环中指出我的错误。每次我观察到符号变化时,我都会尝试更改“变量”的值(如 return[i] 和 return[i + 1] 所示)。

wealth = 1000
count = 0
length_return = length(returns)
for(i in 1:(length_return - 1))
{
    if(returns[i] > 0)
    {
        wealth = wealth*(price[i]/price[i - 1])
    }
    if((returns[i] > 0) & (returns[i + 1] < 0))
    {
        count = count + 1
        wealth = wealth*(1 - .001)
    }
    else if((returns[i] < 0) & (returns[i + 1] > 0))
    {
        count = count + 1
        wealth = wealth*(1 - .001)
    }
}

程序应该将财富值调整为 (wealth*.999),但“财富”的值是“数字 (0)”

这是 dput 的结果:

> wealth
numeric(0)
> dput(wealth)
numeric(0)

向量返回的结果[]

> dput(returns)
structure(c(0.04649726, 0.051260078, 0.064587685, 0.081688958, 
0.089178327, 0.088220746, 0.076685447, 0.08073864, 0.107918192, 
0.110432178, 0.119241244, 0.11470364, 0.107377131, 0.092958042, 
0.086196845, 0.082730935, 0.073386354, 0.082530002, 0.085342293, 
0.068458328, 0.076918215, 0.06372015, 0.073365275, 0.059020339, 
0.053460179, 0.053393368, 0.071874253, 0.090166017, 0.104658779, 

-0.050155439, -0.037186422, -0.050748794, -0.049519578, -0.050189338, 
-0.014833199, -0.002455173, -0.005934613, 0.007156353, 0.026845375, 
0.019969084, 0.017407996, 0.007269087, 0.003774705, 0.007976551, 
0.015977842, 0.007582187, 0.022870584, 0.012927018, 0.013875901, 
0.024793852, 0.021017486, 0.015188849, 0.009545373, 0.005816416, 
-0.000224326, -0.008068232, 0.001404623, 0.011375253, 0.021525748, 
0.034434378, 0.031539849, 0.030957107, 0.045225295, 0.043167887, 
0.042020082, 0.047133966, 0.045394564, 0.041455272, 0.043179827, 9945572, -0.041879781,     -0.041896857, -0.040369338, -0.037020153, 
0.040798471, 0.026819125, 0.030540597, 0.031310001, 0.021894158, 
0.017817084, 0.022059595, 0.021358056, 0.026762391, 0.022619225, 
0.02209257, 0.018882881, 0.019061966, 0.016261758, -0.004505179, 
-0.001457767, 0.006123557, 0.023709635, 0.035736269, 0.039109498, 
0.032387708, 0.033475559, 0.03742047, 0.036994833, 0.039972321, 
0.039112388, 0.025337715, 0.029414862, 0.026180163, 0.029382261, 
0.021469624, 0.015715534, 0.019535668, 0.017153586, 0.014145351, 
0.023289324, 0.012607206, 0.010220737, 0.01045078, 0.013243276, 
0.027074632, 0.023394827, 0.020926566, 0.020438976, 0.021330029, 
0.008604304, 0.014850638, 0.014686919, 0.013096954, 0.009374837, 
0.01443215, 0.01661252, 0.01536676, 0.016158815, 0.021767492, 
0.030394682, 0.030475736, 0.029487158, 0.028347715, 0.04105261, 
0.044154818, 0.056462216, 0.055224199, 0.056843288, 0.055654649, 
0.072682111, 0.069483533, 0.066373033, 0.063211431, 0.051366019, 
0.039760278, 0.047199156, 0.042253481, 0.030054723, 0.04184188, 
0.057175652, 0.052457792, 0.051794408, 0.037161545, 0.030259341, 
0.03059966, 0.029873752, 0.007304931, 0.004460967, -0.0015488, 
0.003125818, -0.001533689, -0.016161098, -0.017086177, -0.032358642, 
-0.030717303, -0.02689349, -0.024375075, -0.027481957, -0.034408133, 
-0.03258321, -0.026264889, -0.034690815, -0.034125666, -0.029183139, 
-0.019846483, -0.013114683, -0.01026677, -0.011285312, -0.018233584, 
-0.021051026, -0.027712669, -0.033608687, -0.02612171, -0.024127724, 
-0.030445729, -0.028967942, -0.021405891, -0.024171724, -0.021434177, 
-0.013715497, -0.002838327, -0.004074417, -0.000505583, 0.003139364, 
0.012513817, 0.012566525, 0.004373204, 0.004273826, -0.004080628, 
-0.004343782, 1.55099e-05, 0.014110229, 0.015430673, 0.0171028, 
0.030533699, 0.041469979, 0.036545248, 0.036434168, 0.045897523, 
0.047582759, 0.033954326, 0.038055636, 0.038070104, 0.026744856, 
0.027417561, 0.023161529, 0.028813037, 0.033653558, 0.03892672, 
0.04248052, 0.030778164, 0.027244448, 0.032521666, 0.034439243, 
0.03652718, 0.040391984, 0.026665423, 0.026127712, 0.040948888, 
0.049199194, 0.056136519, 0.059816934, 0.063331553, 0.062560205, 
0.05573074, 0.057457918, 0.053210566, 0.061868296, 0.060517643, 
0.065842535, 0.056788492, 0.020374537, 0.029021998, 0.019599729, 
0.017109049, 0.0142233, 0.01385032, 0.013664016, 0.018648185, 
0.021368724, 0.023747624, 0.021966254, 0.021445966, 0.021101993, 
0.013155832, 0.025693416, 0.028289433, 0.04028089, 0.037326679, 
0.038879551, 0.047571875, 0.048461808, 0.04884816, 0.045672085, 
0.05457715, 0.052152652, 0.032950707, 0.028645946, 0.024074085, 
0.010971707, 0.007218255, 0.002189754, 0.004059126, 0.003782036, 
0.008144712, 0.004256947, -0.001760174, 0.001352159, 0.002846419, 
0.00062595, 0.00617678, 0.007722022, 0.011879437, 0.015524757, 
0.017971861, 0.018865608, 0.021164099, 0.009689763, 0.002417621, 
-0.003509852, -0.015181537, -0.017544458, -0.035842462, -0.030717803, 
-0.030176812, -0.035764545, -0.033777136, -0.022469742, -0.021718647, 
-0.019902874, -0.032961927, -0.043416544, -0.040985411, -0.035366011, 
-0.051626959, -0.058473353, -0.067016303, -0.07114409, -0.071232613, 
-0.064722123, -0.076188295), na.action = structure(1:50, class = "omit"))
4

1 回答 1

1

看看这一行:
wealth = wealth*(price[i]/price[i - 1])

您的 for 循环从 1 开始,因此price[i-1]没有值。你的意思是price[i+1]


此外,如果您只是想检查符号更改,您可能可以使用更少的 if 语句:

if(returns[i] > 0)  {
    wealth = wealth*(price[i]/price[i + 1])
}
if(returns[i+1] != 0 && (returns[i] / returns[i+1] < 0))  {
    count = count + 1
    wealth = wealth*(1 - .001)
}
于 2012-11-11T22:21:43.480 回答