计算运行标准差
Calculate the running standard deviation
我正在将方程转换为c++。对于运行标准偏差,这是否正确?
this->runningStandardDeviation = (this->sumOfProcessedSquaredSamples - sumSquaredDividedBySampleCount) / (sampleCount - 1);
完整功能如下:
void BM_Functions::standardDeviationForRunningSamples (float samples [], int sampleCount)
{
// update the running process samples count
this->totalSamplesProcessed += sampleCount;
// get the mean of the samples
double mean = meanForSamples(samples, sampleCount);
// sum the deviations
// sum the squared deviations
for (int i = 0; i < sampleCount; i++)
{
// update the deviation sum of processed samples
double deviation = samples[i] - mean;
this->sumOfProcessedSamples += deviation;
// update the squared deviations sum
double deviationSquared = deviation * deviation;
this->sumOfProcessedSquaredSamples += deviationSquared;
}
// get the sum squared
double sumSquared = this->sumOfProcessedSamples * this->sumOfProcessedSamples;
// get the sum/N
double sumSquaredDividedBySampleCount = sumSquared / this->totalSamplesProcessed;
this->runningStandardDeviation = sqrt((this->sumOfProcessedSquaredSamples - sumSquaredDividedBySampleCount) / (sampleCount - 1));
}
计算运行均值和方差/标准差的一种数值稳定且有效的算法是Welford算法。
一个c++实现是:
std::pair<double,double> getMeanVariance(const std::vector<double>& vec) {
double mean = 0, M2 = 0, variance = 0;
size_t n = vec.size();
for(size_t i = 0; i < n; ++i) {
double delta = vec[i] - mean;
mean += delta / (i + 1);
M2 += delta * (vec[i] - mean);
variance = M2 / (i + 1);
if (i >= 2) {
// <-- You can use the running mean and variance here
}
}
return std::make_pair(mean, variance);
}
注意:要获得SD,只需取sqrt(variance)
您可以检查是否有足够的sampleSount(1将导致除零)
确保变量具有合适的数据类型(浮点数)
相关文章:
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- Windows 将标准的 C 文件结构注入到正在运行的进程中
- 标准库容器最简单、性能差的哈希类是什么?
- 删除 Netbeans 中的标准运行参数
- 如果 2 个数字之间的差值在 1 到 100 之间,则应运行 if 语句
- 对于具有固定运行时确定大小的数组,是否有标准C++类?
- 如何使用向量查找平均值和标准差
- 是在同一体系结构上运行的静态编译的纯标准C 程序可移植
- C++11 标准::运行时有条件?
- 将一个进程标准输出重定向到管道将产生不同的运行结果
- Windows API,C++标准库,VS运行库,我应该使用哪一个
- 如何使动态数组或矢量以与标准阵列相似的速度运行?C++
- 标准::地图未按预期运行
- 以 root 身份运行应用程序时如何获取标准输出
- 计算运行标准差
- c++中的加权方差和加权标准差
- 如何在C++或Java中计算方差、中值和标准差
- 在c++中使用Boost计算样本向量的均值和标准差
- C++使用std::accumulate计算不正确的标准差
- 图像的均值、标准差、方差和偏度