Ejemplo n.º 1
0
        private void GenerateOrthogonality()
        {
            double a   = 0.01;
            double b   = 10e4;
            var    lg  = new Laguerre(FirstDegree);
            var    lg2 = new Laguerre(SecondDegree);
            double dx  = (b - a) / (steps - 1);
            double x;
            double integral = 0.0;

            alpha = 0;
            double fx1;

            if (alpha != 0)
            {
                integral = NewtonCotesTrapeziumRule.IntegrateTwoPoint(z => (lg.Polynomial.FunctionValueInPoint(z) * lg2.Polynomial.FunctionValueInPoint(z) * Math.Pow(z, -alpha) * Math.Exp(-z)), 0.0, 100);
            }
            else
            {
                for (int i = 0; i < steps; i++)
                {
                    //x = Convert.ToDouble(i);
                    x   = Convert.ToDouble(i) * dx + a;
                    fx1 = lg.Polynomial.FunctionValueInPoint(x) * lg2.Polynomial.FunctionValueInPoint(x) * (Math.Pow(x, alpha) * Math.Exp(-x));
                    //x += dx;
                    //double fx2 = lg.Polynomial.FunctionValueInPoint(x) * lg2.Polynomial.FunctionValueInPoint(x) * (Math.Pow(x, alpha) * Math.Exp(-x));
                    //integral += 0.5 * dx * (fx1 + fx2);
                    integral += fx1 * dx;
                }
            }

            Orthogonal = integral;
        }
Ejemplo n.º 2
0
 public void TrapeziumRuleSupportsTwoPointIntegration()
 {
     Assert.AreEqual(
         TargetAreaA,
         NewtonCotesTrapeziumRule.IntegrateTwoPoint(TargetFunctionA, StartA, StopA),
         0.4 * TargetAreaA,
         "Direct (1 Partition)");
 }
Ejemplo n.º 3
0
 public void TrapeziumRuleSupportsAdaptiveIntegration(double targetRelativeError)
 {
     Assert.AreEqual(
         TargetAreaA,
         NewtonCotesTrapeziumRule.IntegrateAdaptive(TargetFunctionA, StartA, StopA, targetRelativeError),
         targetRelativeError * TargetAreaA,
         "Adaptive {0}",
         targetRelativeError);
 }
Ejemplo n.º 4
0
 public void TrapeziumRuleSupportsCompositeIntegration(int partitions, double maxRelativeError)
 {
     Assert.AreEqual(
         TargetAreaA,
         NewtonCotesTrapeziumRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions),
         maxRelativeError * TargetAreaA,
         "Composite {0} Partitions",
         partitions);
 }
Ejemplo n.º 5
0
 public void TrapeziumRuleSupportsCompositeIntegration([Values(1, 5, 10, 50, 1000)] int partitions, [Values(3.5e-1, 1e-1, 2e-2, 6e-4, 1.5e-6)] double maxRelativeError)
 {
     Assert.AreEqual(
         TargetAreaA,
         NewtonCotesTrapeziumRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions),
         maxRelativeError * TargetAreaA,
         "Composite {0} Partitions",
         partitions);
 }
Ejemplo n.º 6
0
        public static double trapezoidalRuleEt(Expr expr, string varName, double a, double b, int n)
        {
            var f = expr.Compile(varName);

            double trValue = NewtonCotesTrapeziumRule.IntegrateComposite(f, a, b, n);

            double trueValue = Integrate.OnClosedInterval(f, a, b);

            return(abs(trueValue - trValue));
        }
Ejemplo n.º 7
0
        // This evaluates the SQUARED intergral of (S*Etan)
        public static double TFInt(
            Vector <Double> ETan_Z,
            Vector <Complex> ETan_RMS,
            Vector <Double> TF_Z,
            Vector <Complex> TF_Sr
            )
        {
            int    num_points = 5000;
            double zmin       = Math.Max(TF_Z.Minimum(), ETan_Z.Minimum());
            double zmax       = Math.Min(TF_Z.Maximum(), ETan_Z.Maximum());

            LinearSpline etansinterpR = LinearSpline.Interpolate(
                ETan_Z, ETan_RMS.Real());
            LinearSpline etansinterpI = LinearSpline.Interpolate(
                ETan_Z, ETan_RMS.Imaginary());
            LinearSpline TF_SrInterpR = LinearSpline.Interpolate(
                TF_Z, TF_Sr.Real());
            LinearSpline TF_SrInterpI = LinearSpline.Interpolate(
                TF_Z, TF_Sr.Imaginary());

            /*var ETanGridded = CreateVector.DenseOfEnumerable(
             *  zvals.Select(z => new Complex(
             *     etansinterpR.Interpolate(z), etansinterpI.Interpolate(z)))
             *     );
             * var SrGridded = CreateVector.DenseOfEnumerable(
             *  zvals.Select(z => new Complex(
             * TF_SrInterpR.Interpolate(z), TF_SrInterpI.Interpolate(z)))
             * );*/
            //var Z = SrGridded.PointwiseMultiply(ETanGridded);
            // This is the integrand, SR*Etan
            Func <double, Complex> SrEtan = (z) =>
            {
                var s = new Complex(
                    TF_SrInterpR.Interpolate(z), TF_SrInterpI.Interpolate(z));
                var e = new Complex(
                    etansinterpR.Interpolate(z), etansinterpI.Interpolate(z));
                return(s * e);
            };
            // And to integrate it, the real and imaginary components must be
            // separately computed.
            Complex sum =
                new Complex(
                    NewtonCotesTrapeziumRule.IntegrateComposite(
                        (z => SrEtan(z).Real), zmin, zmax, num_points),
                    NewtonCotesTrapeziumRule.IntegrateComposite(
                        (z => SrEtan(z).Imaginary), zmin, zmax, num_points)
                    );
            // Usually we need the magnitude squared (for the temperature calculations)
            // But, the caller may find the square root, if needed, for example for voltages
            double dT = sum.MagnitudeSquared();

            return(dT);
        }
Ejemplo n.º 8
0
        private double CalculateAverageDopplerShift(
            QQDataFunction function,
            double qgpTemperature,
            double qgpVelocity
            )
        {
            Func <double, double> integrand = cosine => function(
                GetDopplerShiftedTemperature(qgpTemperature, qgpVelocity, cosine));

            return(0.5 * NewtonCotesTrapeziumRule.IntegrateComposite(
                       integrand, -1, 1, NumberAveragingAngles));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Calculates the p.
        /// </summary>
        /// <returns>The p.</returns>
        private double P(int j)
        {
            double v = _VarianceParameters.V0;
            double x = Math.Log(_InitialStockPrice);

            // Integrand
            Func <double, double> integrad = phi =>
                                             ((Complex.Exp(new Complex(0, -phi * Math.Log(_StrikePrice))) *
                                               (Complex.Exp(C(j, phi) + D(j, phi) * v + new Complex(0, phi * x)))) /
                                              new Complex(0, phi)).Real;

            double integral = NewtonCotesTrapeziumRule.IntegrateAdaptive(integrad, 1e-6, 1000, 1e-5);

            return(0.5 + (integral / Math.PI));
        }
Ejemplo n.º 10
0
        private void GenerateOrthogonality2()
        {
            double a  = 0;
            double b  = 10e10;
            double dx = (b - a) / steps;
            double x;
            double integral = 0.0;

            alpha = 0;
            for (int i = 0; i < steps; i++)
            {
                x = Convert.ToDouble(i);
                x = x * dx + a;
                double fx1 = alglib.laguerrecalculate(FirstDegree, x) * alglib.laguerrecalculate(SecondDegree, x) * (Math.Pow(x, alpha) * Math.Exp(-x));
                x += dx;
                double fx2 = alglib.laguerrecalculate(FirstDegree, x) * alglib.laguerrecalculate(SecondDegree, x) * (Math.Pow(x, alpha) * Math.Exp(-x));
                integral += 0.5 * dx * (fx1 + fx2);
            }
            double adaptive = NewtonCotesTrapeziumRule.IntegrateTwoPoint(z => (alglib.laguerrecalculate(degree, z) * alglib.laguerrecalculate(degree, z) * Math.Exp(-z)), 0.0, 10e10);
            //Orthogonal = integral.ToString();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// the formula for calculating
        /// </summary>
        /// <param name="S">Initial Stock Price</param>
        /// <param name="r">Risk Free Rate</param>
        /// <param name="T">Maturity</param>
        /// <param name="K">Strike Price</param>
        /// <returns>the final price of Euorpean option</returns>
        public double CalculateCallOptionPrice(double S, double K, double r, double T)
        {
            double b1 = k - rho * sigma;
            double b2 = k;
            double a  = k * theta;

            if (sigma <= 0 || T <= 0 || K <= 0 || S <= 0)
            {
                throw new System.ArgumentException("Need sigma > 0, T > 0, K > 0 and S > 0.");
            }
            Complex d1(double phi)
            {
                return(Complex.Sqrt(Complex.Pow(rho * sigma * phi * i - b1, 2.0) - sigma * sigma * (2.0 * u1 * phi * i - phi * phi)));
            }

            Complex d2(double phi)
            {
                return(Complex.Sqrt(Complex.Pow(rho * sigma * phi * i - b2, 2.0) - sigma * sigma * (2.0 * u2 * phi * i - phi * phi)));
            }

            Complex g1(double phi)
            {
                return((b1 - rho * sigma * phi * i - d1(phi)) / (b1 - rho * sigma * phi * i + d1(phi)));
            }

            Complex g2(double phi)
            {
                return((b2 - rho * sigma * phi * i - d2(phi)) / (b2 - rho * sigma * phi * i + d2(phi)));
            }

            Complex C1(double tau, double phi)
            {
                return(r * phi * i * tau + (a / Math.Pow(sigma, 2.0)) * ((b1 - rho * sigma * phi * i - d1(phi)) * tau - 2.0 * Complex.Log((1.0 - g1(phi) * Complex.Exp(-tau * d1(phi))) / (1.0 - g1(phi)))));
            }

            Complex C2(double tau, double phi)
            {
                return(r * phi * i * tau + (a / Math.Pow(sigma, 2.0)) * ((b2 - rho * sigma * phi * i - d2(phi)) * tau - 2.0 * Complex.Log((1.0 - g2(phi) * Complex.Exp(-tau * d2(phi))) / (1.0 - g2(phi)))));
            }

            Complex D1(double tau, double phi)
            {
                return(((b1 - rho * sigma * phi * i - d1(phi)) / Math.Pow(sigma, 2.0)) * ((1.0 - Complex.Exp(-tau * d1(phi))) / (1.0 - g1(phi) * Complex.Exp(-tau * d1(phi)))));
            }

            Complex D2(double tau, double phi)
            {
                return(((b2 - rho * sigma * phi * i - d2(phi)) / Math.Pow(sigma, 2.0)) * ((1.0 - Complex.Exp(-tau * d2(phi))) / (1.0 - g2(phi) * Complex.Exp(-tau * d2(phi)))));
            }

            Complex Phi1(double x, double phi)
            {
                return(Complex.Exp(C1(T, phi) + D1(T, phi) * nv + i * phi * x));
            }

            Complex Phi2(double x, double phi)
            {
                return(Complex.Exp(C2(T, phi) + D2(T, phi) * nv + i * phi * x));
            }

            Complex integrand1(double x, double phi)
            {
                return((Complex.Exp(-i * phi * Math.Log(K)) * Phi1(x, phi)) / (i * phi));
            }

            Complex integrand2(double x, double phi)
            {
                return((Complex.Exp(-i * phi * Math.Log(K)) * Phi2(x, phi)) / (i * phi));
            }

            double P1(double x)
            {
                Func <double, double> IntegrateFormula
                    = (phi) => {
                    Complex aj = integrand1(x, phi);
                    return(aj.Real);
                    };
                CompositeIntegrator integrator = new CompositeIntegrator(4);
                /*double integral = integrator.Integrate(IntegrateFormula, 0.0001, 10000, 10000);*/
                double integral = NewtonCotesTrapeziumRule.IntegrateComposite(IntegrateFormula, 0.0001, 10000, 10000);

                /*double integral = DoubleExponentialTransformation.Integrate(IntegrateFormula, 0.01, 1e3, 1e-5);*/
                return(0.50 + (1 / Math.PI) * integral);
            }

            double P2(double x)
            {
                Func <double, double> IntegrateFormula
                    = (phi) => {
                    Complex al = integrand2(x, phi);
                    return(al.Real);
                    };
                CompositeIntegrator integrator = new CompositeIntegrator(4);
                /*double integral = integrator.Integrate(IntegrateFormula, 0.0001, 10000, 10000);*/
                double integral2 = NewtonCotesTrapeziumRule.IntegrateComposite(IntegrateFormula, 0.0001, 10000, 10000);

                /*double integral = DoubleExponentialTransformation.Integrate(IntegrateFormula, 0.01, 1e3, 1e-5);*/
                return(0.50 + (1 / Math.PI) * integral2);
            }

            return(S * P1(Math.Log(S)) - Math.Exp(-r * T) * K * P2(Math.Log(S)));
        }
Ejemplo n.º 12
0
 public double Integrate(ITestFunction testFunction, double tolerance)
 {
     return(NewtonCotesTrapeziumRule.IntegrateAdaptive(testFunction.Eval,
                                                       testFunction.A, testFunction.B, tolerance));
 }
Ejemplo n.º 13
0
        public static double trapezoidalRuleError(Expr expr, string varName, double a, double b, double Et)
        {
            var f = expr.Compile(varName);

            return(NewtonCotesTrapeziumRule.IntegrateAdaptive(f, a, b, Et));
        }
Ejemplo n.º 14
0
        public static double trapezoidalRule(Expr expr, string varName, double a, double b, int n)
        {
            var f = expr.Compile(varName);

            return(NewtonCotesTrapeziumRule.IntegrateComposite(f, a, b, n));
        }