public void LinearInterpolation_NoCalibration() { LinearInterpolation target = new LinearInterpolation(); Assert.AreEqual(0, target.GetAdjustedValue(0)); Assert.AreEqual(1, target.GetAdjustedValue(1)); }
/// <summary> /// A percentile is the value of a variable below which a certain /// percentage of observations reside. /// If percentile*(data size - 1) is not an integer, then linear /// interpolation around the relevant index is applied to compute /// the percentile. /// </summary> /// <param name="data">Data from which to compute the /// percentile. There is no requirement for the array to be sorted /// into ascending/descending order.</param> /// <param name="percentile">The requested percentile as an integer /// in the range [0,100]. /// Example: To request the 99'th percentile use the value 99.</param> /// <returns></returns> public double Percentile(ref double[] data, int percentile) { // Check for valid inputs. if (data.Length < 1) { const string errorMessage = "Data array used to calculate a percentile cannot be empty."; throw new ArgumentException(errorMessage); } if (percentile < 0 || percentile > 100) { const string errorMessage = "Percentile is restricted to the range [0,100]."; throw new ArgumentException(errorMessage); } // Copy the array into a temporary array and then sort into // ascending order. long numElements = data.Length; var yArray = new double[numElements]; Array.Copy(data, yArray, numElements); Array.Sort(yArray); // Compute the index at which the percentile is to be located. var percentileIndex = (numElements - 1) * percentile / 100.0; // Compute the percentile by linear interpolation at the // target percentile index. var xArray = new double[numElements]; // array index for (long i = 0; i < numElements; ++i) { xArray[i] = i; } var interpObj = LinearInterpolation.Interpolate(xArray, yArray); var percentileValue = interpObj.ValueAt(percentileIndex, true); return(percentileValue); }
/// <summary> /// Sets the interpolated curve. /// </summary> public void SetInterpolatedCurve() { int n1 = NodalExpiries.Length; if (n1 == 0) { return; } int n2 = NodalExpiries[0].Strikes.Length; if (n2 <= 1) { return; } var rows = new double[n1]; var cols = new double[n2]; var vols = new double[n1, n2]; PopulateArrays(ref rows, ref cols, ref vols); var mvols = new Matrix(vols); var linInterp = new LinearInterpolation(); var qrWing = new WingModelInterpolation(); var interpCurve = new ExtendedInterpolatedSurface(rows, cols, mvols, linInterp, qrWing); InterpCurve = interpCurve; }
public void LinearInterpolation_OnePoint() { LinearInterpolation target = new LinearInterpolation(); target.Add(0, 1); Assert.AreEqual(0, target.GetAdjustedValue(1)); }
public void OnePoint() { var target = new LinearInterpolation(); target.AddReferencePoint(0, 1); Assert.Equal(0, target.GetAdjustedValue(1)); }
public void NoCalibration() { var target = new LinearInterpolation(); Assert.Equal(0, target.GetAdjustedValue(0)); Assert.Equal(1, target.GetAdjustedValue(1)); }
public void TestValueOutOfRange() { LinearInterpolation interpolation = new LinearInterpolation(new[] { 1.0, 2.0, 3.0 }, new[] { 1.0, 2.0, 3.0 }); double value = interpolation.Interpolate(3.5); Assert.AreEqual(3.5, value); }
public void TestUnsortedValues() { LinearInterpolation interpolation = new LinearInterpolation(new[] { 2.0, 1.0, 3.0, 1.5 }, new[] { 2.0, 1.0, 3.0, 1.5 }); double value = interpolation.Interpolate(2.5); Assert.AreEqual(2.5, value); }
public void TestGeneral() { LinearInterpolation interpolation = new LinearInterpolation(new [] { 1.0, 2.0, 3.0 }, new [] { 1.0, 2.0, 3.0 }); double value = interpolation.Interpolate(1.5); Assert.AreEqual(1.5, value); }
public void Visualize(IEnumerable <RateAtDate> data, int numPoints) { const string LinearFile = "Lin.csv"; const string CosineFile = "Cos.csv"; const string CubicFile = "Cub.csv"; DateTime start = data.First().Date; LinearInterpolation lin = new LinearInterpolation(data); CosineInterpolation cos = new CosineInterpolation(data); CubicInterpolation cub = new CubicInterpolation(data); using (StreamWriter linsw = new StreamWriter(fileRoot + LinearFile)) using (StreamWriter cossw = new StreamWriter(fileRoot + CosineFile)) using (StreamWriter cubsw = new StreamWriter(fileRoot + CubicFile)) { double increment = ((double)(data.Last().Date - data.First().Date).TotalMilliseconds) / (double)numPoints; for (int i = 0; i < numPoints; i++) { DateTime writeDate = start + TimeSpan.FromMilliseconds((i * increment)); linsw.WriteLine(lin.GetRate(writeDate)); cossw.WriteLine(cos.GetRate(writeDate)); cubsw.WriteLine(cub.GetRate(writeDate)); } } ShowData(fileRoot + LinearFile); ShowData(fileRoot + CosineFile); ShowData(fileRoot + CubicFile); }
private void OpenGrabber() { Robotmap map = Robotmap.GetInstance(); float pulses = LinearInterpolation.Calculate(OPENGRABBER, -1.0f, minPWMSignalRange, 1.0f, maxPWMSignalRange); float percentOut = pulses / pwmOutput; CANController.SetPWMOutput(map.GetFlagGrabberServo_ID(), pulses); //move servo } //FLAGSURVO LOOK LOOK LOOOK LOOK LOOK LOOK LOOK DO THIS FIX IT HE SHOWED YOU YOU GOTTA DO IT LLLLLLOOOOOOOOOOOK
private void CloseGrabber() { Robotmap map = Robotmap.GetInstance(); float pulses = LinearInterpolation.Calculate(CLOSEGRABBER, -1.0f, minPWMSignalRange, 1.0f, maxPWMSignalRange); float percentOut = pulses / pwmOutput; CANController.SetPWMOutput(map.GetFlagGrabberServo_ID(), percentOut); //move servo }
public void Above_1() { var target = new LinearInterpolation(); target.AddReferencePoint(11, 10); Assert.Equal(11, target.GetAdjustedValue(10)); Assert.Equal(10, target.GetAdjustedValue(9)); }
public void Below_1() { var target = new LinearInterpolation(); target.AddReferencePoint(1, 0); Assert.Equal(1, target.GetAdjustedValue(0)); Assert.Equal(2, target.GetAdjustedValue(1)); }
public void Middle_3() { var target = new LinearInterpolation(); target.AddReferencePoint(0, 0); target.AddReferencePoint(1, 10); Assert.Equal(0.5, target.GetAdjustedValue(5)); }
public void ComputeRateForValidEntries() { var linearInterpolation = new LinearInterpolation(); var result = linearInterpolation.ComputeRate(1, 2, 1, 1, 1.5); Assert.AreEqual(1, result); }
public void LinearInterpolation_Above_1() { LinearInterpolation target = new LinearInterpolation(); target.Add(11, 10); Assert.AreEqual(11, target.GetAdjustedValue(10)); Assert.AreEqual(10, target.GetAdjustedValue(9)); }
public void LinearInterpolation_Below_1() { LinearInterpolation target = new LinearInterpolation(); target.Add(1, 0); Assert.AreEqual(1, target.GetAdjustedValue(0)); Assert.AreEqual(2, target.GetAdjustedValue(1)); }
public void LinearInterpolation_Middle_3() { LinearInterpolation target = new LinearInterpolation(); target.Add(0, 0); target.Add(1, 10); Assert.AreEqual(0.5, target.GetAdjustedValue(5)); }
public void CalibratedThreePoints() { var target = new LinearInterpolation(); target.AddReferencePoint(1, 1.1); target.AddReferencePoint(2, 1.2); target.AddReferencePoint(3, 1.3); Assert.Equal(2.5, target.GetAdjustedValue(1.25)); }
public void LinearInterpolation_CalibratedThreePoints() { LinearInterpolation target = new LinearInterpolation(); target.Add(1, 1.1); target.Add(2, 1.2); target.Add(3, 1.3); Assert.AreEqual(2.5, target.GetAdjustedValue(1.25)); }
public void CalibratedValueHit() { var target = new LinearInterpolation(); target.AddReferencePoint(1, 0); target.AddReferencePoint(2, 1); target.AddReferencePoint(3, 2); Assert.Equal(3, target.GetAdjustedValue(2)); }
private void deliver() { Robotmap map = Robotmap.GetInstance(); float pulses = LinearInterpolation.Calculate(DELIVER, -1.0f, minPWMSignalRange, 1.0f, maxPWMSignalRange); float percentOut = pulses / pwmOutput; deliverServo.EnablePWMOutput((int)map.GetDeliverMec_ID(), true); deliverServo.SetPWMOutput(map.GetDeliverMec_ID(), percentOut); //move servo }
public void LinearInterpolation_CalibratedValueHit() { LinearInterpolation target = new LinearInterpolation(); target.Add(1, 0); target.Add(2, 1); target.Add(3, 2); Assert.AreEqual(3, target.GetAdjustedValue(2)); }
/// <summary> /// Main ctor. /// </summary> /// <param name="discreteSurface">The discrete surface ie 2 dimensional.</param> /// <param name="forwards">The forwards to be used for calibration.</param> /// <param name="xInterpolation">The basic interpolation to be applied to the x axis.</param> /// <param name="yInterpolation">The interpolation type for the y axis</param> /// <param name="allowExtrapolation">Not implemented in EO.</param> protected ExtendedInterpolatedSurface(DiscreteSurface discreteSurface, ParametricAdjustmentPoint[] forwards, IInterpolation xInterpolation, IInterpolation yInterpolation, bool allowExtrapolation) : base(discreteSurface, xInterpolation, yInterpolation, allowExtrapolation) { var values = discreteSurface.GetMatrixOfValues(); var x = discreteSurface.XArray; var width = values.ColumnCount; if (forwards != null) { SpotValue = (double)forwards[0].parameterValue; var length = forwards.Length; var fwds = new double[length - 1]; for (var index = 1; index < length; index++) { fwds[index - 1] = (double)forwards[index].parameterValue; } var fwdcurve = new LinearInterpolation(); fwdcurve.Initialize(x, fwds); ForwardCurve = fwdcurve; } if (yInterpolation.GetType() == typeof(SABRModelInterpolation)) { var y = discreteSurface.YArray; var length = values.RowCount; for (int i = 0; i < length; i++) { //interpolate each maturity first (in strike) with SABR var yinterp = (SABRModelInterpolation)yInterpolation.Clone(); yinterp.ExpiryTime = x[i]; if (Forward != null && Spot != null) { yinterp.AssetPrice = Convert.ToDecimal(Forward); } else { var fwd = ForwardCurve.ValueAt(yinterp.ExpiryTime, true); yinterp.AssetPrice = Convert.ToDecimal(fwd); } yinterp.Initialize(y, values.Row(i).Data); var curve = new DiscreteCurve(y, values.Row(i).Data); var interpolatedCol = new InterpolatedCurve(curve, yinterp, true); _interpolatedColumns.Add(interpolatedCol); } } else //o.w interpolate at each strike point (in time) { for (int i = 0; i < width; i++) { var interp = xInterpolation.Clone(); interp.Initialize(x, values.Column(i).Data); var curve = new DiscreteCurve(x, values.Column(i).Data); var interpolatedCol = new InterpolatedCurve(curve, interp, true); _interpolatedColumns.Add(interpolatedCol); } } }
public void InterpolateTest() { var keyPoints = new AbsoluteKeyPointCollection <int, int>(new ValueTypeInterpolationProvider <int>(), 0) { 0, 10 }; var interpolate = new LinearInterpolation <int, int>(keyPoints); Assert.Equal(5, interpolate.Interpolate(0.5)); }
public void LinearInterpolation_Above_2() { LinearInterpolation target = new LinearInterpolation(); target.Add(6, 3); target.Add(8, 4); target.Add(10, 5); Assert.AreEqual(2, target.GetAdjustedValue(1)); Assert.AreEqual(4, target.GetAdjustedValue(2)); }
public void LinearInterpolation_Below_2() { LinearInterpolation target = new LinearInterpolation(); target.Add(2, 1); target.Add(4, 2); target.Add(6, 3); Assert.AreEqual(8, target.GetAdjustedValue(4)); Assert.AreEqual(10, target.GetAdjustedValue(5)); }
public void LinearInterpolation_Middle_1() { LinearInterpolation target = new LinearInterpolation(); target.Add(2, 1); target.Add(6, 3); Assert.AreEqual(2, target.GetAdjustedValue(1)); Assert.AreEqual(4, target.GetAdjustedValue(2)); Assert.AreEqual(6, target.GetAdjustedValue(3)); }
private Interpolation put_imvolFitting() { List <double> xGrid = this.put_strikeData_; List <double> yGrid = this.put_imvolData_; //List<double> xGrid = Enumerable.Range(0, 100); LinearInterpolation cubic = new LinearInterpolation(xGrid, xGrid.Count, yGrid); return(cubic); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(LinearInterpolation obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }