List of usage examples for org.apache.commons.math.analysis.polynomials PolynomialSplineFunction value
public double value(double v) throws ArgumentOutsideDomainException
From source file:ch.algotrader.option.OptionUtil.java
/** * Gets the implied volatility of a {@link Option} based on a {@link SABRSurfaceVO}. */// w w w . jav a 2 s.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 void checkFunction(PolynomialSplineFunction function, PeakListRow row, int batch, PeakListRow batches, PeakListRow runOrder, List<String> names) { boolean ok = true; for (String sampleNames : names) { if (batch == (Integer) batches.getPeak(sampleNames)) { try { double value = function.value((Double) runOrder.getPeak(sampleNames)); if (value <= 0.0 || value == Double.NaN) { ok = false;// w ww . j a v a2 s . c om } } catch (ArgumentOutsideDomainException ex) { Logger.getLogger(SerumHuNormalizationTask.class.getName()).log(Level.SEVERE, null, ex); } } } if (!ok) { System.out.println("values are zero, negative or nan " + row.getID()); } }
From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java
private void plotInterpolations(Dataset data) { this.message = "Creating Plots"; final Rengine rEngine; try {// w w w .jav a2 s .c o m rEngine = RUtilities.getREngine(); } catch (Throwable t) { throw new IllegalStateException( "Plotting requires R but it couldn't be loaded (" + t.getMessage() + ')'); } synchronized (RUtilities.R_SEMAPHORE) { this.totalRows = data.getNumberRows(); this.processedRows = 0; message = "Plotting curves"; List<String> names = data.getAllColumnNames(); // assing the values to the matrix String path = this.fileName.getAbsolutePath(); path = path.replaceAll("\\\\", "/"); rEngine.eval("pdf(\"" + path + "\")"); rEngine.eval("p <- vector(mode=\"numeric\",length=" + data.getNumberCols() + ")"); rEngine.eval("loess <- vector(mode=\"numeric\",length=" + data.getNumberCols() + ")"); rEngine.eval("time <- vector(mode=\"numeric\",length=" + data.getNumberCols() + ")"); rEngine.eval("color <- vector(mode=\"numeric\",length=" + data.getNumberCols() * 2 + ")"); for (int row = 0; row < data.getNumberRows(); row++) { List<PolynomialSplineFunction> functions = this.functions.get(data.getRow(row).getID()); double lastPoint = 0; for (int col = 0; col < data.getNumberCols(); col++) { int r = col + 1; rEngine.eval("p[" + r + "] <- " + data.getRow(row).getPeak(names.get(col))); if (functions != null) { PolynomialSplineFunction function = functions .get(Double.valueOf((Double) batches.getPeak(names.get(col))).intValue()); if (function != null) { try { rEngine.eval("loess[" + r + "] <- " + function.value((Double) runOrder.getPeak(names.get(col)))); lastPoint = function.value((Double) runOrder.getPeak(names.get(col))); } catch (ArgumentOutsideDomainException ex) { rEngine.eval("loess[" + r + "] <- " + lastPoint); } } } rEngine.eval("time[" + r + "] <- " + col); } rEngine.eval("plot(p~time, xlab=\"Running time\", ylab=\"Intensity\")"); rEngine.eval("lines(loess~time)"); String name = data.getRow(row).getID() + "-" + data.getRow(row).getName(); rEngine.eval("title(main = \"" + name + "\")"); this.processedRows++; } rEngine.eval("dev.off()"); } }
From source file:guineu.modules.filter.Alignment.SerumHuNormalization.SerumHuNormalizationTask.java
private void normalize(Dataset data, Dataset newData) { // First row => ids of the samples ( 0 == standard serum, 1 == normal sample) this.ids = data.getRow(0).clone(); // Second row => run order this.runOrder = data.getRow(0).clone(); // Third row => different data sets this.batches = data.getRow(0).clone(); for (String name : data.getAllColumnNames()) { ids.setPeak(name, data.getParametersValue(name, this.id)); runOrder.setPeak(name, data.getParametersValue(name, this.order)); batches.setPeak(name, data.getParametersValue(name, this.batchesName)); }//from w w w. j a v a 2 s . c om int numBatches = 1; double n = (Double) batches.getPeak(data.getAllColumnNames().get(0)); for (String name : data.getAllColumnNames()) { if ((Double) batches.getPeak(name) > n) { numBatches++; n = (Double) batches.getPeak(name); } } this.createCurves(data, numBatches); for (int batch = 0; batch < numBatches; batch++) { message = "Normalizing"; this.totalRows = data.getNumberRows(); this.processedRows = 0; List<String> names = data.getAllColumnNames(); for (int i = 0; i < data.getNumberRows(); i++) { this.processedRows++; PeakListRow row = data.getRow(i); PeakListRow newrow = newData.getRow(i); try { // Get the interpolation of all the human serum points using Loess PolynomialSplineFunction function = functions.get(row.getID()).get(batch); if (function != null) { // Prepare the points for the extrapolation PolynomialFunction extrapolationFunction = null; if (this.extrapolation) { List<Double> points = new ArrayList<Double>(); for (int e = 0; e < row.getNumberPeaks(); e++) { if ((Double) batches.getPeak(names.get(e)) == batch) { try { points.add(function.value((Double) runOrder.getPeak(names.get(e)))); } catch (ArgumentOutsideDomainException ex) { Logger.getLogger(SerumHuNormalizationTask.class.getName()).log(Level.SEVERE, null, ex); } } } // Extrapolation function extrapolationFunction = this.fittPolinomialFunction(batches, runOrder, names, batch, points); } double lastPoint = 0; for (int e = 0; e < row.getNumberPeaks(); e++) { String sampleName = names.get(e); if ((Double) ids.getPeak(sampleName) > 0.0) { if ((Double) batches.getPeak(sampleName) == batch) { try { if ((Double) ids.getPeak(sampleName) == 0) { lastPoint = function.value((Double) runOrder.getPeak(sampleName)); } double value = 0; try { Double controlMol = function .value((Double) runOrder.getPeak(names.get(e))); if (controlMol < 0.0 || controlMol == Double.NaN || controlMol == Double.POSITIVE_INFINITY || controlMol == Double.NEGATIVE_INFINITY) { controlMol = getAverage(ids, row, e, names); } value = (Double) row.getPeak(sampleName) / controlMol; if (value < 0.0 || value == Double.NaN || value == Double.POSITIVE_INFINITY || value == Double.NEGATIVE_INFINITY) { controlMol = getAverage(ids, row, e, names); } value = (Double) row.getPeak(sampleName) / controlMol; } catch (ClassCastException exception) { value = -100; } newrow.setPeak(sampleName, value); } catch (ArgumentOutsideDomainException ex) { // ex.printStackTrace(); //if the value has to be extrapolated if (extrapolation && extrapolationFunction != null) { double value = 0; try { Double controlMol = extrapolationFunction .value((Double) runOrder.getPeak(names.get(e))); if (controlMol < 0.0 || controlMol == Double.NaN || controlMol == Double.POSITIVE_INFINITY || controlMol == Double.NEGATIVE_INFINITY) { controlMol = getAverage(ids, row, e, names); } value = (Double) row.getPeak(sampleName) / controlMol; if (value < 0.0 || value == Double.NaN || value == Double.POSITIVE_INFINITY || value == Double.NEGATIVE_INFINITY) { controlMol = getAverage(ids, row, e, names); } value = (Double) row.getPeak(sampleName) / controlMol; } catch (ClassCastException exception) { value = -100; } newrow.setPeak(sampleName, value); } else { double value = 0; try { value = (Double) row.getPeak(sampleName) / lastPoint;//extrapolationFunction.value((Double) runOrder.getPeak(names.elementAt(e))); } catch (ClassCastException exception) { value = -100; } newrow.setPeak(sampleName, value); } } } } } } else { System.out.println("Function is null" + row.getID()); } } catch (Exception exception) { exception.printStackTrace(); System.out.println(row.getID()); } } } }
From source file:org.micromanager.plugins.magellan.autofocus.CrossCorrelationAutofocus.java
/** * * @param original// www . ja v a 2 s .com * @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; }