org.apache.commons.math3.analysis.function.Gaussian Java Examples

The following examples show how to use org.apache.commons.math3.analysis.function.Gaussian. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IterativeLegendreGaussIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNormalDistributionWithLargeSigma() {
    final double sigma = 1000;
    final double mean = 0;
    final double factor = 1 / (sigma * FastMath.sqrt(2 * FastMath.PI));
    final UnivariateFunction normal = new Gaussian(factor, mean, sigma);

    final double tol = 1e-2;
    final IterativeLegendreGaussIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, tol, tol);

    final double a = -5000;
    final double b = 5000;
    final double s = integrator.integrate(50, normal, a, b);
    Assert.assertEquals(1, s, 1e-5);
}
 
Example #2
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #3
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        2.776e-17, 1.742e-15, 2.385e-13, 1.329e-11, 2.668e-9, 8.873e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #4
Source File: IterativeLegendreGaussIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNormalDistributionWithLargeSigma() {
    final double sigma = 1000;
    final double mean = 0;
    final double factor = 1 / (sigma * FastMath.sqrt(2 * FastMath.PI));
    final UnivariateFunction normal = new Gaussian(factor, mean, sigma);

    final double tol = 1e-2;
    final IterativeLegendreGaussIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, tol, tol);

    final double a = -5000;
    final double b = 5000;
    final double s = integrator.integrate(50, normal, a, b);
    Assert.assertEquals(1, s, 1e-5);
}
 
Example #5
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #6
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #7
Source File: IterativeLegendreGaussIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNormalDistributionWithLargeSigma() {
    final double sigma = 1000;
    final double mean = 0;
    final double factor = 1 / (sigma * FastMath.sqrt(2 * FastMath.PI));
    final UnivariateFunction normal = new Gaussian(factor, mean, sigma);

    final double tol = 1e-2;
    final IterativeLegendreGaussIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, tol, tol);

    final double a = -5000;
    final double b = 5000;
    final double s = integrator.integrate(50, normal, a, b);
    Assert.assertEquals(1, s, 1e-5);
}
 
Example #8
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #9
Source File: IterativeLegendreGaussIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNormalDistributionWithLargeSigma() {
    final double sigma = 1000;
    final double mean = 0;
    final double factor = 1 / (sigma * FastMath.sqrt(2 * FastMath.PI));
    final UnivariateFunction normal = new Gaussian(factor, mean, sigma);

    final double tol = 1e-2;
    final IterativeLegendreGaussIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, tol, tol);

    final double a = -5000;
    final double b = 5000;
    final double s = integrator.integrate(50, normal, a, b);
    Assert.assertEquals(1, s, 1e-5);
}
 
Example #10
Source File: FiniteDifferencesDifferentiatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGaussian() {
    FiniteDifferencesDifferentiator differentiator =
            new FiniteDifferencesDifferentiator(9, 0.02);
    UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0);
    UnivariateDifferentiableFunction f =
            differentiator.differentiate(gaussian);
    double[] expectedError = new double[] {
        6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8
    };
   double[] maxError = new double[expectedError.length];
    for (double x = -10; x < 10; x += 0.1) {
        DerivativeStructure dsX  = new DerivativeStructure(1, maxError.length - 1, 0, x);
        DerivativeStructure yRef = gaussian.value(dsX);
        DerivativeStructure y    = f.value(dsX);
        Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
        for (int order = 0; order <= yRef.getOrder(); ++order) {
            maxError[order] = FastMath.max(maxError[order],
                                    FastMath.abs(yRef.getPartialDerivative(order) -
                                                 y.getPartialDerivative(order)));
        }
    }
    for (int i = 0; i < maxError.length; ++i) {
        Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]);
    }
}
 
Example #11
Source File: GaussFitEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length >= 3) {
    throw new IOException("gaussfit function takes a maximum of 2 arguments.");
  }

  Object first = objects[0];

  double[] x = null;
  double[] y = null;

  if(objects.length == 1) {
    //Only the y values passed

    y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    x = new double[y.length];
    for(int i=0; i<y.length; i++) {
      x[i] = i;
    }

  } else if(objects.length == 2) {
    // x and y passed
    Object second = objects[1];
    x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();


  }

  GaussianCurveFitter curveFitter = GaussianCurveFitter.create();

  WeightedObservedPoints points = new WeightedObservedPoints();
  for(int i=0; i<x.length; i++) {
    points.add(x[i], y[i]);
  }

  List<WeightedObservedPoint> pointList = points.toList();

  double[] guess = new GaussianCurveFitter.ParameterGuesser(pointList).guess();
  curveFitter = curveFitter.withStartPoint(guess);

  double[] coef = curveFitter.fit(pointList);
  Gaussian gaussian = new Gaussian(coef[0], coef[1], coef[2]);
  List list = new ArrayList();
  for(double xvalue : x) {
    double yvalue= gaussian.value(xvalue);
    list.add(yvalue);
  }

  return new VectorFunction(gaussian, list);
}
 
Example #12
Source File: Windows.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
private static double[] gaussianWindow(int size) {
    double sigma = ((double) size - 1) / 2;
    double[] gaussian = new double[size];
    Gaussian distribution = new Gaussian(((double) size - 1.) / 2.0, sigma);
    for (int i = 0; i < size; i++) {
        gaussian[i] = distribution.value(i);
    }
    return gaussian;
}
 
Example #13
Source File: KohonenUpdateAction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void update(Network net,
                   double[] features) {
    final long numCalls = numberOfCalls.incrementAndGet();
    final double currentLearning = learningFactor.value(numCalls);
    final Neuron best = findAndUpdateBestNeuron(net,
                                                features,
                                                currentLearning);

    final int currentNeighbourhood = neighbourhoodSize.value(numCalls);
    // The farther away the neighbour is from the winning neuron, the
    // smaller the learning rate will become.
    final Gaussian neighbourhoodDecay
        = new Gaussian(currentLearning,
                       0,
                       1d / currentNeighbourhood);

    if (currentNeighbourhood > 0) {
        // Initial set of neurons only contains the winning neuron.
        Collection<Neuron> neighbours = new HashSet<Neuron>();
        neighbours.add(best);
        // Winning neuron must be excluded from the neighbours.
        final HashSet<Neuron> exclude = new HashSet<Neuron>();
        exclude.add(best);

        int radius = 1;
        do {
            // Retrieve immediate neighbours of the current set of neurons.
            neighbours = net.getNeighbours(neighbours, exclude);

            // Update all the neighbours.
            for (Neuron n : neighbours) {
                updateNeighbouringNeuron(n, features, neighbourhoodDecay.value(radius));
            }

            // Add the neighbours to the exclude list so that they will
            // not be update more than once per training step.
            exclude.addAll(neighbours);
            ++radius;
        } while (radius <= currentNeighbourhood);
    }
}
 
Example #14
Source File: KohonenUpdateAction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void update(Network net,
                   double[] features) {
    final long numCalls = numberOfCalls.incrementAndGet();
    final double currentLearning = learningFactor.value(numCalls);
    final Neuron best = findAndUpdateBestNeuron(net,
                                                features,
                                                currentLearning);

    final int currentNeighbourhood = neighbourhoodSize.value(numCalls);
    // The farther away the neighbour is from the winning neuron, the
    // smaller the learning rate will become.
    final Gaussian neighbourhoodDecay
        = new Gaussian(currentLearning,
                       0,
                       1d / currentNeighbourhood);

    if (currentNeighbourhood > 0) {
        // Initial set of neurons only contains the winning neuron.
        Collection<Neuron> neighbours = new HashSet<Neuron>();
        neighbours.add(best);
        // Winning neuron must be excluded from the neighbours.
        final HashSet<Neuron> exclude = new HashSet<Neuron>();
        exclude.add(best);

        int radius = 1;
        do {
            // Retrieve immediate neighbours of the current set of neurons.
            neighbours = net.getNeighbours(neighbours, exclude);

            // Update all the neighbours.
            for (Neuron n : neighbours) {
                updateNeighbouringNeuron(n, features, neighbourhoodDecay.value(radius));
            }

            // Add the neighbours to the exclude list so that they will
            // not be update more than once per training step.
            exclude.addAll(neighbours);
            ++radius;
        } while (radius <= currentNeighbourhood);
    }
}