在本教程中,我们将讨论实现均值标准误差的程序。
平均值的标准误是根据总体平均值估算样本平均值的离散度。然后用于估计平均值的近似置信区间。
#include <bits/stdc++.h> using namespace std; //calculating sample mean float calc_mean(float arr[], int n){ float sum = 0; for (int i = 0; i < n; i++) sum = sum + arr[i]; return sum / n; } //calculating standard deviation float calc_deviation(float arr[], int n){ float sum = 0; for (int i = 0; i < n; i++) sum = sum + (arr[i] - calc_mean(arr, n)) * (arr[i] - calc_mean(arr, n)); return sqrt(sum / (n - 1)); } //calculating sample error float calc_error(float arr[], int n){ return calc_deviation(arr, n) / sqrt(n); } int main(){ float arr[] = { 78.53, 79.62, 80.25, 81.05, 83.21, 83.46 }; int n = sizeof(arr) / sizeof(arr[0]); cout << calc_error(arr, n) << endl; return 0; }
输出结果
0.8063