public ComboHelper(ISectionHelper quadraticHelper, ISectionHelper convMonoHelper, double quadraticity) { quadraticity_ = quadraticity; quadraticHelper_ = quadraticHelper; convMonoHelper_ = convMonoHelper; Utils.QL_REQUIRE(quadraticity <1.0 && quadraticity> 0.0, () => "Quadratic value must lie between 0 and 1"); }
public ComboHelper(ISectionHelper quadraticHelper, ISectionHelper convMonoHelper, double quadraticity) { quadraticity_ = quadraticity; quadraticHelper_ = quadraticHelper; convMonoHelper_ = convMonoHelper; if (!(quadraticity < 1.0 && quadraticity > 0.0)) throw new ApplicationException("Quadratic value must lie between 0 and 1"); }
public ComboHelper(ISectionHelper quadraticHelper, ISectionHelper convMonoHelper, double quadraticity) { quadraticity_ = quadraticity; _quadraticHelper = quadraticHelper; _convMonoHelper = convMonoHelper; }
public ComboHelper(ISectionHelper quadraticHelper, ISectionHelper convMonoHelper, double quadraticity) { quadraticity_ = quadraticity; quadraticHelper_ = quadraticHelper; convMonoHelper_ = convMonoHelper; if (!(quadraticity < 1.0 && quadraticity > 0.0)) { throw new Exception("Quadratic value must lie between 0 and 1"); } }
public override void update() { sectionHelpers_.Clear(); if (size_ == 2) //single period { ISectionHelper singleHelper = new EverywhereConstantHelper(yBegin_[1], 0.0, xBegin_[0]); sectionHelpers_.Add(xBegin_[1], singleHelper); extrapolationHelper_ = singleHelper; return; } List <double> f = new InitializedList <double>(size_); sectionHelpers_ = new Dictionary <double, ISectionHelper>(preSectionHelpers_); int startPoint = sectionHelpers_.Count + 1; //first derive the boundary forwards. for (int i = startPoint; i < size_ - 1; ++i) { double dxPrev = xBegin_[i] - xBegin_[i - 1]; double dx = xBegin_[i + 1] - xBegin_[i]; f[i] = dxPrev / (dx + dxPrev) * yBegin_[i] + dx / (dx + dxPrev) * yBegin_[i + 1]; } if (startPoint > 1) { f[startPoint - 1] = preSectionHelpers_.Last().Value.fNext(); } if (startPoint == 1) { f[0] = 1.5 * yBegin_[1] - 0.5 * f[1]; } f[size_ - 1] = 1.5 * yBegin_[size_ - 1] - 0.5 * f[size_ - 2]; if (forcePositive_) { if (f[0] < 0) { f[0] = 0.0; } if (f[size_ - 1] < 0.0) { f[size_ - 1] = 0.0; } } double primitive = 0.0; for (int i = 0; i < startPoint - 1; ++i) { primitive += yBegin_[i + 1] * (xBegin_[i + 1] - xBegin_[i]); } int endPoint = size_; //constantLastPeriod_ = false; if (constantLastPeriod_) { endPoint = endPoint - 1; } for (int i = startPoint; i < endPoint; ++i) { double gPrev = f[i - 1] - yBegin_[i]; double gNext = f[i] - yBegin_[i]; //first deal with the zero gradient case if (Math.Abs(gPrev) < 1.0E-14 && Math.Abs(gNext) < 1.0E-14) { ISectionHelper singleHelper = new ConstantGradHelper(f[i - 1], primitive, xBegin_[i - 1], xBegin_[i], f[i]); sectionHelpers_.Add(xBegin_[i], singleHelper); } else { double quadraticity = quadraticity_; ISectionHelper quadraticHelper = null; ISectionHelper convMonotoneHelper = null; if (quadraticity_ > 0.0) { if (gPrev >= -2.0 * gNext && gPrev > -0.5 * gNext && forcePositive_) { quadraticHelper = new QuadraticMinHelper(xBegin_[i - 1], xBegin_[i], f[i - 1], f[i], yBegin_[i], primitive); } else { quadraticHelper = new QuadraticHelper(xBegin_[i - 1], xBegin_[i], f[i - 1], f[i], yBegin_[i], primitive); } } if (quadraticity_ < 1.0) { if ((gPrev > 0.0 && -0.5 * gPrev >= gNext && gNext >= -2.0 * gPrev) || (gPrev < 0.0 && -0.5 * gPrev <= gNext && gNext <= -2.0 * gPrev)) { quadraticity = 1.0; if (quadraticity_ == 0) { if (forcePositive_) { quadraticHelper = new QuadraticMinHelper( xBegin_[i - 1], xBegin_[i], f[i - 1], f[i], yBegin_[i], primitive); } else { quadraticHelper = new QuadraticHelper( xBegin_[i - 1], xBegin_[i], f[i - 1], f[i], yBegin_[i], primitive); } } } else if ((gPrev < 0.0 && gNext > -2.0 * gPrev) || (gPrev > 0.0 && gNext < -2.0 * gPrev)) { double eta = (gNext + 2.0 * gPrev) / (gNext - gPrev); double b2 = (1.0 + monotonicity_) / 2.0; if (eta < b2) { convMonotoneHelper = new ConvexMonotone2Helper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], b2, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], b2, primitive); } } } else if ((gPrev > 0.0 && gNext < 0.0 && gNext > -0.5 * gPrev) || (gPrev < 0.0 && gNext > 0.0 && gNext < -0.5 * gPrev)) { double eta = gNext / (gNext - gPrev) * 3.0; double b3 = (1.0 - monotonicity_) / 2.0; if (eta > b3) { convMonotoneHelper = new ConvexMonotone3Helper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], b3, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], b3, primitive); } } } else { double eta = gNext / (gPrev + gNext); double b2 = (1.0 + monotonicity_) / 2.0; double b3 = (1.0 - monotonicity_) / 2.0; if (eta > b2) { eta = b2; } if (eta < b3) { eta = b3; } if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i - 1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } } } if (quadraticity == 1.0) { sectionHelpers_.Add(xBegin_[i], quadraticHelper); } else if (quadraticity == 0.0) { sectionHelpers_.Add(xBegin_[i], convMonotoneHelper); } else { sectionHelpers_.Add(xBegin_[i], new ComboHelper(quadraticHelper, convMonotoneHelper, quadraticity)); } } primitive += yBegin_[i] * (xBegin_[i] - xBegin_[i - 1]); } if (constantLastPeriod_) { sectionHelpers_.Add(xBegin_[size_ - 1], new EverywhereConstantHelper(yBegin_[size_ - 1], primitive, xBegin_[size_ - 2])); extrapolationHelper_ = sectionHelpers_[xBegin_[size_ - 1]]; } else { extrapolationHelper_ = new EverywhereConstantHelper((sectionHelpers_.Last()).Value.value(xBegin_.Last()), primitive, xBegin_.Last()); } }
public ConvexMonoticInterpolator( IEnumerable <Tuple <double, double> > points, double quadraticity = 0.3, double monotonicity = 0.7, bool forcePositive = true, bool constFinalPeriod = false, bool allowExtrapolation = true) { _keyPoints = points.ToArray(); _xArr = _keyPoints.Select(x => x.Item1).ToArray(); _yArr = _keyPoints.Select(x => x.Item2).ToArray(); _quadraticity = quadraticity; _monotonicity = monotonicity; _forcePositive = forcePositive; _constFinalPeriod = constFinalPeriod; _allowExtrapolation = allowExtrapolation; var length = _keyPoints.Length; _helpers = new ISectionHelper[length]; if (length < 2) { throw new PricingLibraryException("Convex monotonic interpolator must have at least 2 points"); } if (_keyPoints.Length == 2) { var helper = new EverywhereConstantHelper(_keyPoints.Last().Item2, 0.0, _keyPoints.First().Item1); _helpers[1] = helper; _extrapolationHelper = helper; } var f = new double[length]; for (var i = 1; i < length - 1; ++i) { var dxPrev = _xArr[i] - _xArr[i - 1]; var dx = _xArr[i + 1] - _xArr[i]; f[i] = dxPrev / (dx + dxPrev) * _yArr[i] + dx / (dx + dxPrev) * _yArr[i + 1]; } f[0] = 1.5 * _yArr[1] - 0.5 * f[1]; f[length - 1] = 1.5 * _yArr.Last() - 0.5 * f[length - 2]; if (_forcePositive) { if (f[0] < 0.0) { f[0] = 0; } if (f[length - 1] < 0.0) { f[length - 1] = 0.0; } } var integral = 0.0; var end = constFinalPeriod ? length - 1 : length; for (var i = 1; i < end; ++i) { var gPrev = f[i - 1] - _yArr[i]; var gNext = f[i] - _yArr[i]; if (gPrev.IsAlmostZero() && gNext.IsAlmostZero()) { _helpers[i] = new ConstantGradHelper(f[i - 1], integral, _xArr[i - 1], _xArr[i], f[i]); } else { quadraticity = _quadraticity; ISectionHelper quadraticHelper = null; ISectionHelper convMonotoneHelper = null; if (_quadraticity > 0.0) { if (gPrev >= -2.0 * gNext && gPrev > -0.5 * gNext && _forcePositive) { quadraticHelper = new QuadraticMinHelper(_xArr[i - 1], _xArr[i], f[i - 1], f[i], _yArr[i], integral); } else { quadraticHelper = new QuadraticHelper(_xArr[i - 1], _xArr[i], f[i - 1], f[i], _yArr[i], integral); } } if (_quadraticity < 1.0) { if ((gPrev > 0.0 && -0.5 * gPrev >= gNext && gNext >= -2.0 * gPrev) || (gPrev < 0.0 && -0.5 * gPrev <= gNext && gNext <= -2.0 * gPrev)) { quadraticity = 1.0; if (_quadraticity.IsAlmostZero()) { quadraticHelper = _forcePositive ? (ISectionHelper) new QuadraticMinHelper(_xArr[i - 1], _xArr[i], f[i - 1], f[i], _yArr[i], integral) : new QuadraticHelper(_xArr[i - 1], _xArr[i], f[i - 1], f[i], _yArr[i], integral); } } else if ((gPrev < 0.0 && gNext > -2.0 * gPrev) || (gPrev > 0.0 && gNext < -2.0 * gPrev)) { var eta = (gNext + 2.0 * gPrev) / (gNext - gPrev); var b2 = (1.0 + _monotonicity) / 2.0; if (eta < b2) { convMonotoneHelper = new ConvexMonotone2Helper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], eta, integral); } else { convMonotoneHelper = _forcePositive ? new ConvexMonotone4MinHelper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], b2, integral) : new ConvexMonotone4Helper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], b2, integral); } } else if ((gPrev > 0.0 && gNext < 0.0 && gNext > -0.5 * gPrev) || (gPrev < 0.0 && gNext > 0.0 && gNext < -0.5 * gPrev)) { var eta = gNext / (gNext - gPrev) * 3.0; var b3 = (1.0 - _monotonicity) / 2.0; if (eta > b3) { convMonotoneHelper = new ConvexMonotone3Helper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], eta, integral); } else { convMonotoneHelper = _forcePositive ? new ConvexMonotone4MinHelper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], b3, integral) : new ConvexMonotone4Helper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], b3, integral); } } else { var eta = gNext / (gPrev + gNext); var b2 = (1.0 + _monotonicity) / 2.0; var b3 = (1.0 - _monotonicity) / 2.0; if (eta > b2) { eta = b2; } if (eta < b3) { eta = b3; } convMonotoneHelper = _forcePositive ? new ConvexMonotone4MinHelper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], eta, integral) : new ConvexMonotone4Helper(_xArr[i - 1], _xArr[i], gPrev, gNext, _yArr[i], eta, integral); } } if (quadraticity.AlmostEqual(1.0)) { _helpers[i] = quadraticHelper; } else if (quadraticity.IsAlmostZero()) { _helpers[i] = convMonotoneHelper; } else { _helpers[i] = new ComboHelper(quadraticHelper, convMonotoneHelper, quadraticity); } } integral += _yArr[i] * (_xArr[i] - _xArr[i - 1]); } if (_constFinalPeriod) { _helpers[length - 1] = new EverywhereConstantHelper(_yArr[length - 1], integral, _xArr[length - 2]); _extrapolationHelper = _helpers[length - 1]; } else { _extrapolationHelper = new EverywhereConstantHelper(_helpers[length - 1].GetValue(_xArr.Last()), integral, _xArr.Last()); } }
public Regression(ISectionHelper sectionHelper, IOLDBConnection oldbConnection) { _sectionHelper = sectionHelper; _oldbConnection = oldbConnection; }
public MergedToEpic(ISectionHelper sectionHelper, IOLDBConnection oldbConnection) { _sectionHelper = sectionHelper; _oldbConnection = oldbConnection; }
public override void update() { sectionHelpers_.Clear(); if (size_ == 2) { //single period ISectionHelper singleHelper = new EverywhereConstantHelper(yBegin_[1], 0.0, xBegin_[0]); sectionHelpers_.Add(xBegin_[1], singleHelper); extrapolationHelper_ = singleHelper; return; } List<double> f = new InitializedList<double>(size_); sectionHelpers_ = new Dictionary<double,ISectionHelper>(preSectionHelpers_); int startPoint = sectionHelpers_.Count+1; //first derive the boundary forwards. for (int i=startPoint; i<size_-1; ++i) { double dxPrev = xBegin_[i] - xBegin_[i-1]; double dx = xBegin_[i+1] - xBegin_[i]; f[i] = dxPrev/(dx+dxPrev) * yBegin_[i] + dx/(dx+dxPrev) * yBegin_[i+1]; } if (startPoint > 1) f[startPoint-1] = preSectionHelpers_.Last().Value.fNext(); if (startPoint == 1) f[0] = 1.5 * yBegin_[1] - 0.5 * f[1]; f[size_-1] = 1.5 * yBegin_[size_-1] - 0.5 * f[size_-2]; if (forcePositive_) { if (f[0] < 0) f[0] = 0.0; if (f[size_-1] < 0.0) f[size_-1] = 0.0; } double primitive = 0.0; for (int i = 0; i < startPoint-1; ++i) primitive += yBegin_[i+1] * (xBegin_[i+1]-xBegin_[i]); int endPoint = size_; //constantLastPeriod_ = false; if (constantLastPeriod_) endPoint = endPoint-1; for (int i=startPoint; i< endPoint; ++i) { double gPrev = f[i-1] - yBegin_[i]; double gNext = f[i] - yBegin_[i]; //first deal with the zero gradient case if ( Math.Abs(gPrev) < 1.0E-14 && Math.Abs(gNext) < 1.0E-14 ) { ISectionHelper singleHelper = new ConstantGradHelper(f[i - 1], primitive, xBegin_[i-1], xBegin_[i], f[i]); sectionHelpers_.Add(xBegin_[i], singleHelper); } else { double quadraticity = quadraticity_; ISectionHelper quadraticHelper = null; ISectionHelper convMonotoneHelper = null; if (quadraticity_ > 0.0) { if (gPrev >= -2.0*gNext && gPrev > -0.5*gNext && forcePositive_) { quadraticHelper = new QuadraticMinHelper(xBegin_[i-1], xBegin_[i], f[i-1], f[i], yBegin_[i], primitive); } else { quadraticHelper = new QuadraticHelper(xBegin_[i-1], xBegin_[i], f[i-1], f[i], yBegin_[i], primitive); } } if (quadraticity_ < 1.0) { if ((gPrev > 0.0 && -0.5*gPrev >= gNext && gNext >= -2.0*gPrev) || (gPrev < 0.0 && -0.5*gPrev <= gNext && gNext <= -2.0*gPrev)) { quadraticity = 1.0; if (quadraticity_ == 0) { if (forcePositive_) { quadraticHelper = new QuadraticMinHelper( xBegin_[i-1], xBegin_[i], f[i-1], f[i], yBegin_[i], primitive); } else { quadraticHelper = new QuadraticHelper( xBegin_[i-1], xBegin_[i], f[i-1], f[i], yBegin_[i], primitive); } } } else if ( (gPrev < 0.0 && gNext > -2.0*gPrev) || (gPrev > 0.0 && gNext < -2.0*gPrev)) { double eta = (gNext + 2.0*gPrev)/(gNext - gPrev); double b2 = (1.0 + monotonicity_)/2.0; if (eta < b2) { convMonotoneHelper = new ConvexMonotone2Helper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], b2, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], b2, primitive); } } } else if ( (gPrev > 0.0 && gNext < 0.0 && gNext > -0.5*gPrev) || (gPrev < 0.0 && gNext > 0.0 && gNext < -0.5*gPrev) ) { double eta = gNext/(gNext-gPrev) * 3.0; double b3 = (1.0 - monotonicity_) / 2.0; if (eta > b3) { convMonotoneHelper = new ConvexMonotone3Helper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], b3, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], b3, primitive); } } } else { double eta = gNext/(gPrev + gNext); double b2 = (1.0 + monotonicity_) / 2.0; double b3 = (1.0 - monotonicity_) / 2.0; if (eta > b2) eta = b2; if (eta < b3) eta = b3; if (forcePositive_) { convMonotoneHelper = new ConvexMonotone4MinHelper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } else { convMonotoneHelper = new ConvexMonotone4Helper( xBegin_[i-1], xBegin_[i], gPrev, gNext, yBegin_[i], eta, primitive); } } } if (quadraticity == 1.0) { sectionHelpers_.Add(xBegin_[i], quadraticHelper); } else if (quadraticity == 0.0) { sectionHelpers_.Add(xBegin_[i], convMonotoneHelper); } else { sectionHelpers_.Add(xBegin_[i], new ComboHelper(quadraticHelper, convMonotoneHelper, quadraticity)); } } primitive += yBegin_[i] * (xBegin_[i]-xBegin_[i-1]); } if (constantLastPeriod_) { sectionHelpers_.Add(xBegin_[size_-1], new EverywhereConstantHelper(yBegin_[size_-1], primitive, xBegin_[size_-2])); extrapolationHelper_ = sectionHelpers_[xBegin_[size_-1]]; } else { extrapolationHelper_ = new EverywhereConstantHelper((sectionHelpers_.Last()).Value.value(xBegin_.Last()), primitive, xBegin_.Last()); } }
public InProgess(ISectionHelper sectionHelper, IOLDBConnection oldbConnection) { _sectionHelper = sectionHelper; _oldbConnection = oldbConnection; }