Skip to content

Instantly share code, notes, and snippets.

@viniciusmss
Created December 3, 2019 17:21
Show Gist options
  • Select an option

  • Save viniciusmss/5bb1abc28db140ee68f9de0b0cc1d3bb to your computer and use it in GitHub Desktop.

Select an option

Save viniciusmss/5bb1abc28db140ee68f9de0b0cc1d3bb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# Load package\n",
"import numpy as np\n",
"\n",
"# Define A\n",
"A = np.array([[5,1,20,0,5],\n",
" [1,2,1,0,0],\n",
" [5,1,5,0,5],\n",
" [0,0,5,10,10],\n",
" [10,0,0,15,30]], dtype=np.float32)\n",
"\n",
"# Normalize A\n",
"for i in range(A.shape[1]):\n",
" A[:, i] = A[:, i] / A[:, i].sum() "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.23809524 0.25 0.6451613 0. 0.1 ]\n",
" [0.04761905 0.5 0.03225806 0. 0. ]\n",
" [0.23809524 0.25 0.16129032 0. 0.1 ]\n",
" [0. 0. 0.16129032 0.4 0.2 ]\n",
" [0.47619048 0. 0. 0.6 0.6 ]]\n"
]
}
],
"source": [
"print(A)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of eigenvalues:\n",
"[ 1. -0.13700466 0.05162274 0.3724222 0.6123453 ]\n",
"\n",
"List of eigenvectors (shown in columns):\n",
"[[-0.29666978 -0.62488663 -0.36838657 0.7118548 -0.49538493]\n",
" [-0.04115295 0.03421432 0.04167876 -0.34398633 -0.28943142]\n",
" [-0.19992964 0.24681665 -0.03551326 0.30960253 -0.2767215 ]\n",
" [-0.34294128 -0.3221922 -0.44963944 -0.496214 0.4066375 ]\n",
" [-0.86759025 0.6660479 0.81186044 -0.18125694 0.65490025]]\n",
"\n",
"Normalized eigenvector with eigenvalue = 1:\n",
"[0.169692 0.02353906 0.11435765 0.19615881 0.4962525 ]\n"
]
}
],
"source": [
"# Get eigenvalues and eigenvectors\n",
"from numpy import linalg as LA\n",
"values, vectors = LA.eig(A)\n",
"print(\"List of eigenvalues:\")\n",
"print(values)\n",
"print(\"\\nList of eigenvectors (shown in columns):\")\n",
"print(vectors)\n",
"\n",
"# Normalize eigenvector that corresponds to lambda = 1\n",
"print(\"\\nNormalized eigenvector with eigenvalue = 1:\")\n",
"print(vectors[:, 0] / vectors[:, 0].sum())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.20034178 0.03621352 0.10356759 0.22225807 0.43761906]\n",
" [0.16733351 0.03098773 0.11722016 0.19313149 0.49132714]\n",
" [0.17234687 0.02724342 0.11562744 0.1944245 0.49035782]\n",
" [0.17147995 0.02555862 0.11553119 0.19449095 0.49293935]\n",
" [0.1710484 0.02467184 0.11514621 0.19501832 0.49411532]\n",
" [0.17059318 0.02419547 0.11487727 0.19540236 0.49493182]\n",
" [0.17027384 0.02392694 0.11468807 0.1956759 0.49543537]\n",
" [0.17005896 0.02377136 0.11456474 0.19585551 0.49574956]\n",
" [0.16992076 0.02367936 0.11448621 0.1959703 0.49594352]\n",
" [0.16983359 0.02362425 0.11443703 0.19604234 0.49606296]]\n"
]
}
],
"source": [
"# Define column vector with distribution\n",
"v1 = np.array([[0.1, 0.05, 0.2, 0.3, 0.35]]).T\n",
"v2 = np.array([[0, 1, 0, 0, 0]]).T\n",
"v3 = np.array([[0.2, 0.2, 0.2, 0.2, 0.2]]).T\n",
"show_convergence(v3)\n",
"\n",
"def show_convergence(v, niter = 10) :\n",
" '''Receive distribuition and save every step for niter (default : 10) steps.'''\n",
" steps = []\n",
" for i in range(niter): # For 10 steps\n",
" v = A@v # Move one step forward\n",
" steps.append(v.T[0]) # Save the state\n",
" print(np.array(steps)) # Print after done\n",
" \n",
"show_convergence(v1)\n",
"show_convergence(v2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.25 0.5 0.25 0. 0. ]\n",
" [0.34581413 0.26996928 0.22484639 0.04032258 0.11904762]\n",
" [0.30679596 0.15870509 0.19799932 0.0762041 0.26029552]\n",
" [0.26649398 0.10034895 0.17068786 0.11447612 0.34799309]\n",
" [0.23345869 0.06837072 0.15086779 0.14291937 0.40438343]\n",
" [0.21045048 0.05016915 0.13744994 0.16237795 0.4395525 ]\n",
" [0.19528217 0.03953989 0.12877414 0.17503103 0.4613728 ]\n",
" [0.1855981 0.0332231 0.12328803 0.183057 0.47483382]\n",
" [0.17951985 0.02942659 0.11986435 0.18807473 0.48311455]\n",
" [0.17574276 0.02712845 0.11774388 0.19118576 0.48819923]]\n"
]
}
],
"source": [
"v2 = np.array([[0, 1, 0, 0, 0]]).T\n",
"show_convergence(v2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.2466513 0.11597542 0.14987711 0.15225807 0.33523811]\n",
" [0.21793908 0.07456776 0.14541789 0.15212458 0.40995072]\n",
" [0.20534526 0.05235283 0.13498177 0.16629447 0.4410257 ]\n",
" [0.19316751 0.04030901 0.12785376 0.17649418 0.46217558]\n",
" [0.18477337 0.03347727 0.12290865 0.18365436 0.4751864 ]\n",
" [0.17917752 0.02950216 0.11970559 0.188323 0.4832918 ]\n",
" [0.17559545 0.02714482 0.11767339 0.19129492 0.48829152]\n",
" [0.17334211 0.02573001 0.11640338 0.19315585 0.49136876]\n",
" [0.17194026 0.02487434 0.11561605 0.19431083 0.49325865]\n",
" [0.1710736 0.02435435 0.11513036 0.19502381 0.49441802]]\n"
]
}
],
"source": [
"v3 = np.array([[0.2, 0.2, 0.2, 0.2, 0.2]]).T\n",
"show_convergence(v3)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (Anaconda 5)",
"env": {
"LD_LIBRARY_PATH": "/ext/anaconda5/lib",
"PROJ_LIB": "/ext/anaconda-2019.03/share/proj",
"PYTHONHOME": "/ext/anaconda5/lib/python3.5",
"PYTHONPATH": "/ext/anaconda5/lib/python3.5:/ext/anaconda5/lib/python3.5/site-packages"
},
"language": "python",
"metadata": {
"cocalc": {
"description": "Python/R distribution for data science",
"priority": -1,
"url": "https://www.anaconda.com/distribution/"
}
},
"name": "anaconda5"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# Load package\n",
"import numpy as np\n",
"\n",
"# Define A\n",
"A = np.array([[5,1,20,0,5],\n",
" [1,2,1,0,0],\n",
" [5,1,5,0,5],\n",
" [0,0,5,10,10],\n",
" [10,0,0,15,30]], dtype=np.float32)\n",
"\n",
"# Normalize A\n",
"for i in range(A.shape[1]):\n",
" A[:, i] = A[:, i] / A[:, i].sum() "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.23809524 0.25 0.6451613 0. 0.1 ]\n",
" [0.04761905 0.5 0.03225806 0. 0. ]\n",
" [0.23809524 0.25 0.16129032 0. 0.1 ]\n",
" [0. 0. 0.16129032 0.4 0.2 ]\n",
" [0.47619048 0. 0. 0.6 0.6 ]]\n"
]
}
],
"source": [
"print(A)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of eigenvalues:\n",
"[ 1. -0.13700466 0.05162274 0.3724222 0.6123453 ]\n",
"\n",
"List of eigenvectors (shown in columns):\n",
"[[-0.29666978 -0.62488663 -0.36838657 0.7118548 -0.49538493]\n",
" [-0.04115295 0.03421432 0.04167876 -0.34398633 -0.28943142]\n",
" [-0.19992964 0.24681665 -0.03551326 0.30960253 -0.2767215 ]\n",
" [-0.34294128 -0.3221922 -0.44963944 -0.496214 0.4066375 ]\n",
" [-0.86759025 0.6660479 0.81186044 -0.18125694 0.65490025]]\n",
"\n",
"Normalized eigenvector with eigenvalue = 1:\n",
"[0.169692 0.02353906 0.11435765 0.19615881 0.4962525 ]\n"
]
}
],
"source": [
"# Get eigenvalues and eigenvectors\n",
"from numpy import linalg as LA\n",
"values, vectors = LA.eig(A)\n",
"print(\"List of eigenvalues:\")\n",
"print(values)\n",
"print(\"\\nList of eigenvectors (shown in columns):\")\n",
"print(vectors)\n",
"\n",
"# Normalize eigenvector that corresponds to lambda = 1\n",
"print(\"\\nNormalized eigenvector with eigenvalue = 1:\")\n",
"print(vectors[:, 0] / vectors[:, 0].sum())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.20034178 0.03621352 0.10356759 0.22225807 0.43761906]\n",
" [0.16733351 0.03098773 0.11722016 0.19313149 0.49132714]\n",
" [0.17234687 0.02724342 0.11562744 0.1944245 0.49035782]\n",
" [0.17147995 0.02555862 0.11553119 0.19449095 0.49293935]\n",
" [0.1710484 0.02467184 0.11514621 0.19501832 0.49411532]\n",
" [0.17059318 0.02419547 0.11487727 0.19540236 0.49493182]\n",
" [0.17027384 0.02392694 0.11468807 0.1956759 0.49543537]\n",
" [0.17005896 0.02377136 0.11456474 0.19585551 0.49574956]\n",
" [0.16992076 0.02367936 0.11448621 0.1959703 0.49594352]\n",
" [0.16983359 0.02362425 0.11443703 0.19604234 0.49606296]]\n"
]
}
],
"source": [
"# Define column vector with distribution\n",
"v1 = np.array([[0.1, 0.05, 0.2, 0.3, 0.35]]).T\n",
"v2 = np.array([[0, 1, 0, 0, 0]]).T\n",
"v3 = np.array([[0.2, 0.2, 0.2, 0.2, 0.2]]).T\n",
"show_convergence(v3)\n",
"\n",
"def show_convergence(v, niter = 10) :\n",
" '''Receive distribuition and save every step for niter (default : 10) steps.'''\n",
" steps = []\n",
" for i in range(niter): # For 10 steps\n",
" v = A@v # Move one step forward\n",
" steps.append(v.T[0]) # Save the state\n",
" print(np.array(steps)) # Print after done\n",
" \n",
"show_convergence(v1)\n",
"show_convergence(v2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.25 0.5 0.25 0. 0. ]\n",
" [0.34581413 0.26996928 0.22484639 0.04032258 0.11904762]\n",
" [0.30679596 0.15870509 0.19799932 0.0762041 0.26029552]\n",
" [0.26649398 0.10034895 0.17068786 0.11447612 0.34799309]\n",
" [0.23345869 0.06837072 0.15086779 0.14291937 0.40438343]\n",
" [0.21045048 0.05016915 0.13744994 0.16237795 0.4395525 ]\n",
" [0.19528217 0.03953989 0.12877414 0.17503103 0.4613728 ]\n",
" [0.1855981 0.0332231 0.12328803 0.183057 0.47483382]\n",
" [0.17951985 0.02942659 0.11986435 0.18807473 0.48311455]\n",
" [0.17574276 0.02712845 0.11774388 0.19118576 0.48819923]]\n"
]
}
],
"source": [
"v2 = np.array([[0, 1, 0, 0, 0]]).T\n",
"show_convergence(v2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### Initial Distribution \\#3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.2466513 0.11597542 0.14987711 0.15225807 0.33523811]\n",
" [0.21793908 0.07456776 0.14541789 0.15212458 0.40995072]\n",
" [0.20534526 0.05235283 0.13498177 0.16629447 0.4410257 ]\n",
" [0.19316751 0.04030901 0.12785376 0.17649418 0.46217558]\n",
" [0.18477337 0.03347727 0.12290865 0.18365436 0.4751864 ]\n",
" [0.17917752 0.02950216 0.11970559 0.188323 0.4832918 ]\n",
" [0.17559545 0.02714482 0.11767339 0.19129492 0.48829152]\n",
" [0.17334211 0.02573001 0.11640338 0.19315585 0.49136876]\n",
" [0.17194026 0.02487434 0.11561605 0.19431083 0.49325865]\n",
" [0.1710736 0.02435435 0.11513036 0.19502381 0.49441802]]\n"
]
}
],
"source": [
"v3 = np.array([[0.2, 0.2, 0.2, 0.2, 0.2]]).T\n",
"show_convergence(v3)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (Anaconda 5)",
"env": {
"LD_LIBRARY_PATH": "/ext/anaconda5/lib",
"PROJ_LIB": "/ext/anaconda-2019.03/share/proj",
"PYTHONHOME": "/ext/anaconda5/lib/python3.5",
"PYTHONPATH": "/ext/anaconda5/lib/python3.5:/ext/anaconda5/lib/python3.5/site-packages"
},
"language": "python",
"metadata": {
"cocalc": {
"description": "Python/R distribution for data science",
"priority": -1,
"url": "https://www.anaconda.com/distribution/"
}
},
"name": "anaconda5"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment