C#, Lesson 18 of "Xiaobai Learning Program": Random Number (Random) Fifth, the calculation method and code of variance and standard deviation (standard deviation)

1 text format


/// <summary>
/// Lesson 18 of "Xiaobai Programming": Random Number (Random) Fifth, the calculation method and code of variance and standard deviation (standard deviation)
/// Variance = SUM((Xi - X)^2 ) / n i=0...n-1 X = Average of X[i]
/// The variance is the sum of the squares (each value minus the average value), and then divided by the number.
/// The square of the variance = the standard deviation, otherwise, just take the square root.
/// This lesson is to verify the previous normal distribution function.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button18_Click(object sender, EventArgs e)
{     // Number of random numbers     int m = 50000;     // Range of random numbers (0---1023)     int n = 1024;     int[] num = new int[m];     int i = 0;     while (i < m)     {         // Generate random numbers from a normal distribution         // mean = 0.











        int a = (int)(Rand(0.5, 0.1) * n);

        if (a < 0) continue;
        if (a >= n) continue;
        num[i++] = a;
    }

    // Calculate the average
    double sum = 0.0;
    for (int j = 0; j < m; j++)
    {         sum += num[j];     }     double avg = sum / (double)m;


    // Calculate the variance
    double delta = 0.0;
    for (int j = 0; j < m; j++)
    {         // Original way of writing         //delta = delta + (num[j] - avg) * (num[j] - avg) ;         // Another way to write         delta += Math.Pow((num[j] - avg), 2);     }     // Variance     double variance_1 = delta / (double)m;     // Standard deviation     double standard_variance_1 = Math.Sqrt (delta / (double)(m)) / (double)n;








    #region For more serious algorithms, the data should be normalized first,
    // normalize the data to (0 --- 1.0) and then calculate the variance and so on.
    double[] xnum = new double[m];
    for (int j = 0; j < m; j++)
    {         xnum[j] = num[j] / (double)n;     }     // mean also needs normalization     avg /= n;     // calculate variance     delta = 0.0;     for (int j = 0; j < m; j++)     {         delta += Math.Pow((xnum[j] - avg), 2.0);     }     double standard_variance_2 = Math.Sqrt (delta / (double)(m));     #endregion











    StringBuilder sb = new StringBuilder();
    sb.AppendLine("average=" + avg + "<br>");
    sb.AppendLine("variance=" + variance_1 + "<br>");
    sb.AppendLine(" Standard variance=" + standard_variance_1 + "<br>");
    sb.AppendLine("Standard variance=" + standard_variance_2 + " (calculated after normalizing data)<br>");
    webBrowser1.DocumentText = sb.ToString();
}
 

2 code format


/// <summary>
/// 《小白学程序》第十八课:随机数(Random)第五,方差及标准方差(标准差)的计算方法与代码
/// 方差 = SUM((Xi - X)^2 ) / n  i=0...n-1 X = Average of X[i]
/// 方差是 (各数值减去平均值)的平方 之和,再除以个数。
/// 方差的平方 = 标准差,反之,开平方即可。
/// 本课是为了验证前面的正态分布函数。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button18_Click(object sender, EventArgs e)
{
    // 随机数个数
    int m = 50000;
    // 随机数的范围(0---1023)
    int n = 1024;
    int[] num = new int[m];
    int i = 0;
    while (i < m)
    {
        // 按正太分布生成随机数
        // 平均值 = 0.5 * n
        // 标准差 = 0.1 * n
        int a = (int)(Rand(0.5, 0.1) * n);

        if (a < 0) continue;
        if (a >= n) continue;
        num[i++] = a;
    }

    // 计算平均值
    double sum = 0.0;
    for (int j = 0; j < m; j++)
    {
        sum += num[j];
    }
    double avg = sum / (double)m;

    // 计算方差
    double delta = 0.0;
    for (int j = 0; j < m; j++)
    {
        // 原始写法
        //delta = delta + (num[j] - avg) * (num[j] - avg);
        // 另一种写法
        delta += Math.Pow((num[j] - avg), 2);
    }
    // 方差
    double variance_1 = delta / (double)m;
    // 标准差
    double standard_variance_1 = Math.Sqrt(delta / (double)(m)) / (double)n;

    #region 更严肃的算法,应该先进行数据规范化,
    // 将数据规整到(0 --- 1.0)再计算方差等等。
    double[] xnum = new double[m];
    for (int j = 0; j < m; j++)
    {
        xnum[j] = num[j] / (double)n;
    }
    // 均值也需要规范化
    avg /= n;
    // 计算方差
    delta = 0.0;
    for (int j = 0; j < m; j++)
    {
        delta += Math.Pow((xnum[j] - avg), 2.0);
    }
    double standard_variance_2 = Math.Sqrt(delta / (double)(m));
    #endregion

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("平均值=" + avg + "<br>");
    sb.AppendLine("方差= " + variance_1 + "<br>");
    sb.AppendLine("标准方差=" + standard_variance_1 + "<br>");
    sb.AppendLine("标准方差=" + standard_variance_2 + " (规范数据后计算)<br>");
    webBrowser1.DocumentText = sb.ToString();
}

3 Calculation results

Guess you like

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