Java Code Examples for org.apache.commons.math.analysis.integration.TrapezoidIntegrator

Following code examples demonstrate how to use org.apache.commons.math.analysis.integration.TrapezoidIntegratorfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.commons.math.analysis.integration.TrapezoidIntegratorand various code implementation of this class.

    public void testPdf() {

        for (int i = 0; i < shape1s.length; ++i) {
            final GeneralizedIntegerGammaDistribution gig = new GeneralizedIntegerGammaDistribution(
                    shape1s[i], shape2s[i], rate1s[i], rate2s[i]
            );

            TrapezoidIntegrator integrator = new TrapezoidIntegrator();
            double m0 = 0.0;
            double m1 = 0.0;
            double m2 = 0.0;
            try {
                m0 = integrator.integrate(
                        new UnivariateRealFunction() {
                            @Override
                            public double value(double x) throws FunctionEvaluationException {
                                final double pdf = gig.pdf(x);
                                return pdf;
                            }
                        }, 0.0, 1000.0);

                m1 = integrator.integrate(
                        new UnivariateRealFunction() {
                            @Override
                            public double value(double x) throws FunctionEvaluationException {
                                final double pdf = gig.pdf(x);
                                return x * pdf;
                            }
                        }, 0.0, 1000.0);


                m2 = integrator.integrate(
                        new UnivariateRealFunction() {
                            @Override
                            public double value(double x) throws FunctionEvaluationException {
                                final double pdf = gig.pdf(x);
                                return x * x * pdf;
                            }
                        }, 0.0, 1000.0);

            } catch (MaxIterationsExceededException e) {
                e.printStackTrace();
            } catch (FunctionEvaluationException e) {
                e.printStackTrace();
            }

            assertEquals(1.0, m0, tolerance);

            double mean = shape1s[i] / rate1s[i] + shape2s[i] / rate2s[i];
            assertEquals(mean, m1, tolerance);

            m2 -= m1 * m1;
            double variance = shape1s[i] / rate1s[i] / rate1s[i] + shape2s[i] / rate2s[i] / rate2s[i];
        }
    } 


        public double value(final double val) throws FunctionEvaluationException {
            try {
                return function.value(val);
            } catch (final FunctionEvaluationException e) {
            }
            return 0;
        } 

    public double value(SimulationData simulationData) {

        TrapezoidIntegrator integrator = new TrapezoidIntegrator();

        UnivariateRealFunction f = new PiecewiseConstantFunction(
                simulationData.getTimesteps(), simulationData
                .getSimulationData().selectColumns(Ret.LINK, 0)
                .transpose().toDoubleArray()[0]);
        try {
            return Math.exp(-1 * integrator.integrate(f, 0, bondMaturity));
        } catch (Exception ex) {
            throw new RuntimeException("Error integrating spot rate", ex);
        }
    } 

	public static void main(String[] args) {
		GaussFunction gf = new GaussFunction(0.2);
		TrapezoidIntegrator integrator = new TrapezoidIntegrator(gf);
		
		double value;
		try {
			System.out.println("start");
			value = integrator.integrate(-50000,0.5);
			System.out.println(value);
		} catch (MaxIterationsExceededException e) {
			e.printStackTrace();
		} catch (FunctionEvaluationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 

    private double zcbFromFwdCurve(double[] tenors, double[] fwdRates, double bondMaturity) {
        TrapezoidIntegrator integrator = new TrapezoidIntegrator();
        UnivariateRealFunction f = new PiecewiseConstantFunction(tenors, fwdRates);
        try {
            return Math.exp(-1 * integrator.integrate(f, 0, bondMaturity));
        } catch (Exception ex) {
            throw new RuntimeException("Error integrating spot rate", ex);
        }
    } 

  public void test08()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.setRelativeAccuracy(0.0);
      trapezoidIntegrator0.getRelativeAccuracy();
      assertEquals(64, trapezoidIntegrator0.getMaximalIterationCount());
      assertEquals(0, trapezoidIntegrator0.getIterationCount());
  } 

  public void test14()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.getRelativeAccuracy();
      assertEquals(0, trapezoidIntegrator0.getIterationCount());
      assertEquals(64, trapezoidIntegrator0.getMaximalIterationCount());
  } 

  public void test02()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.setRelativeAccuracy(314.7287817584456);
      assertEquals(314.7287817584456, trapezoidIntegrator0.getRelativeAccuracy(), 0.01);
  } 

  public void test05()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.resetRelativeAccuracy();
      assertEquals(1.0E-14, trapezoidIntegrator0.getRelativeAccuracy(), 0.01);
  } 

  public void test13()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.setRelativeAccuracy((-40.689161469562634));
      double double0 = trapezoidIntegrator0.getRelativeAccuracy();
      assertEquals((-40.689161469562634), double0, 0.01);
  } 

  public void test07()  throws Throwable  {
      TrapezoidIntegrator trapezoidIntegrator0 = new TrapezoidIntegrator();
      trapezoidIntegrator0.resetAbsoluteAccuracy();
      assertEquals(0, trapezoidIntegrator0.getIterationCount());
      assertEquals(64, trapezoidIntegrator0.getMaximalIterationCount());
  } 

    public void testLatentStateBranchRateModel() throws FunctionEvaluationException, MaxIterationsExceededException {

        LatentStateBranchRateModel model = new LatentStateBranchRateModel(
                new Parameter.Default(0.001), new Parameter.Default(0.5));

        TrapezoidIntegrator integator = new TrapezoidIntegrator();

        final double branchLength = 2000;
        double integral = integator.integrate(new LatentStateDensityFunction(model, branchLength), 0.0, 1.0);

        System.out.println("testLatentStateBeanchRateModel");
        System.out.println("Integral = " + integral);

        assertEquals(integral, 1.0, tolerance);
    } 

	public double getConfidence() {
		try {
			TrapezoidIntegrator integrator = new TrapezoidIntegrator();

			UnivariateRealFunction p = new UnivariateRealFunction() {

				@Override
				public double value(double x) throws FunctionEvaluationException {
					return Math.pow(x, r) * Math.pow(1 - x, s);
				}
				
			};

			final double intp = integrator.integrate(p, 0, 1);
			
			if (intp == 0)
				return 1;
			
			UnivariateRealFunction q = new UnivariateRealFunction() {

				@Override
				public double value(double x) throws FunctionEvaluationException {
					return Math.abs((Math.pow(x, r) * Math.pow(1 - x, s)) / intp - 1);
				}
				
			};
			
			return integrator.integrate(q, 0, 1) / 2;
		} catch (MaxIterationsExceededException | FunctionEvaluationException
				| IllegalArgumentException e) {
			throw new Error(e);
		}
	} 

Advertisement
Advertisement