{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from scipy.stats import pearsonr, spearmanr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Case 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case 1 pearson coeficient: \u001b[1m1.0.\n" ] } ], "source": [ "x1 = [4, 5, 6, 7]\n", "y1 = [5, 6, 7, 8]\n", "p1 = pearsonr(x1, y1)\n", "print('Test case 1 pearson coeficient: \\033[1m{0:.8}.'.format(p1[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Case 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case 2 pearson coeficient: \u001b[1m1.0.\n" ] } ], "source": [ "x2 = [9, 8, 7, 6]\n", "y2 = [8, 7, 6, 5]\n", "p2 = pearsonr(x2, y2)\n", "print('Test case 2 pearson coeficient: \\033[1m{0:.8}.'.format(p2[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Case 3" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case 3 pearson coeficient: \u001b[1m-0.58913542.\n" ] } ], "source": [ "x3 = [48, 84, 39, 54]\n", "y3 = [84, 39, 54, 77]\n", "p3 = pearsonr(x3, y3)\n", "print('Test case 3 pearson coeficient: \\033[1m{0:.8}.'.format(p3[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases 4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case 4 pearson coeficient: \u001b[1m-0.037601474.\n" ] } ], "source": [ "x4 = [86, 97, 99, 100, 101, 103, 106, 110, 112, 113]\n", "y4 = [0, 20, 28, 27, 50, 29, 7, 17, 6, 12]\n", "p4 = pearsonr(x4, y4)\n", "print('Test case 4 pearson coeficient: \\033[1m{0:.8}.'.format(p4[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases 5" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case 0 - x=[ 93 76 18 38 27 ] y=[ 76 18 38 27 48 ] r= 0.34670807201\n", "Test case 1 - x=[ 76 18 38 27 48 ] y=[ 18 38 27 48 22 ] r= -0.821747728214\n", "Test case 2 - x=[ 18 38 27 48 22 ] y=[ 38 27 48 22 73 ] r= -0.701175297732\n", "Test case 3 - x=[ 38 27 48 22 73 ] y=[ 27 48 22 73 54 ] r= -0.233376493759\n" ] } ], "source": [ "data = [93, 76, 18, 38, 27, 48, 22, 73, 54, 68]\n", "n = len(data)\n", "window = 5\n", "for i in range(n-window-1):\n", " x = data[i:i+window]\n", " y = data[i+1:i+window+1]\n", " p = pearsonr(x, y)\n", " print('Test case ', i, '- x=[', *x, '] y=[', *y, '] r=', p[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Case Wikipedia's [article](https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test case from Wikipedia Article PEARSON coeficient: \u001b[1m-0.037601474.\n", "Test case from Wikipedia Article SPEARMAN coeficient: \u001b[1m-0.17575758.\n" ] } ], "source": [ "x5 = [86, 97, 99, 100, 101, 103, 106, 110, 112, 113]\n", "y5 = [0, 20, 28, 27, 50, 29, 7, 17, 6, 12]\n", "p5 = pearsonr(x5, y5)\n", "s = spearmanr(x5, y5)\n", "print('Test case from Wikipedia Article PEARSON coeficient: \\033[1m{0:.8}.'.format(p5[0]))\n", "print('Test case from Wikipedia Article SPEARMAN coeficient: \\033[1m{0:.8}.'.format(s[0]))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }