C 言語で標準偏差を計算する

CBeginner
オンラインで実践に進む

はじめに

この実験(Lab)では、C 言語プログラミングでデータセットの標準偏差を計算する方法を学びます。この実験では、データセットの平均を計算し、平均からの二乗偏差を合計して分散を計算し、平方根を取って標準偏差を求めるという 3 つの主要なステップを扱います。この実験の終わりには、これらの基本的な統計的概念と、C 言語でそれらを実装する方法について、確かな理解が得られるでしょう。

この実験では、プロセスをガイドするためのステップバイステップの指示とサンプルコードを提供します。まず、与えられたデータセットの平均を計算する C プログラムを作成し、次に平均からの二乗偏差を合計して分散を計算するようにプログラムを拡張します。最後に、分散の平方根を取って標準偏差を求め、結果を出力します。

データセットの平均を計算する

このステップでは、C 言語プログラミングでデータセットの平均を計算する方法を学びます。平均は、数値の集合の平均値を表す基本的な統計的尺度です。

まず、データセットの平均を計算する C プログラムを作成しましょう。nano を使用して新しいファイルを開きます。

cd ~/project
nano mean_calculation.c

次に、以下のコードを入力します。

#include <stdio.h>

#define MAX_SIZE 100

float calculateMean(int arr[], int size) {
    float sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    return sum / size;
}

int main() {
    int dataset[MAX_SIZE];
    int size;

    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &size);

    printf("Enter %d integers:\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &dataset[i]);
    }

    float mean = calculateMean(dataset, size);
    printf("Mean of the dataset: %.2f\n", mean);

    return 0;
}

プログラムをコンパイルします。

gcc mean_calculation.c -o mean_calculation

プログラムを実行し、いくつかのサンプルデータを入力します。

./mean_calculation

出力例:

Enter the number of elements (max 100): 5
Enter 5 integers:
10
20
30
40
50
Mean of the dataset: 30.00

コードを分解してみましょう。

  1. 配列とそのサイズをパラメータとして受け取る calculateMean 関数を定義します。
  2. この関数は、配列内のすべての要素の合計を計算します。
  3. 平均は、合計を要素の総数で割って計算されます。
  4. main 関数では、ユーザーにデータセットの入力を促します。
  5. calculateMean を呼び出し、結果を小数点以下 2 桁で表示します。

二乗偏差の合計と分散の計算

このステップでは、前のプログラムを拡張して、平均からの二乗偏差を合計することにより分散を計算します。分散は、データセット内の数値がどれだけ広がっているかを測定します。

前のファイルを修正するために開きます。

cd ~/project
nano mean_calculation.c

分散計算でプログラムを更新します。

#include <stdio.h>
#include <math.h>

#define MAX_SIZE 100

float calculateMean(int arr[], int size) {
    float sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    return sum / size;
}

float calculateVariance(int arr[], int size, float mean) {
    float sumSquaredDeviations = 0;
    for (int i = 0; i < size; i++) {
        float deviation = arr[i] - mean;
        sumSquaredDeviations += deviation * deviation;
    }
    return sumSquaredDeviations / size;
}

int main() {
    int dataset[MAX_SIZE];
    int size;

    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &size);

    printf("Enter %d integers:\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &dataset[i]);
    }

    float mean = calculateMean(dataset, size);
    float variance = calculateVariance(dataset, size, mean);

    printf("Mean of the dataset: %.2f\n", mean);
    printf("Variance of the dataset: %.2f\n", variance);

    return 0;
}

更新されたプログラムをコンパイルします。

gcc mean_calculation.c -o mean_calculation -lm

プログラムを実行し、サンプルデータを入力します。

./mean_calculation

出力例:

Enter the number of elements (max 100): 5
Enter 5 integers:
10
20
30
40
50
Mean of the dataset: 30.00
Variance of the dataset: 200.00

コードの重要なポイント:

  1. 配列、サイズ、および平均を受け取る新しい calculateVariance 関数を追加しました。
  2. この関数は、各要素の平均からの偏差を計算します。
  3. これらの偏差を二乗し、それらを合計します。
  4. 分散は、二乗偏差の合計を要素数で割って計算されます。
  5. 数学ライブラリをリンクするために、コンパイル時に -lm フラグを使用します。

標準偏差の平方根を計算して出力する

この最終ステップでは、分散の平方根を計算することにより、標準偏差の計算を完了します。標準偏差は、統計分析におけるデータの分散の重要な尺度です。

前のファイルを修正するために開きます。

cd ~/project
nano mean_calculation.c

標準偏差の計算でプログラムを更新します。

#include <stdio.h>
#include <math.h>

#define MAX_SIZE 100

float calculateMean(int arr[], int size) {
    float sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    return sum / size;
}

float calculateVariance(int arr[], int size, float mean) {
    float sumSquaredDeviations = 0;
    for (int i = 0; i < size; i++) {
        float deviation = arr[i] - mean;
        sumSquaredDeviations += deviation * deviation;
    }
    return sumSquaredDeviations / size;
}

float calculateStandardDeviation(float variance) {
    return sqrt(variance);
}

int main() {
    int dataset[MAX_SIZE];
    int size;

    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &size);

    printf("Enter %d integers:\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &dataset[i]);
    }

    float mean = calculateMean(dataset, size);
    float variance = calculateVariance(dataset, size, mean);
    float standardDeviation = calculateStandardDeviation(variance);

    printf("Dataset Statistics:\n");
    printf("Mean: %.2f\n", mean);
    printf("Variance: %.2f\n", variance);
    printf("Standard Deviation: %.2f\n", standardDeviation);

    return 0;
}

更新されたプログラムをコンパイルします。

gcc mean_calculation.c -o mean_calculation -lm

プログラムを実行し、サンプルデータを入力します。

./mean_calculation

出力例:

Enter the number of elements (max 100): 5
Enter 5 integers:
10
20
30
40
50
Dataset Statistics:
Mean: 30.00
Variance: 200.00
Standard Deviation: 14.14

コードの重要なポイント:

  1. 新しい calculateStandardDeviation 関数を追加しました。
  2. この関数は、数学ライブラリの sqrt() を使用して標準偏差を計算します。
  3. 標準偏差は、分散の平方根です。
  4. main 関数は、これで 3 つのすべての統計的尺度を出力します。
  5. 引き続き -lm フラグを使用して、数学ライブラリをリンクします。

まとめ

この実験(Lab)では、まず C 言語プログラミングでデータセットの平均を計算する方法を学びました。平均は、数値の集合の平均値を表す基本的な統計的尺度です。次に、平均からの二乗偏差を合計することにより、分散を計算するようにプログラムを拡張しました。分散は、データセット内の数値がどれだけ広がっているかを測定します。最後に、分散の平方根を計算して標準偏差を計算し、結果を出力する方法を学びました。