C 言語でピアソンの相関係数を計算する方法

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

はじめに

この実験では、C 言語でピアソンの相関係数を計算する方法を学びます。この実験は、ペア (x,y) データの読み込み、必要な合計の計算、そして相関係数を計算するための式を用いる、という 3 つの主要なステップで構成されています。ユーザーが入力したデータポイントを受け取り、プログラムが相関係数分析を実行し、結果を出力する C プログラムを作成します。

この実験は、データ入力機能の実装から始まり、相関係数式に必要な合計の計算、そして最後に相関係数の出力まで、段階的な手順を示したガイドを提供します。

ペア (x,y) データの読み込み

このステップでは、C 言語でピアソンの相関係数を計算するために、ペア (x,y) データを読み込む方法を学びます。ユーザーがペアになった数値データを入力できるようにし、さらに分析のために保存するプログラムを作成します。

まず、データ入力機能のための C ソースファイルを作成しましょう。

cd ~/project
nano correlation_input.c

次に、ファイルに以下のコードを追加します。

#include <stdio.h>
#define MAX_POINTS 100

int main() {
    double x[MAX_POINTS], y[MAX_POINTS];
    int n, i;

    printf("Enter the number of data points (max %d): ", MAX_POINTS);
    scanf("%d", &n);

    printf("Enter x and y coordinates:\n");
    for (i = 0; i < n; i++) {
        printf("Point %d (x y): ", i + 1);
        scanf("%lf %lf", &x[i], &y[i]);
    }

    printf("\nData Points Entered:\n");
    for (i = 0; i < n; i++) {
        printf("Point %d: (%.2f, %.2f)\n", i + 1, x[i], y[i]);
    }

    return 0;
}

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

gcc -o correlation_input correlation_input.c

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

./correlation_input

出力例:

Enter the number of data points (max 100): 5
Enter x and y coordinates:
Point 1 (x y): 1 2
Point 2 (x y): 2 4
Point 3 (x y): 3 5
Point 4 (x y): 4 4
Point 5 (x y): 5 5

Data Points Entered:
Point 1: (1.00, 2.00)
Point 2: (2.00, 4.00)
Point 3: (3.00, 5.00)
Point 4: (4.00, 4.00)
Point 5: (5.00, 5.00)

コードを詳しく見てみましょう。

  1. データポイントの最大数 (MAX_POINTS) を定義して、メモリオーバーフローを防ぎます。
  2. プログラムは、ユーザーにデータポイントの数を入力するよう促します。
  3. それから、各ポイントの x 座標と y 座標を入力できるようにします。
  4. 最後に、入力されたデータポイントを出力して、入力を確認します。

相関係数の計算と式の適用

このステップでは、ピアソンの相関係数を計算するために必要な合計を計算するために、前のプログラムを拡張します。相関係数式のための計算を含めるために、correlation_input.c ファイルを修正します。

前のファイルを開きます。

cd ~/project
nano correlation_input.c

以下の実装でコードを更新します。

#include <stdio.h>
#include <math.h>
#define MAX_POINTS 100

double calculatePearsonCorrelation(double x[], double y[], int n) {
    double sum_x = 0, sum_y = 0, sum_xy = 0;
    double sum_x_squared = 0, sum_y_squared = 0;

    // 必要な合計を計算
    for (int i = 0; i < n; i++) {
        sum_x += x[i];
        sum_y += y[i];
        sum_xy += x[i] * y[i];
        sum_x_squared += x[i] * x[i];
        sum_y_squared += y[i] * y[i];
    }

    // ピアソンの相関係数式
    double numerator = n * sum_xy - sum_x * sum_y;
    double denominator = sqrt((n * sum_x_squared - sum_x * sum_x) *
                               (n * sum_y_squared - sum_y * sum_y));

    return numerator / denominator;
}

int main() {
    double x[MAX_POINTS], y[MAX_POINTS];
    int n, i;

    printf("Enter the number of data points (max %d): ", MAX_POINTS);
    scanf("%d", &n);

    printf("Enter x and y coordinates:\n");
    for (i = 0; i < n; i++) {
        printf("Point %d (x y): ", i + 1);
        scanf("%lf %lf", &x[i], &y[i]);
    }

    double correlation = calculatePearsonCorrelation(x, y, n);

    printf("\nData Points Entered:\n");
    for (i = 0; i < n; i++) {
        printf("Point %d: (%.2f, %.2f)\n", i + 1, x[i], y[i]);
    }

    printf("\nPearson Correlation Coefficient: %.4f\n", correlation);

    return 0;
}

数学ライブラリを使用してプログラムをコンパイルします。

gcc -o correlation_input correlation_input.c -lm

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

./correlation_input

出力例:

Enter the number of data points (max 100): 5
Enter x and y coordinates:
Point 1 (x y): 1 2
Point 2 (x y): 2 4
Point 3 (x y): 3 5
Point 4 (x y): 4 4
Point 5 (x y): 5 5

Data Points Entered:
Point 1: (1.00, 2.00)
Point 2: (2.00, 4.00)
Point 3: (3.00, 5.00)
Point 4: (4.00, 4.00)
Point 5: (5.00, 5.00)

Pearson Correlation Coefficient: 0.8528

ピアソンの相関係数計算に関する重要な点:

  1. 必要な合計を計算します:x, y, xy, x², y²
  2. ピアソンの相関係数式を適用します
  3. sqrt() 関数 (math.h から) を使用して計算します
  4. -1 から 1 の間の相関係数を返します

相関係数の表示

この最終ステップでは、ピアソンの相関係数の包括的な解釈を提供し、よりユーザーフレンドリーな出力を作成するために、プログラムを強化します。

前のファイルを開きます。

cd ~/project
nano correlation_input.c

以下の実装でコードを更新します。

#include <stdio.h>
#include <math.h>
#define MAX_POINTS 100

double calculatePearsonCorrelation(double x[], double y[], int n) {
    double sum_x = 0, sum_y = 0, sum_xy = 0;
    double sum_x_squared = 0, sum_y_squared = 0;

    for (int i = 0; i < n; i++) {
        sum_x += x[i];
        sum_y += y[i];
        sum_xy += x[i] * y[i];
        sum_x_squared += x[i] * x[i];
        sum_y_squared += y[i] * y[i];
    }

    double numerator = n * sum_xy - sum_x * sum_y;
    double denominator = sqrt((n * sum_x_squared - sum_x * sum_x) *
                               (n * sum_y_squared - sum_y * sum_y));

    return numerator / denominator;
}

void interpretCorrelation(double correlation) {
    printf("\n相関係数解釈:\n");
    printf("相関係数値:%.4f\n", correlation);

    if (correlation > 0.8) {
        printf("強い正の相関\n");
    } else if (correlation > 0.5) {
        printf("中程度の正の相関\n");
    } else if (correlation > 0.3) {
        printf("弱い正の相関\n");
    } else if (correlation > -0.3) {
        printf("線形相関なし\n");
    } else if (correlation > -0.5) {
        printf("弱い負の相関\n");
    } else if (correlation > -0.8) {
        printf("中程度の負の相関\n");
    } else {
        printf("強い負の相関\n");
    }
}

int main() {
    double x[MAX_POINTS], y[MAX_POINTS];
    int n, i;

    printf("ピアソンの相関係数計算機\n");
    printf("------------------------\n");
    printf("データポイント数を入力してください (最大 %d): ", MAX_POINTS);
    scanf("%d", &n);

    printf("x 座標と y 座標を入力してください:\n");
    for (i = 0; i < n; i++) {
        printf("ポイント %d (x y): ", i + 1);
        scanf("%lf %lf", &x[i], &y[i]);
    }

    double correlation = calculatePearsonCorrelation(x, y, n);

    printf("\n入力されたデータポイント:\n");
    for (i = 0; i < n; i++) {
        printf("ポイント %d: (%.2f, %.2f)\n", i + 1, x[i], y[i]);
    }

    interpretCorrelation(correlation);

    return 0;
}

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

gcc -o correlation_calculator correlation_input.c -lm

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

./correlation_calculator

出力例:

ピアソンの相関係数計算機
------------------------
データポイント数を入力してください (最大 100): 5
x座標とy座標を入力してください:
ポイント 1 (x y): 1 2
ポイント 2 (x y): 2 4
ポイント 3 (x y): 3 5
ポイント 4 (x y): 4 4
ポイント 5 (x y): 5 5

入力されたデータポイント:
ポイント 1: (1.00, 2.00)
ポイント 2: (2.00, 4.00)
ポイント 3: (3.00, 5.00)
ポイント 4: (4.00, 4.00)
ポイント 5: (5.00, 5.00)

相関係数解釈:
相関係数値: 0.8528
強い正の相関

主な改善点:

  1. interpretCorrelation() 関数を追加しました
  2. 相関係数の強さの詳細な説明を提供します
  3. 相関をさまざまなレベルに分類します
  4. タイトルと明確な出力でユーザーインターフェースを強化しました

まとめ

この実験では、C 言語でピアソンの相関係数を計算するために、ペアの (x, y) データをどのように読み込むかを学びました。ペアの数字データをユーザーに入力させ、さらに分析するために保存するプログラムを作成しました。また、プログラムを拡張して、式を使用してピアソンの相関係数を計算するために必要な合計を計算しました。

この実験でカバーした主な手順には、ペアの (x, y) データの読み込み、相関係数式に必要な合計の計算、そして最終的な相関係数の出力があります。これらの手順に従うことで、独自の C プログラムでピアソンの相関係数の計算を実装できます。