public static void TestNewTailFittingV4() { var dist = new Beta(2, 2); var data = new double[1000]; dist.Samples(data); Sorting.Sort(data); Program.logger.WriteLine("idx,data,ECDF"); for (int i = 0; i < data.Length; i++) { Program.logger.WriteLine($"{i},{data[i]},{(i + 1.0) / data.Length}"); } GPDApproximation.ApproximateExcessDistributionParametersV4(data, out double a, out double c, out double u); }
public static void TestGEVLocation() { //var dist = new Normal(0, 1, Program.rand); var dist = new Exponential(2, Program.rand); double estimateLoc(int n) => Math.Sqrt(2 * Math.Log(n) - Math.Log(Math.Log(n)) - Math.Log(4 * Math.PI)); for (int n = 150; n < 500; n += 10) { double proportion = (n - 0.78) / n; double quant = dist.InverseCumulativeDistribution(proportion); double estimate = estimateLoc(n); //Console.WriteLine($"Quantile: {quant} Est: {estimate} Error: {Math.Abs(estimate - quant)}"); var props = Interpolation.Linspace(0.000000001, 0.999999999, 10000); var quants = new double[10000]; for (int i = 0; i < quants.Length; i++) { quants[i] = dist.InverseCumulativeDistribution(props[i]); } GPDApproximation.ApproximateExcessDistributionParametersV4(quants, out double a, out double c, out double u); var tailApprox = new GPDApproximation(quants, GPDApproximation.FittingMethod.V4); var sample = new double[10000]; for (int i = 0; i < 10000; i++) { double max = 0; for (int j = 0; j < n; j++) { max = Math.Max(max, tailApprox.Sample()); } sample[i] = max; } double shapeGuess = tailApprox.c; double g1 = SpecialFunctions.Gamma(1 - shapeGuess); double g2 = SpecialFunctions.Gamma(1 - 2 * shapeGuess); double scaleGuess = Math.Sqrt(Statistics.Variance(sample) * shapeGuess * shapeGuess / (g2 - g1 * g1)); double locationGuess = Statistics.Mean(sample) - scaleGuess * (g1 - 1) / shapeGuess; Console.WriteLine($"Quantile: {quant} IntroEst: {estimate} Error: {Math.Abs(estimate - quant)} Bootstrap:{locationGuess} Shape {shapeGuess}"); } }