当前位置: 动力学知识库 > 问答 > 编程问答 >

scope - 0 vector result in R after running function

问题描述:

After I finished running my function, I kept on getting 0s as my answer:

 niv_density <- function(returns, mu, delta, alpha, beta, t)

{

t <- 1/t

gamma <- sqrt(alpha^2 - beta^2)

result <- rep(0, (1/t))

for(i in 1:(1/t))

{

term3 <- exp(delta*gamma*t + beta*(returns[i] - mu*t))

term1 <- alpha*delta*t/pi

term2_1 <- besselY(alpha*sqrt(delta^2*t^2 + (returns[i] - mu*t)^2), 1)

term2_2 <- sqrt(delta^2*t^2 + (returns[i] - mu*t)^2)

term2 <- term2_1/term2_2

result[i] <- (term1*term2*term3)

}

}

niv_density(returns, 0, 2, 50, 0, 10)

result

After executing the last part, I get a vector of 0s. I think I'm having a problem with global vs. local scopes, but I'm not sure how to fix it.

dput(returns)

structure(c(-0.003859212, 0.011873365, -0.004826217, -0.004006846,

-0.004527209, -0.005597606, -0.001446292, 0.004890173, 0.001260653,

-0.005469839, 0.001715495, 0.00776223, -6.79514e-05, -0.002405413,

-0.00344927, 0.013203733, 0.009007395, -0.002918161, -0.000682757,

0.003600917, -0.001584568, 0.001778635, 0.003881849, -0.003228443,

0.00809855, -0.003407655, 0.006570117, -0.001629285, -0.001479157,

-0.000683758, 0.007489741, 0.007807378, 0.001399056, -0.000578823,

-0.002437511, -0.000593349, -0.004020762, 0.004744014, -0.001815119,

0.007757796, -0.002401808, -0.00225831, -0.005162853, -0.002256747,

0.032891919, 0.005882631, -0.011822966, -0.005744899, -0.004359233,

0.00405189, 0.017035644, 0.001079738, 0.001845759, -0.004758891,

0.006067706, -0.006027932, -0.00224155, -0.010844493, 0, -0.003861616,

-0.004698823, 0.000397524, 0.001840917, 0.013599978, -0.008376557,

1.92494e-05, 0.010797502, -0.004105023, 0.003119424, -0.004797368,

-0.001962367, 0.002663974, 0.008489008, 0.007827146, -0.000566674,

-0.003404669, -0.000160508, -0.003953786, -0.000635631, 0.0023086,

0.008931147, -0.002761431, 0.013046559, -0.009673618, 0.007572105,

-0.011309217, 0.003777911, -0.004767721, -0.004096769, 0.003915212,

-0.005571037, 0.008566323, -0.009063831, -0.011191246, -0.000639167,

0.002834983, -0.009156367, 0.00189252, 0.007166451, -0.001788182,

-0.002437146, 0.00226261, -0.010459432, -0.001511577, 0.00039628,

-0.00349739, 0.009561965, 0.063504871, 0.003492974, 0.009233691,

0.004795333, -0.003995969, -0.002552804, 6.81834e-05, 0.006134657,

0.006713932, -0.006875273, -0.005108732, 0.006239377, 0.002293386,

-0.01121192, -0.005666844, 0.000894577, -0.012511724, 0.00351622,

-0.009671627, -0.004480382, 0.007385228, -0.009143379, 0.005467177,

0.017094141, 0.005918621, 0.001514995, -0.001356959, 0.015656296,

0.001101646, 0.001457523, 0.0051402, -0.005516804, 0.002832519,

-0.002196811, -0.007752963, 0.009050809, 0.006380147, 0.001995102,

0.002319077, -0.001788715, 0.000845096, -0.009821598, 0.012634302,

-0.001457121, 0.000582262, -0.004083585, -0.004021717, -0.000571503,

0.006159289, -0.010822168, -0.015789222, -0.000657867, 0.013935285,

0.001312777, -0.001172312, 0.003031039, 0.002482838, -0.010634785,

0.014015267, 0.005435065, -0.034817949, 0.005145224, -0.007217488,

0.00458109, 0.012581199, 0.001853981, 0.002118571, -0.011151137,

-0.007933775, 0.011336262, 0.018212375, 0.007815775, 0.006103632,

-0.007270438, -0.001066825, 0.001892988, -0.009740379, 0.012057142,

0.00024459, -0.003702988, 0.014628744, -0.001902607, -7.49322e-05,

-0.005903797, -0.002481339, -0.004266069, 0.01150386, -0.019888508,

0.007657512, -0.004649027, 0, 0.002523089, -0.00072238, -0.021153782,

-0.007969763, 0.005775428, -0.010897333, 0.007468107, -0.009508927,

0.000464995, -0.002430182, 0.010796022, 0.008898853, -0.013079549,

0.027112561, -0.015413991, -0.007630787, 0.007033724, -0.017738864,

-0.015961032, -0.015579591, -0.011802317, -0.002187586, 0.003065715,

0.013389559, -0.000885034, -0.013701533, 0.001976838, 0.001041955,

-0.003616062, 0.005344799, 0.007148373, -0.002877552, -0.007681476,

0.021591165, 0.017966863, -0.058771073, -0.019551973, 0.005203616,

0.002169669, 0.003884158, -0.022568915, 0.002769004, -0.007779571,

0.018998803, -0.001212088, 0.002446011, 0.007740844, 0.012532807,

0.006287039, 0.003958813, 0.01407559, 0.001064047, -0.00862106,

-0.012296938, -0.013967015, 0.010524923, -0.010789529, 0.011953286,

0.000738662, -0.016492003, -0.00257709, -0.015437029, 0.004315983,

0.023337948, 0.008138125, 0.005972748, 0.005915635, 0.010493804,

-0.011895336, -0.005245454, 0.007409717, 0.012596218, -0.005221382,

-0.005462129, 0.008785043, 0.009134618, 0.015541224, 0.016072839,

-0.003827797, 0.000403703, 0.03749696, -0.003386946, -0.008627298,

-0.030790478, -0.003861794, -0.011426323, 0.001393173, 0.008541783,

0.009361445, -0.023851831, 0.024814864, -0.019724128, 0.002621807,

-0.017904622, -0.003584294, -0.019299804, -0.00234839, -0.002685042,

0.002685042, 0.016590137, 0.001401377, -0.006120481, 0.006690448,

-0.004740457, -0.005027981, 0.013204038, -0.002742491, 0.005110009,

-0.006393429, 0.00464228, -0.00270551, -0.011552836, 0.003074876,

0.005139878, 0.002032361, 0.007603533, 0.010491222, 0.000658875,

0.003909991, 0.00236732, 0.019192366, -0.00361624, 0.005696264,

-0.005852811, 0.014805765, 0.00313454, 0.006385073, -0.005475311,

-0.009195918, 0.008472618, -0.000559148, -0.007272851, 0.003748203,

0.001156269, 0.004328552, -0.006107929, -0.012121056, 0.002812434,

-0.009577213, 0.005689626, -0.001941957, 0.006145673, -0.002275509,

-0.006578825, -0.005345298, -0.000327811, 0.003751791, 0.005053343,

0.005157952, -0.022100394, -0.007461083, 0.003576376, 0.00093598,

0.006738706, 0.006976768, 0.001078282, -0.006256189, 0.003313743,

-0.005955287, 0.011771523, 0.001644383, -0.003459295, 0.032863111,

-0.007369908, -0.001099451, 0.004745151, 0.012094786, 0.001167328,

-0.00404787, -0.004345022, -0.001121192, 0.004333763, -0.008483142,

-0.001578184, -0.00046999, 0.005079249, -0.005970832, 0.005543307,

0.006722626, 0, 0.001715197, 0.011776868, 0.013308783, -0.004160112,

-0.000304697, 0.014924613, 0.007204855, -0.00509816, 0.007186504,

0.002287253, -0.009948655, -0.001000861, -0.00431929, -0.00347645,

0.005015994, -0.007540969, 0.00558486, -0.005661924, -0.006602168,

-0.002824197, 0.001939661, 0.006563001, -0.009757559, -0.00978824,

-0.001247868, 0.002622219, -0.009097288, -0.014394158, -0.00292424,

0.002644891, -0.005572549, -0.003181826, 0.002676673, 0.007032888,

0.002127581, 0.005281961, 0.016021024, 0.001232531, 0.005515082,

0.000450254, 0.003568462, 0.006277841, -0.003823264, -0.032527132,

0.021873831, -0.003231721, -0.000368515, -0.001397511, -0.010973353,

-0.011563657, -0.010061858, 0.005714484, 0.007472816, 0.003407539,

-0.000612977, -0.000800283, -0.001900635, -0.000865432, -0.003630001,

0.00562073, 0.001858425, 0.010064273, -0.006584881, -0.001470899,

0.005433816, -0.002510864, -0.001071656, -0.005130965, 2.35065e-05,

0.003445676, 0.01374472, -0.001123534, 0.006067276, 0.004050843,

-0.000773321, -0.003401186, 0.001908336, -0.003562041, -0.001180884,

-0.003133416, 0.005819655, -0.002096198, -4.92007e-05, 0.002838133,

-0.010010669, 0.00557654, -0.000122526, 0.022760252, -0.005618111,

0.014434193, 0.001716112, 0.01567573, 0.001566116, -0.003071945,

-0.018146189, -0.012123038, -0.007480614, 0.007735601, -0.00436506,

0.003091618, 0.004704796, 0.001184206, 0.010066361, 0.005389096,

-0.007021784, -0.004211278, -0.001740557, -0.00628043, 0.002434464,

-0.000333944, 0.010815674, 0.016910153, 0, -0.01318228, -0.002858256,

0.024721185, 0.001006412, -0.003651077, 0.009682259, -0.007093437,

-0.002005597, 0.002424598, -0.015024047, 0.015051995, 0.004720944

), na.action = structure(504L, class = "omit"))

网友答案:

Your version is not working because result is local to the function's body and it is lost as you exit the function. I suspect you have another result object in your global environment (a vector of zeroes) and that's what you always get when you try to check your result.

Instead, make your function explicitly return result by adding a return statement at the end:

niv_density <- function(returns, mu, delta, alpha, beta, n) {
    t <- 1/n
    gamma <- sqrt(alpha^2 - beta^2)
    result <- rep(0, n)
    for(i in seq_len(n)) {
        term3   <- exp(delta*gamma*t + beta*(returns[i] - mu*t))
        term1   <- alpha*delta*t/pi
        term2_1 <- besselY(alpha*sqrt(delta^2*t^2 + (returns[i] - mu*t)^2), 1)
        term2_2 <- sqrt(delta^2*t^2 + (returns[i] - mu*t)^2)
        term2   <- term2_1/term2_2
        result[i] <- (term1*term2*term3)
    }
    return(result)
}

And when calling the function, assign the result as follows:

result <- niv_density(returns, 0, 2, 50, 0, 10)

(and maybe you should avoid calling a variable result, I'm sure you can find a more descriptive name from the context.)

分享给朋友:
您可能感兴趣的文章:
随机阅读: