Home > Net >  Loop over a sequence of values and return the mean
Loop over a sequence of values and return the mean

Time:03-31

I have a data.frame that consists of multiple assets for four different asset classes:

    mData = structure(
  list(
    Id = c(
      "100522",
      "102342",
      "103966",
      "110035",
      "113372",
      "128700",
      "129172",
      "129376",
      "129387",
      "2692",
      "5898",
      "5915",
      "6579",
      "6582",
      "99532",
      "7109",
      "159347",
      "161863",
      "22646",
      "CIBO03M"
    ),
    ISIN = c(
      "DK0002038827=",
      "DK0009514630=",
      "DK0009515876=",
      "DK0009516411=",
      "DK0002041458=",
      "DK0004605839=",
      "DK0009296469=",
      "DK0009521841=",
      "DK0009523540=",
      "DK0002033000=",
      "DK0009292989=",
      "DK0009295149=",
      "DK0009504326=",
      "DK0009504755=",
      "DK0009513582=",
      "DK0009922916=",
      "DK0061156759",
      "DK0061533726",
      "DK0060681468",
      ""
    ),
    MarketValue = structure(
      list(
        EopBasHoldingValueAtMarketPrice = c(
          16669183.7007,
          6278523.94593,
          5915910.4949175,
          11819928.8978516,
          34397488.6380576,
          7650550.3511525,
          6631560,
          8347035.6,
          13704711.6448,
          15232946.38065,
          3151649.4561,
          10325031.2,
          21740572.2606,
          4066411.732155,
          12969412.6293,
          29862889.0168,
          7599626.22,
          5163591.21,
          7159142.25,
          3947.66591068683
        )
      ),
      row.names = c(NA, 20L),
      class = "data.frame"
    ),
    Class = c(
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Realer",
      "Stater",
      "Funds",
      "Funds",
      "Funds",
      "Cash"
    ),
    Share = structure(
      list(
        EopBasHoldingValueAtMarketPrice = c(
          0.0728898309617914,
          0.0274542867440581,
          0.0258686762172713,
          0.0516853515333577,
          0.150410912577133,
          0.0334537870523775,
          0.0289980179049007,
          0.0364993286318217,
          0.0599269966127746,
          0.0666095536933212,
          0.0137813104847062,
          0.045148568301615,
          0.0950656412180039,
          0.0177813184556468,
          0.0567117329316913,
          0.130582335137016,
          0.0332311096028857,
          0.0225789875023626,
          0.0313049923621638,
          0.0000172620751015844
        )
      ),
      class = "data.frame",
      row.names = c(NA, 20L)
    ),
    StandardDev = c(
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0144093997537608,
      0.0108242422424169,
      0.09225012356422,
      0.09225012356422,
      0.09225012356422,
      0.000281246885676718
    ),
    VaR = structure(
      list(
        EopBasHoldingValueAtMarketPrice = c(
          395082.214566048,
          148809.51516899,
          140215.085601845,
          280148.312528366,
          815267.03590552,
          181328.397938014,
          157176.947466096,
          197836.040991687,
          324820.214584912,
          361041.446209531,
          74698.3577005982,
          244717.213824228,
          515280.987292158,
          96379.4617255129,
          307392.632729193,
          531687.659072421,
          1153151.70590702,
          783512.746554265,
          1086313.57114542,
          1.82622956893417
        )
      ),
      class = "data.frame",
      row.names = c(NA,-20L)
    )
  ),
  row.names = c(NA,-20L),
  class = "data.frame"
)

In the simple calculation I define a single number:

alpha.tolerance = 0.05

Which I use in the following simple calculation:

mData$VaR = (abs(mData[3] * qnorm(1-alpha.tolerance,0,1))*mData[6])

But, I also need to make a similar calculation over a sequence of different "alpha.tolerance" values.

alpha.seq = seq(0.001, alpha.tolerance, by=0.0001)

Which has a length of 491.

Hence, the simple calculation needs to be adjusted to mean((abs(mData[3] * qnorm(1-alpha.seq,0,1))*mData[6])) where the alpha.tolerance has been replaced with alpha.seq - then for all the 491 alpha values calculate the VaR for each Id and take the mean of these. So, I sort of need to "loop" over the values and take the mean. Do you folks have any idea how to go around this? I have looked into the sapply function, but I cannot really get it to work.

CodePudding user response:

You can use indeed purrr::map_dbl or sapply to apply a function on a vector of different thresholds:

alpha.seq <- seq(0.001, alpha.tolerance, by=0.0001)
sapply(alpha.seq, function(x) mean(abs(mData[3] * qnorm(1-x,0,1))[,1], na.rm = TRUE))
  • Related