Mathematica Asked on August 15, 2021
Say I generated n random variates from the standard cauchy distribution. I want to construct the empirical mean excess plot of (μ, e(μ)) , where
e(μ)={Σ(xi-μ) *I {xi>=μ}}/Nμ . Nμ is the number of values xi that exceed the μ and the summation part is all xi that exceed the μ(the threshold value), i=1,…,n. Could someone know how to plot this? Thanks!
Spit-balling a little here, based on my best understanding of your definition:
n = 500;
variates = RandomVariate[CauchyDistribution[], n];
ClearAll[meanExcess]
meanExcess[data_, mu_] :=
Total[#]/Length[#]& @ Cases[data, x_?(# > mu &) :> x - mu]
Plot[meanExcess[variates, mu], {mu, 0, 10}]
Correct answer by MarcoB on August 15, 2021
Three additional ways to implement empirical mean excess using
ClearAll[eme1, eme2, eme3]
eme1[data_, μ_] := Mean @ TruncatedDistribution[{μ, ∞}, EmpiricalDistribution @ data]
eme2[data_, μ_] := Mean @ Pick[data, UnitStep[data - μ], 1]
eme3[data_, μ_] := Mean @ DeleteCases[Null] @ Clip[data, {μ, ∞}, {Null, ∞}]
Examples:
SeedRandom[1]
rv = RandomVariate[CauchyDistribution[], 100];
Verify that all three methods give the same result when the threshold parameter is within the sample range:
Max @ Chop[{Norm[eme1[rv, #] - eme2[rv, #]],
Norm[eme1[rv, #] - eme3[rv, #]],
Norm[eme2[rv, #] - eme3[rv, #]]} & /@ RandomReal[MinMax[rv], 500]]
0
Plot[{eme1[rv, x], eme2[rv, x], eme3[rv, x]}, {x, 0, 10},
PlotStyle -> {Directive[AbsoluteThickness[7], Red],
Directive[AbsoluteThickness[3], Blue], Directive[Thin, Green]},
ImageSize -> Large, PlotLegends -> {"eme1", "eme2", "eme3"}]
Answered by kglr on August 15, 2021
Following an example, I tried to make it this way:
n = 100000;
SeedRandom[123456];
x = RandomVariate[CauchyDistribution[0, 1], n];
x = Sort[x, Greater];
meanExcess =Table[{x[[k + 1]], Mean[x[[1 ;; k]] - x[[k + 1]]]}, {k, 1, 99999}];
ListPlot[meanExcess, AxesLabel -> (Style[#, 18, Italic, Bold] &) /@ {"x[[k+1]]",
"meanExcess"}, Joined -> True, PlotRange -> All]
Answered by Leslie Chiu on August 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP