// Method with alt form using gauss hermite public static double[] ComplementsHermite(Normal[] distributions, int order) { double[] complementProbs = new double[distributions.Length]; distributions = NegateDistributions(distributions); // This change is local to this method for (int i = 0; i < distributions.Length; i++) { Normal distribution_i = distributions[i]; Func <double, double> xOfZ = (double z) => distribution_i.Mean + z * Math.Sqrt(2) * distribution_i.StdDev; // Converts Z to X Func <double, double> integrand = z => { double product = 1; // The density is already factored into the method for (int j = 0; j < distributions.Length; j++) { if (j != i) { product *= distributions[j].CumulativeDistribution(xOfZ(z)); } } return(product); }; //complementProbs[i] = (1 / Math.Sqrt(Math.PI)) * GaussHermite.Integrate(integrand, order); complementProbs[i] = GaussHermite.Integrate(integrand, order); //Console.WriteLine($"GH[{i}]: {complementProbs[i]}"); } return(complementProbs); }
public void ProbaMeasure(double lambda, RrFunction sigma, Duration probaMaturity, Duration simulationMaturity, int quadratureNbPoints, double precision) { var refDate = DateTime.Parse("07/06/2009"); var hw1 = new Hw1Model(TimeMeasure.Act365(refDate), Currency.Eur, lambda, sigma); var probaMeasure = new PaymentInfo(hw1.Currency, refDate + probaMaturity); var t = refDate + simulationMaturity; var hw1PathGen = new Hw1ModelPathGeneratorFactory().Build(hw1, null, probaMeasure, new[] { t }); var brownianBridge = BrownianBridge.Create(hw1PathGen.AllSimulatedDates); var hw1Zc = new Hw1ModelZcRepresentation(hw1); var numeraireZc = hw1Zc.Zc(t, probaMeasure.Date, 1.0); Assert.AreEqual(hw1PathGen.AllSimulatedDates.Length, 1); double[] x, w; GaussHermite.GetQuadrature(quadratureNbPoints, out x, out w); double flow = 0.0; for (int i = 0; i < x.Length; i++) { var dw = brownianBridge.PathIncrements(new[] { x[i] }, 1); var ornstein = hw1PathGen.Path(dw).GetProcessValue(0); flow += w[i] * 1.0 / numeraireZc.Eval(ornstein); } var error = Math.Abs(flow - 1.0); Assert.LessOrEqual(error, precision); }
public static double p00_gauss_hermite(ref p00Data data, int problem, int order) //****************************************************************************80 // // Purpose: // // P00_GAUSS_HERMITE applies a Gauss-Hermite quadrature rule. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 26 May 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int PROBLEM, the index of the problem. // // Input, int ORDER, the order of the rule to apply. // // Output, double P00_GAUSS_HERMITE, the approximate integral. // { double[] f_vec = new double[order]; double[] weight = new double[order]; double[] xtab = new double[order]; GaussHermite.hermite_compute(order, ref xtab, ref weight); const int option = 1; p00_fun(ref data, problem, option, order, xtab, ref f_vec); double result = typeMethods.r8vec_dot_product(order, weight, f_vec); return(result); }
public void Zc(double lambda, Duration zcStart, Duration zcDuration, int quadratureNbPoints, double precision) { var sigma = new StepFunction(new[] { 0.0, 1.0, 2.0 }, new[] { 0.007, 0.004, 0.0065 }, 0.0); var refDate = DateTime.Parse("07/06/2009"); var hw1 = new Hw1Model(TimeMeasure.Act365(refDate), Currency.Eur, lambda, sigma); var hw1Zc = new Hw1ModelZcRepresentation(hw1); var zcDate = refDate + zcStart; var zc = hw1Zc.Zc(zcDate, zcDate + zcDuration, 1.0); var drift = hw1.DriftTerm(); var stdDev = Math.Sqrt(drift.Eval(hw1.Time[zcDate])); double[] x, w; GaussHermite.GetQuadrature(quadratureNbPoints, out x, out w); double zcQuad = x.Select((t, i) => w[i] * zc.Eval(new[] { stdDev *t })).Sum(); var error = Math.Abs(zcQuad - 1.0); Assert.LessOrEqual(error, precision); }
void App_Startup(object sender, StartupEventArgs e) { Func <double, double> function = null; int funSelection = 0; bool isParsable = false; bool isBetween = false; do { Console.WriteLine("Wybierz wariant funkcji do całkowania:"); Console.WriteLine("(1). f(x) = 4x^4 -2x^3 + 4x^2 -4x +1 "); Console.WriteLine("(2). f(x) = 4sin(2x) "); Console.WriteLine("(3). f(x) = 2|x^3|"); isParsable = Int32.TryParse(Console.ReadLine(), out funSelection); isBetween = funSelection.IsBetween(1, 3); } while(!(isParsable && isBetween)); switch (funSelection) { case 1: function = Example.Polynomial; break; case 2: function = Example.Sinus; break; case 3: function = Example.Absolute; break; } Console.Write("Nieskończone przedziały? (t/*)"); bool isInifniteRange = Console.ReadKey().IsKey('t'); Console.WriteLine(); double a, b; if (isInifniteRange) { a = double.NegativeInfinity; b = double.PositiveInfinity; } else { bool isNumber = false; bool isCorrect = false; do { Console.WriteLine("Określ przedziały całkowania:"); Console.Write("a:"); isNumber = double.TryParse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture, out a); Console.Write("b:"); isNumber = double.TryParse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture, out b); isCorrect = a < b; } while (!isNumber || !isCorrect); } double epsilon = 0; bool isEpsilonCorrect = false; do { Console.Write("Określ dokładność obliczeń: "); isEpsilonCorrect = double.TryParse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture, out epsilon); } while (!isEpsilonCorrect); if (double.IsInfinity(a) || double.IsInfinity(b)) { a = Utilis.CalculateLimit(0, -2, Example.Exponent(function)); b = Utilis.CalculateLimit(0, 2, Example.Exponent(function)); } Newton_Cotes NewtonCotes; Gauss_Hermite GaussHermite; if (isInifniteRange) { NewtonCotes = new Newton_Cotes(Example.Exponent(function), a, b, epsilon); GaussHermite = new Gauss_Hermite(function, epsilon); } else { NewtonCotes = new Newton_Cotes(Example.Exponent(function), a, b, epsilon); GaussHermite = null; } Console.WriteLine("Całka obliczona przy użyciu metody Newtona-Cotesa wynosi: " + NewtonCotes.GetResult()); Console.WriteLine("Liczba węzłów: " + NewtonCotes.GetIterationsCount()); if (!(GaussHermite is null)) { Console.WriteLine("Całka obliczona przy użyciu kwadratury Gaussa wynosi: " + GaussHermite.GetResult()); Console.WriteLine("Liczba węzłów: " + GaussHermite.GetIterationsCount()); } MainWindow mainWindow = new MainWindow(); mainWindow.Plot.NewtonCotesValues = NewtonCotes.GetNodes(); mainWindow.Plot.Values = NewtonCotes.GetValues(); mainWindow.Show(); }
public static double[] product_weights(int dim_num, int[] order_1d, int order_nd, int rule) //****************************************************************************80 // // Purpose: // // PRODUCT_WEIGHTS computes the weights of a product rule. // // Discussion: // // This routine computes the weights for a quadrature rule which is // a product of closed rules of varying order. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 November 2007 // // Author: // // John Burkardt // // Parameters: // // Input, int DIM_NUM, the spatial dimension. // // Input, int ORDER_1D[DIM_NUM], the order of the 1D rules. // // Input, int ORDER_ND, the order of the product rule. // // Input, int RULE, the index of the rule. // 1, "CC", Clenshaw Curtis Closed Fully Nested rule. // 2, "F1", Fejer 1 Open Fully Nested rule. // 3, "F2", Fejer 2 Open Fully Nested rule. // 4, "GP", Gauss Patterson Open Fully Nested rule. // 5, "GL", Gauss Legendre Open Weakly Nested rule. // 6, "GH", Gauss Hermite Open Weakly Nested rule. // 7, "LG", Gauss Laguerre Open Non Nested rule. // // Output, double PRODUCT_WEIGHTS_CC[DIM_NUM*ORDER_ND], // the product rule weights. // { int dim; int order; typeMethods.r8vecDPData data = new(); double[] w_nd = new double[order_nd]; for (order = 0; order < order_nd; order++) { w_nd[order] = 1.0; } for (dim = 0; dim < dim_num; dim++) { double[] w_1d; switch (rule) { case 1: w_1d = ClenshawCurtis.cc_weights(order_1d[dim]); break; case 2: w_1d = Fejer1.f1_weights(order_1d[dim]); break; case 3: w_1d = Fejer2.f2_weights(order_1d[dim]); break; case 4: w_1d = PattersonQuadrature.gp_weights(order_1d[dim]); break; case 5: w_1d = GaussQuadrature.gl_weights(order_1d[dim]); break; case 6: w_1d = GaussHermite.gh_weights(order_1d[dim]); break; case 7: w_1d = Legendre.QuadratureRule.lg_weights(order_1d[dim]); break; default: Console.WriteLine(""); Console.WriteLine("PRODUCT_WEIGHTS - Fatal error!"); Console.WriteLine(" Unrecognized rule number = " + rule + ""); return(null); } typeMethods.r8vec_direct_product2(ref data, dim, order_1d[dim], w_1d, dim_num, order_nd, ref w_nd); } return(w_nd); }
public BlackScholesWithDividendOption(double spot, AffineDivCurveUtils affineDivUtils, int quadratureNbPoints = 10) { this.affineDivUtils = affineDivUtils; this.spot = spot; GaussHermite.GetQuadrature(quadratureNbPoints, out quadPoints, out quadWeights); }