遍历一系列值和 return 平均值

Loop over a sequence of values and return the mean

我有一个 data.frame,它由四种不同资产的多种资产组成 类:

    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"
)

在简单计算中我定义了一个数字:

alpha.tolerance = 0.05

我在下面的简单计算中使用了它:

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

但是,我还需要对一系列不同的“alpha.tolerance”值进行类似的计算。

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

长度为 491。

因此,需要将简单计算调整为 mean((abs(mData[3] * qnorm(1-alpha.seq,0,1))*mData[6])),其中 alpha.tolerance 已替换为 alpha.seq - 然后针对所有 491 个 alpha 值计算每个 ID 的 VaR并取这些的平均值。所以,我有点需要“循环”这些值并取平均值。你们知道如何解决这个问题吗?我研究了 sapply 函数,但我无法真正让它工作。

您确实可以使用 purrr::map_dblsapply 将函数应用于不同阈值的向量:

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))