2016年8月14日日曜日

開発環境

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の3章(データを統計量で記述する)、3.9(プログラミングチャレンジ)、問題3-1(よりよい相関関係を求めるプログラム)を取り組んでみる。

問題3-1(よりよい相関関係を求めるプログラム)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt


def find_corr_x_y(x, y):
    if len(x) != len(y):
        raise Exception('相関関係は計算できない')
    n = len(x)
    prod = [xi * yi for xi, yi in zip(x, y)]
    sum_prod_x_y = sum(prod)
    sum_x = sum(x)
    sum_y = sum(y)
    squared_sum_x = sum_x**2
    squared_sum_y = sum_y**2
    x_square = [xi**2 for xi in x]
    x_square_sum = sum(x_square)
    y_square = [yi**2 for yi in y]
    y_square_sum = sum(y_square)

    numerator = n * sum_prod_x_y - sum_x * sum_y
    denominator_term1 = n * x_square_sum - squared_sum_x
    denominator_term2 = n * y_square_sum - squared_sum_y
    denominator = (denominator_term1 * denominator_term2)**0.5
    correlation = numerator / denominator
    return correlation

if __name__ == '__main__':
    import random
    x0 = [x for x in range(10)]
    x1 = [random.randrange(10) for _ in range(10)]
    x2 = [random.randrange(10) for _ in range(11)]
    y0 = [y for y in range(9, -1, -1)]
    y1 = [random.randrange(10) for _ in range(10)]
    y2 = [random.randrange(10) for _ in range(11)]
    for x in [x0, y0, x1, x2]:
        for y in [x0, y0, y1, y2]:
            print('x: {0}\ny: {1}'.format(x, y))
            try:
                correlation = find_corr_x_y(x, y)
                print('correlation: {0}'.format(correlation))
            except Exception as err:
                print(err)
            finally:
                print()

入出力結果(Terminal, IPython)

$ ./sample1.py
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
correlation: 1.0

x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
correlation: -1.0

x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y: [7, 4, 2, 6, 2, 1, 3, 7, 1, 3]
correlation: -0.3323300704608777

x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y: [2, 8, 1, 6, 2, 5, 3, 8, 5, 0, 0]
相関関係は計算できない

x: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
correlation: -1.0

x: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
y: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
correlation: 1.0

x: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
y: [7, 4, 2, 6, 2, 1, 3, 7, 1, 3]
correlation: 0.3323300704608777

x: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
y: [2, 8, 1, 6, 2, 5, 3, 8, 5, 0, 0]
相関関係は計算できない

x: [4, 5, 5, 9, 0, 2, 5, 7, 0, 8]
y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
correlation: 0.005918817869693365

x: [4, 5, 5, 9, 0, 2, 5, 7, 0, 8]
y: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
correlation: -0.005918817869693365

x: [4, 5, 5, 9, 0, 2, 5, 7, 0, 8]
y: [7, 4, 2, 6, 2, 1, 3, 7, 1, 3]
correlation: 0.6182003644709508

x: [4, 5, 5, 9, 0, 2, 5, 7, 0, 8]
y: [2, 8, 1, 6, 2, 5, 3, 8, 5, 0, 0]
相関関係は計算できない

x: [9, 6, 9, 6, 9, 9, 7, 7, 0, 7, 1]
y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
相関関係は計算できない

x: [9, 6, 9, 6, 9, 9, 7, 7, 0, 7, 1]
y: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
相関関係は計算できない

x: [9, 6, 9, 6, 9, 9, 7, 7, 0, 7, 1]
y: [7, 4, 2, 6, 2, 1, 3, 7, 1, 3]
相関関係は計算できない

x: [9, 6, 9, 6, 9, 9, 7, 7, 0, 7, 1]
y: [2, 8, 1, 6, 2, 5, 3, 8, 5, 0, 0]
correlation: -0.0383911827259596

$

0 コメント:

コメントを投稿