関数calculate_weighted_average マニュアル

(The documentation of function calculate_weighted_average)

Last Update: 2025/8/7


◆機能・用途(Purpose)

実数値配列の要素\(d_1,\cdots,d_N\)の重み付き平均 \[\begin{equation} \bar{d}=\frac{\sum_{i=1}^Nw_id_i}{\sum_{i=1}^Nw_i} \label{eq.average} \end{equation}\] を計算する。ここで\(w_i\)は\(i\)番目のデータの重みを表す。
Calculate the weighted average (eq. \ref{eq.average}) of the components of an array of real numbers \(d_1,\cdots,d_N\), where \(w_i\) is the weight of \(i\)th data.


◆形式(Format)

#include <statistics.h>
inline double calculate_weighted_average
(const int N,const double ∗d,const double ∗w)


◆引数(Arguments)

N データサンプル数\(N\)。
The number of data samples \(N\).
d 実数値\(d_1,\cdots,d_N\)を並べた配列。
An array composed of the real numbers \(d_1,\cdots,d_N\).
w 各データの重み\(w_1,\cdots,w_N\)を並べた配列。
An array composed of the weights \(w_1,\cdots,w_N\) of individual data.


◆戻り値(Return value)

(\ref{eq.average})式で計算した重み付き平均。
The weighted average calculated with eq. (\ref{eq.average}).


◆使用例(Example)

const int N=5;
const double d[]={1.2,3.4,5.6,7.8,9.0};
const double w[]={1.0,1.0,2.0,0.5,1.0};
double a=calculate_weighted_average(N,d,w);


◆検証(Validation)

上の「使用例」の計算をこの関数を用いて行い、 正しい結果(5.218182)が得られることを確認した。
A calculation of the “Example” above using this function yielded a correct result (5.218182).