C#, Numerical Calculation - Calculation Method and Source Program of Cauchy Distribution

Introduction to Cauchy distribution

The Cauchy distribution, also called the Lorentzian distribution or Lorentz distribution, is a continuous distribution describing resonance behavior. It also describes the distribution of horizontal distances at which a line segment tilted at a random angle cuts the x-axis.

Let theta represent the angle that a line, with fixed point of rotation, makes with the vertical axis, as shown above. Then:

The general Cauchy distribution and its cumulative distribution can be written as

 

where b is the half width at half maximum and m is the statistical median. In the illustration about, m=0.

The Cauchy distribution is implemented in the Wolfram Language as CauchyDistribution[m, Gamma/2].

The characteristic function is

Source program for Cauchy distribution calculation

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Cauchy distribution.
    /// </summary>
    public class Cauchydist
    {
        private double mu { get; set; }
        private double sig { get; set; }

        public Cauchydist(double mmu = 0.0, double ssig = 1.0)
        {
            this.mu = mmu;
            this.sig = ssig;
            if (sig <= 0.0)
            {
                throw new Exception("bad sig in Cauchydist");
            }
        }

        public double p(double x)
        {
            return 0.318309886183790671 / (sig * (1.0 + Globals.SQR((x - mu) / sig)));
        }

        public double cdf(double x)
        {
            return 0.5 + 0.318309886183790671 * Math.Atan2(x - mu, sig);
        }

        public double invcdf(double p)
        {
            if (p <= 0.0 || p >= 1.0)
            {
                throw new Exception("bad p in Cauchydist");
            }
            return mu + sig * Math.Tan(3.14159265358979324 * (p - 0.5));
        }
    }
}
 

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/131670394