Example usage for org.apache.commons.math.analysis.interpolation SplineInterpolator SplineInterpolator

List of usage examples for org.apache.commons.math.analysis.interpolation SplineInterpolator SplineInterpolator

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.interpolation SplineInterpolator SplineInterpolator.

Prototype

SplineInterpolator

Source Link

Usage

From source file:ch.algotrader.option.OptionUtil.java

/**
 * Gets the implied volatility of a {@link Option} based on a {@link SABRSurfaceVO}.
 *//*www .  j  av a2s .c  o  m*/
public static double getImpliedVolatilitySABR(final double underlyingSpot, final double strike,
        final double years, final double intrest, final double dividend, final OptionType type,
        final SABRSurfaceVO surface) throws MathException {

    double forward = getForward(underlyingSpot, years, intrest, dividend);

    // get sabrVolas for all durations at the specified strike
    int i = 0;
    double[] yearsArray = new double[surface.getSmiles().size()];
    double[] volArray = new double[surface.getSmiles().size()];
    for (SABRSmileVO smile : surface.getSmiles()) {

        double vol = SABR.volByAtmVol(forward, strike, smile.getAtmVol(), smile.getYears(), beta,
                smile.getRho(), smile.getVolVol());

        yearsArray[i] = smile.getYears();
        volArray[i] = vol;
        i++;
    }

    // spline interpolation for years
    SplineInterpolator interpolator = new SplineInterpolator();
    PolynomialSplineFunction function = interpolator.interpolate(yearsArray, volArray);

    return function.value(years);
}

From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java

private PolynomialSplineFunction createCurve() {
    try {/*from  ww  w .  ja va2s  .  c  o  m*/
        // if there are more than 2 points in the model
        LoessInterpolator loess = null;
        if (this.loessBandwidth == 0 && this.iterations == 0) {
            loess = new LoessInterpolator();
        } else {
            loess = new LoessInterpolator(this.loessBandwidth, this.iterations);
        }
        double[] newy = loess.smooth(xval, yval);
        PolynomialSplineFunction function = loess.interpolate(xval, newy);
        return function;

    } catch (MathException ex) {
        Logger.getLogger(SerumHuNormalizationTask.class.getName()).log(Level.SEVERE, null, ex);

        SplineInterpolator loess = new SplineInterpolator();
        PolynomialSplineFunction function = loess.interpolate(xval, yval);
        return function;

    }
}

From source file:org.micromanager.plugins.magellan.autofocus.CrossCorrelationAutofocus.java

/**
 *
 * @param original//from   w  ww.j  a v  a2  s .c  om
* @param current
* @param pixelSizeZ
* @return double representing the focus position of current relative to original (i.e. 4 means
* that current is focused 4 um deeper than current)
*/
private double calcFocusDrift(String acqName, final ImageStack tp0Stack, final ImageStack currentTPStack,
        double pixelSizeZ) throws Exception {
    Log.log(acqName + " Autofocus: cross correlating", true);
    //do actual autofocusing on a seperate thread so a bug in it won't crash everything
    Future<ImageStack> f = afExecutor_.submit(new Callable<ImageStack>() {
        @Override
        public ImageStack call() throws Exception {
            return FHTImage3D.crossCorrelation(tp0Stack, currentTPStack);
        }
    });
    ImageStack xCorrStack;
    try {
        xCorrStack = f.get(AF_TIMEOUT_MIN, TimeUnit.MINUTES);
    } catch (InterruptedException ex) {
        Log.log("autofocus aborted");
        throw new Exception();
    } catch (ExecutionException ex) {
        Log.log("Exception while running autofocus");
        Log.log(ex);
        throw new Exception();
    } catch (TimeoutException ex) {
        Log.log("Autofocus timeout for acquisition: " + acqName);
        throw new Exception();
    }

    Log.log(acqName + " Autofocus: finished cross correlating..calculating drift", true);
    ImagePlus xCorr = new ImagePlus("XCorr", xCorrStack);
    //find the maximum cross correlation intensity at each z slice
    double[] ccIntensity = new double[xCorr.getNSlices()], interpolatedCCMax = new double[xCorr.getNSlices()];
    for (int i = 1; i <= ccIntensity.length; i++) {
        xCorr.setSlice(i);
        ccIntensity[i - 1] = findMaxPixelVal((float[]) xCorr.getProcessor().getPixels(), xCorr.getWidth(),
                xCorr.getHeight());
        interpolatedCCMax[i - 1] = i - 1;
    }

    //find maximum value of interpolated spline function
    PolynomialSplineFunction func = new SplineInterpolator().interpolate(interpolatedCCMax, ccIntensity);
    double[] sliceIndexInterpolationPoints = new double[(int) (SPLINE_PRECISION
            * (interpolatedCCMax.length - 1))];
    int maxIndex = 0;
    for (int i = 0; i < sliceIndexInterpolationPoints.length; i++) {
        sliceIndexInterpolationPoints[i] = i / SPLINE_PRECISION;
        try {
            if (func.value(sliceIndexInterpolationPoints[i]) > func
                    .value(sliceIndexInterpolationPoints[maxIndex])) {
                maxIndex = i;
            }
        } catch (ArgumentOutsideDomainException ex) {
            Log.log("Spline value calculation outside range");
        }
    }
    //get maximum value of xCorr in slice index units
    double ccMaxSliceIndex = sliceIndexInterpolationPoints[maxIndex];
    //convert to um
    double drift_um = (ccMaxSliceIndex - (((double) xCorr.getNSlices()) / 2.0)) * pixelSizeZ;
    xCorr.close();

    return drift_um;
}