2017-11-11 10 views
-2

私は多くの整数の標準偏差を見つける機能的なプログラムを持っています。しかし、私は標準偏差を平均なしで得る方法を見つけることです。標準偏差(平均値なし)

Iは式は理解する: STD DEV = SQRT [(B - A^2/N)/ N]

Aは、データ値の合計である

Bは、二乗されたデータ値の合計です。

Nはデータ値の数です。

しかし、私はどのようにコードに書いていますか? これは、偏差のための私の関数であるが、それは平均値を使用しています。

float calculateSD(int arr[]) 
{ 
float sum = 0.0, mean, standardDeviation = 0.0; 

int i; 

for(i = 0; i < SIZE; ++i) 
{ 
    sum += arr[i]; 
} 

mean = sum/SIZE; 

for(i = 0; i < SIZE; ++i) 
    //convert standardDeviation to float 
    standardDeviation += static_cast<float>(pow(arr[i] - mean, 2)); 
//return standard deviation 
return sqrt(standardDeviation/SIZE); 

}  
+0

'mean'せずに標準偏差を取得し、私はなぜ聞いても? – DimChtz

+0

おそらく宿題ですか? – twoleggedhorse

+1

あなたは合計とカウントを持っています。分割する.....さあ。 –

答えて

0
#include <iostream> 
#include <vector> 
#include <numeric> 
#include <math.h> 

double stddev(std::vector<int> const& data) 
{ 
    auto stats = std::make_pair(0.0,0.0); 
    stats = std::accumulate(data.begin(), data.end(), stats, 
          [](std::pair<double,double> stats, double x) { 
           stats.first += x; 
           stats.second += x * x; 
           return stats; 
          }); 
    return sqrt((stats.second - pow(stats.first, 2.0)/data.size())/data.size()); 
} 

int main(int argc, const char *argv[]) 
{ 
    std::cout << stddev({1,1,1,1}) << std::endl; 
    std::cout << stddev({1,2,1,2}) << std::endl; 
    std::cout << stddev({1,10,1,10}) << std::endl; 
} 
関連する問題