Log Normal Distribution

对数正态分布

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Markup;

namespace log_normal
{    
    public partial class logNormal : Form
    {   
        // mean =0.0; sigma =1.0
        int i, j,k; double r,t,s,mean=0,sigma=1.0,miu;
        double[] value;
        // generate(0,1)interval random number
        Random rd = new Random(Guid.NewGuid().GetHashCode());
        //double uniform(double double int*)        
        public logNormal()
        {
            InitializeComponent();
        }

        double gauss(double mean,double sigma)
        {
            for (r = 0, k = 0; k < 12; k++)
            {
                r += rd.NextDouble();
            }                            
            r = r - 6.0;
            r = mean + r * sigma;
            return r;           
        }

        double log_normal(double mean,double sigma)
        {
            t=gauss(mean,sigma);
            t = Math.Exp(t);
            return t;
        }

        private void btCreateData_Click(object sender, EventArgs e)
        {
            double[] value = new double[500];
            mean = 0.0; sigma = 2.0;
            for (int i = 0; i < value.Length; i++)
            {
                value[i] = 0.0;
            }
            
            tbPointData.Clear();
            tbDensityData.Clear();
            foreach (var series in chart_PointDistribute.Series)
            {
                series.Points.Clear();
            }

            foreach (var series in chart_MakeCurve.Series)
            {
                series.Points.Clear();
            }
            for (i = 0; i < 10; i++)
            {
                for (j = 0; j < 5; j++)
                {
                    r=log_normal(mean, sigma);                    
                    value[i * 5 + j] = r;
                    this.chart_PointDistribute.Series[0].Points.AddXY(i * 5 + j, r);
                    tbPointData.AppendText((i * 5 + j).ToString() + ": 	" + r.ToString("f8") + "	
  ");
                }
            }
            //mean
            mean = 0.0;
            for (i = 0; i < 50; i++)
            {
                mean += value[i];
            }
            mean = mean / 50.0;
            tbDensityData.AppendText("mean is " + mean.ToString("f4") + "	
");

            //μ μ=(1/n)*Σlnx
            for (i = 0; i < 50; i++)
            {
                miu += Math.Log(value[i]);                
            }
            miu = miu / 50;
            tbDensityData.AppendText("miu is " + miu.ToString("f4") + "	
");
            //σ σ2=(1/n)*Σ(lnx-μ)2      
            double total = 0;
            for (i = 0; i < 50; i++)
            {
                sigma = 0.0;
                sigma = Math.Log(value[i]) - miu;
                sigma *= sigma;
                total += sigma;
            }
            sigma = total / 50.0;
            sigma = Math.Sqrt(sigma);
            tbDensityData.AppendText("sigam is " + sigma.ToString("f4") + "	
");
            //f(x)=(1/xSqrt(2*PI))*σ*exp(-(lnx-μ)2/2σ2)
            double[] density = new double[200];            
            for (double l = 0; l < 50; l +=0.25){                
                t = Math.Exp(-(Math.Pow(Math.Log(l) - miu, 2.0)/ 2 *sigma*sigma));
                s = 1 / (l * Math.Sqrt(2 * Math.PI) * sigma);
                int w = (int)(l * 4.0);
                density[w] = t*s;
            }
            for(int i = 0; i < 200; i++) {               
                this.chart_MakeCurve.Series[0].Points.AddXY(0.25*i, density[i]);
                tbDensityData.AppendText("X is:" + (0.25*i).ToString("f0")+"	" + "Density is:"+density[i].ToString("f4")+"	
");
            }
        }
    }
}

对数正态分布LogNormalDistribution资源-CSDN文库