我得到了一些被随机扭曲的值,现在我正在寻找一种方法来将失真平均化。
在下面的代码中,我有一个问题的例子,一个度量a[]数组。我得到了一个随机阵列distortion[]。这是由rnd.Nextbytes创建的
使用数组b[],我试图接近数组a[]的值。
在示例中,我使用了10.000个样本,但它不是真的固定的,100.000个样本也可以,最后我宁愿让它运行一段时间,或者直到键被按下。
using System;
namespace test_math
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
byte[] distortion = new byte[8];
int[] b = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] a = { 100, 200, 300, 400, 500, 600, 700, 800 };
for (int count = 0; count < 100000; count++)
{
Console.Write(count+ " ");
rnd.NextBytes(distortion);
for (int i = 0; i < distortion.Length; i++)
{
b[i] = (int)(b[i] * .8 + (a[i] + 127-(int)(distortion[i])) * .2);
Console.Write(b[i].ToString() + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}目前它与行
b[i] = (int)(b[i] * .8 + (a[i] + 127-(int)(distortion[i])) * .2);其中.8和.2是因素(我也用其他数字进行了测试)。但我确实认为这并不理想,尽管这样的数学方法有一种阻尼效应,但它并没有考虑到,在某个时候,新的测量方法对b[]平均值的影响应该较小。
PS --我现在不认为这接近于一个统计值,如果有一个术语,我也很高兴知道它。
发布于 2017-04-06 08:47:29
我还是不太清楚你的目标是什么。然而,从数学上讲,我认为这个想法应该是随着时间的推移来平均样本。从字面上看,这意味着您只需将它们与每次迭代一起添加,然后找出收敛的值,除以样本的总数。
这个版本的代码可以做到这一点(我在输出逻辑方面有点冒昧,这样代码就可以在合理的时间内完成,而不是填充控制台窗口缓冲区):
static void Main(string[] args)
{
Random rnd = new Random();
byte[] distortion = new byte[8];
long[] b = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] a = { 100, 200, 300, 400, 500, 600, 700, 800 };
for (int count = 1; count <= 100000; count++)
{
bool display = count % 100 == 0;
if (display)
{
Console.Write(count + " ");
}
rnd.NextBytes(distortion);
for (int i = 0; i < distortion.Length; i++)
{
int distortedValue = a[i] + 127 - (int)(distortion[i]);
b[i] += distortedValue;
if (display)
{
Console.Write(((int)((double)b[i] / count + 0.5)).ToString() + " ");
}
}
if (display)
{
Console.WriteLine();
}
}
Console.ReadLine();
}如果有足够的样本,这将最终收敛于原始值。如果你真的想用加权和的变化,我想你可以。这将涉及通过将b[]数组值乘以count - 1,将当前失真的值相加,然后再将其除以count,然后再将其存储回b[]数组,从而重新构造以前的和。
https://stackoverflow.com/questions/43249417
复制相似问题