C#, Lesson 17 of "Xiaobai Learning Program": Random number (Random) fourth, the calculation method and code of moving average (Moving Average)

1 text format

/// <summary>
/// Lesson 17 of "Xiaobai Learning Program": Random Number (Random) Fourth, the calculation method and code of the moving average
/// Continue to learn data statistics, the calculation method of the moving average
/// The moving average is the average value of the value within a certain step size, which is used to reflect the periodic value change.
/// Stock software, financial software and some foolish predictions are all this technology.
/// Of course, the moving average can also be used for many positive things such as robots and AI.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button17_Click(object sender, EventArgs e)
{     // generate some random numbers     int n = 60;     double[] temp = new double[n];     double asum = 0.0;     for (int i = 0; i < n; i++)     {         temp[i] = global_rnd.NextDouble() * 10.0 + 25.0;         asum += temp[i];     }     // Calculate the average (overall average)










    double aavg = asum / n;

    // array
    double[] ma = new double[n];
    // step size
    int step = 5;
    for (int i = 0; i < (n - step); i++)
    {         double sum = 0.0 ;         for (int j = 0; j < step; j++)         {             sum += temp[i + j];         }         double avg = sum / step;         ma[i] = avg;     }







    StringBuilder sb = new StringBuilder();
    sb.AppendLine("<style>td { padding:0px;text-align:center;text-size:0px; } </style>");

    // 显示随机数
    sb.AppendLine("<table width=420 border=0 style='border-collapse:collapse;'>");
    sb.AppendLine("<tr>");
    for (int i = 0; i < n - step; i++)
    {
        sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (temp[i] * 3) + "px;border:solid 1px #FF0000;background-color:rgb(255,255,0);'></div></td>");
    }
    sb.AppendLine("</tr>");
    sb.AppendLine("</table>");
    sb.AppendLine("<br>");
    sb.AppendLine("<br>");

    // Display the moving average
    sb.AppendLine("<table width=420 border=0 style='border-collapse:collapse;'>");
    sb.AppendLine("<tr>");
    // The first one from the left is the average (overall average)
    sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (aavg * 3) + "px;border: solid 2px #0000FF;background-color:rgb(255,0,255);'></div></td>");
    for (int i = 0; i < n - step; i++)
    {         sb.AppendLine(" <td style='vertical-align:bottom;'><div style='width:7px;height:" + (ma[i] * 3) + "px;border:solid 1px #FF0000;background-color:rgb (255,255,0);'></div></td>");     }     // The first one on the right is also the average (overall average)



    sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (aavg * 3) + "px;border:solid 2px #0000FF;background-color:rgb(255,0,255);'></div></td>");
    sb.AppendLine("</tr>");
    sb.AppendLine("</table>");

    webBrowser1.DocumentText = sb.ToString();

}
 

2 code format

/// <summary>
/// 《小白学程序》第十七课:随机数(Random)第四,移动平均值的计算方法与代码
/// 继续学习数据统计,移动平均值的计算方法
/// 移动平均值就是一定步长内数值的平均值,用于反映阶段性的数值变化。
/// 股票软件,金融软件及一些忽悠人的预测都是这个技术。
/// 当然移动平均值也可以用于机器人、AI等等很多正能量的事情。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button17_Click(object sender, EventArgs e)
{
    // 生成一些随机数
    int n = 60;
    double[] temp = new double[n];
    double asum = 0.0;
    for (int i = 0; i < n; i++)
    {
        temp[i] = global_rnd.NextDouble() * 10.0 + 25.0;
        asum += temp[i];
    }
    // 计算平均值(总体平均值)
    double aavg = asum / n;

    // 保存移动平均值的数组
    double[] ma = new double[n];
    // 步长
    int step = 5;
    for (int i = 0; i < (n - step); i++)
    {
        double sum = 0.0;
        for (int j = 0; j < step; j++)
        {
            sum += temp[i + j];
        }
        double avg = sum / step;
        ma[i] = avg;
    }

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("<style>td { padding:0px;text-align:center;text-size:0px; } </style>");

    // 显示随机数
    sb.AppendLine("<table width=420 border=0 style='border-collapse:collapse;'>");
    sb.AppendLine("<tr>");
    for (int i = 0; i < n - step; i++)
    {
        sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (temp[i] * 3) + "px;border:solid 1px #FF0000;background-color:rgb(255,255,0);'></div></td>");
    }
    sb.AppendLine("</tr>");
    sb.AppendLine("</table>");
    sb.AppendLine("<br>");
    sb.AppendLine("<br>");

    // 显示移动平均值
    sb.AppendLine("<table width=420 border=0 style='border-collapse:collapse;'>");
    sb.AppendLine("<tr>");
    // 左端第一个为平均值(总体平均值)
    sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (aavg * 3) + "px;border:solid 2px #0000FF;background-color:rgb(255,0,255);'></div></td>");
    for (int i = 0; i < n - step; i++)
    {
        sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (ma[i] * 3) + "px;border:solid 1px #FF0000;background-color:rgb(255,255,0);'></div></td>");
    }
    // 右端第一个也是平均值(总体平均值)
    sb.AppendLine("<td style='vertical-align:bottom;'><div style='width:7px;height:" + (aavg * 3) + "px;border:solid 2px #0000FF;background-color:rgb(255,0,255);'></div></td>");
    sb.AppendLine("</tr>");
    sb.AppendLine("</table>");

    webBrowser1.DocumentText = sb.ToString();

}

The first line of the picture below is a number of random numbers;

The second is the moving average of the random numbers; the blue bar is the overall average.

Guess you like

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