Accord.Math.Integration.MonteCarloIntegration.Compute C# (CSharp) Method

Compute() public method

Computes the area of the function under the selected Range. The computed value will be available at this object's Area.
public Compute ( ) : bool
return bool
        public bool Compute()
        {
            double[] sample = new double[NumberOfParameters];

            for (int j = 0; j < Iterations; j++)
            {

                for (int i = 0; i < sample.Length; i++)
                    sample[i] = Random.NextDouble() * Range[i].Length + Range[i].Min;

                double f = Function(sample);

                count++;
                sum += f;
                sum2 += f * f;
            }

            double volume = 1;
            for (int i = 0; i < Range.Length; i++)
                volume *= Range[i].Length;

            double avg = sum / count;
            double avg2 = sum2 / count;

            Area = volume * avg;
            Error = volume * Math.Sqrt((avg2 - avg * avg) / count);

            return true;
        }

Usage Example

        /// <summary>
        ///   Computes the area of the function under the selected <see cref="Range"/>.
        ///   The computed value will be available at this object's <see cref="Area"/>.
        /// </summary>
        ///
        /// <returns>
        ///   True if the integration method succeeds, false otherwise.
        /// </returns>
        ///
        public static double Integrate(Func <double[], double> func, double[] a, double[] b)
        {
            var mc = new MonteCarloIntegration(a.Length, func);

            for (int i = 0; i < a.Length; i++)
            {
                mc.Range[i] = new DoubleRange(a[i], b[i]);
            }
            mc.Compute();
            return(mc.Area);
        }
All Usage Examples Of Accord.Math.Integration.MonteCarloIntegration::Compute