private static void TestAnalyticDerivatives() { const double intercept = 7.0; const double slope = -3.0; var pactual = new double[] { intercept, slope }; var p = new double[] { 5.9, -1.1 }; var x = new double[] { -5, -3, -1, 0, 1, 3, 5 }; var y = new double[x.Length]; for (uint i = 0; i < x.Length; i++) { y[i] = intercept + slope * x[i]; } var mpPar = new mp_par[] { new mp_par(), new mp_par() }; mpPar[0].side = 3; mpPar[1].side = 3; var result = new mp_result(2); var status = MPFit.Solve(ForwardModels.LineFunc, x.Length, 2, p, mpPar, null, new LineFitData(x, y), ref result); Console.Write("*** TestLineFit status = {0}\n", status); PrintResult(p, pactual, result); }
/* Test harness routine, which contains test gaussian-peak data * * Example of fixing two parameter * * Commented example of how to put boundary constraints */ private static int TestGaussFix() { double[] x = { -1.7237128E+00, 1.8712276E+00, -9.6608055E-01, -2.8394297E-01, 1.3416969E+00, 1.3757038E+00, -1.3703436E+00, 4.2581975E-02, -1.4970151E-01, 8.2065094E-01 }; double[] y = { -4.4494256E-02, 8.7324673E-01, 7.4443483E-01, 4.7631559E+00, 1.7187297E-01, 1.1639182E-01, 1.5646480E+00, 5.2322268E+00, 4.2543168E+00, 6.2792623E-01 }; double[] ey = new double[10]; double[] p = { 0.0, 1.0, 0.0, 0.1 }; /* Initial conditions */ double[] pactual = { 0.0, 4.70, 0.0, 0.5 }; /* Actual values used to make data*/ //double[] perror = new double[4]; /* Returned parameter errors */ int i; int status; mp_result result = new mp_result(4); //result.xerror = perror; mp_par[] pars = new mp_par[4]/* Parameter constraints */ { new mp_par { isFixed = 1 }, /* Fix parameters 0 and 2 */ new mp_par(), new mp_par { isFixed = 1 }, new mp_par() }; /* How to put limits on a parameter. In this case, parameter 3 is * limited to be between -0.3 and +0.2. * pars[3].limited[0] = 0; * pars[3].limited[1] = 1; * pars[3].limits[0] = -0.3; * pars[3].limits[1] = +0.2; */ for (i = 0; i < 10; i++) { ey[i] = 0.5; } CustomUserVariable v = new CustomUserVariable { X = x, Y = y, Ey = ey }; /* Call fitting function for 10 data points and 4 parameters (2 * parameters fixed) */ status = MPFit.Solve(ForwardModels.GaussFunc, 10, 4, p, pars, null, v, ref result); Console.Write("*** TestGaussFix status = {0}\n", status); PrintResult(p, pactual, result); return(0); }
private mp_par[] createMPConstraints() { mp_par[] pars = new mp_par[_constraints.Count]; var minValues = CreateVectorFor(x => x.Min); var maxValues = CreateVectorFor(x => x.Max); for (int i = 0; i < _constraints.Count; i++) { pars[i] = mpParFrom(minValues[i], maxValues[i]); } return(pars); }
/* Test harness routine, which contains test quadratic data; * Example of how to fix a parameter */ private static int TestQuadFix() { double[] x = { -1.7237128E+00, 1.8712276E+00, -9.6608055E-01, -2.8394297E-01, 1.3416969E+00, 1.3757038E+00, -1.3703436E+00, 4.2581975E-02, -1.4970151E-01, 8.2065094E-01 }; double[] y = { 2.3095947E+01, 2.6449392E+01, 1.0204468E+01, 5.40507, 1.5787588E+01, 1.6520903E+01, 1.5971818E+01, 4.7668524E+00, 4.9337711E+00, 8.7348375E+00 }; double[] ey = new double[10]; double[] p = { 1.0, 0.0, 1.0 }; /* Initial conditions */ double[] pactual = { 4.7, 0.0, 6.2 }; /* Actual values used to make data */ //double[] perror = new double[3]; /* Returned parameter errors */ int i; int status; mp_result result = new mp_result(3); //result.xerror = perror; mp_par[] pars = new mp_par[3] /* Parameter constraints */ { new mp_par(), new mp_par() { isFixed = 1 }, /* Fix parameter 1 */ new mp_par() }; for (i = 0; i < 10; i++) { ey[i] = 0.2; } CustomUserVariable v = new CustomUserVariable() { X = x, Y = y, Ey = ey }; /* Call fitting function for 10 data points and 3 parameters (1 * parameter fixed) */ status = MPFit.Solve(ForwardModels.QuadFunc, 10, 3, p, pars, null, v, ref result); Console.Write("*** TestQuadFix status = {0}\n", status); PrintResult(p, pactual, result); return(0); }
/* Test harness routine, which contains test gaussian-peak data */ public static double[] TestGaussFit(double[] xinc, double[] yinc, double[] p) { double[] x = xinc; double[] y = yinc; double[] ey = new double[yinc.Length]; //double[] p = { 0.0, 1.0, 1.0, 1.0 }; /* Initial conditions */ double[] pactual = { 0.0, 4.70, 0.0, 0.5 }; /* Actual values used to make data*/ //double[] perror = new double[4]; /* Returned parameter errors */ mp_par[] pars = new mp_par[4] /* Parameter constraints */ { new mp_par(), new mp_par(), new mp_par(), new mp_par() }; int i; int status; mp_result result = new mp_result(4); //result.xerror = perror; /* No constraints */ for (i = 0; i < yinc.Length; i++) { ey[i] = 0.02; } for (i = 0; i < p.Length; i++) { Console.Write("P" + i + " : " + p[i]); } CustomUserVariable v = new CustomUserVariable { X = x, Y = y, Ey = ey }; /* Call fitting function for 10 data points and 4 parameters (no * parameters fixed) */ status = MPFit.Solve(ForwardModels.GaussFunc, yinc.Length, 4, p, pars, null, v, ref result); Console.Write("*** TestGaussFit status = {0}\n", status); PrintResult(p, pactual, result); double[] retval = { p[0], p[1], p[2], p[3], result.xerror[0], result.xerror[1], result.xerror[2], result.xerror[3] }; return(retval); }
public SabrCoeffOptimizer( double maturity, double spotPrice, double atmVol, IList <double> strikes, IList <double> marketVols, bool estimateAlpha, bool useFineTune, double initAlpha = 0.3, double initBeta = 0.5, double initNu = 0.3, double initRho = 0.3) { MaturityInYears = maturity; _spotPrice = spotPrice; AtmVol = atmVol; _strikes = strikes; _marketVols = marketVols; _estimateAlpha = estimateAlpha; _useFineTune = useFineTune; _initBeta = initBeta; if (strikes.Count != marketVols.Count) { throw new PricingLibraryException("strikes and marketVols should have same number of elements!"); } double bestAlpha, bestBeta, bestRho, bestNu; var nDataPoints = _marketVols.Count(); double[] ey = new double[nDataPoints]; for (int i = 0; i < nDataPoints; ++i) { ey[i] = 0.1; // error for data points, we do not need to use this } const int nParams = 3; mp_par[] pars = new mp_par[nParams] // parameter constraints { new mp_par(), // no constraint new mp_par(), new mp_par() }; if (!estimateAlpha) // fix alpha { pars[2] = new mp_par() { isFixed = 1 }; } CustomUserVariable v = new CustomUserVariable() { X = _strikes.ToArray(), Y = _marketVols.ToArray(), Ey = ey }; double[] p = new double[nParams] { initRho, initNu, initAlpha }; mp_result result = new mp_result(nParams); int status = MPFit.Solve(SabrFunc, nDataPoints, nParams, p, pars, null, v, ref result); bestRho = p[0]; bestNu = p[1]; bestAlpha = estimateAlpha ? p[2] : FindAlpha(_initBeta, p[0], p[1], MaturityInYears, _spotPrice, AtmVol); bestBeta = _initBeta; Result = new SabrCoeffOptimizerResult(MaturityInYears, bestAlpha, bestBeta, bestRho, bestNu); }