From 26e1cbebb2a5149ad0b9e8cf96b160a203bce60f Mon Sep 17 00:00:00 2001 From: alphagammamle Date: Sun, 2 Apr 2017 02:11:38 -0400 Subject: [PATCH 1/2] State Transition Matrix update A easier to read or possbile faster way to compute the state probability transition matrix --- Data Generation and Likelihood Fit.ipynb | 35 +++++++----------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/Data Generation and Likelihood Fit.ipynb b/Data Generation and Likelihood Fit.ipynb index 22e3804..0136a1e 100644 --- a/Data Generation and Likelihood Fit.ipynb +++ b/Data Generation and Likelihood Fit.ipynb @@ -32,6 +32,7 @@ "import random as rnd\n", "import scipy.stats as stats\n", "import scipy.optimize as opt\n", + "import scipy.sparse\n", "import json as json\n", "import matplotlib as mpl\n", "from math import exp\n", @@ -526,17 +527,8 @@ " # Initialization of the state-transition matrices: describe the state-transition probabilities\n", " # if the maintenance cost is incurred, and regenerate the state to 0 if the replacement cost\n", " # is incurred.\n", - " ST_mat = np.zeros((S, S))\n", - " p = np.array(p) \n", - " for i in range(S):\n", - " for j, _p in enumerate(p):\n", - " if i + j < S-1:\n", - " ST_mat[i+j][i] = _p\n", - " \n", - " elif i + j == S-1:\n", - " ST_mat[S-1][i] = p[j:].sum()\n", - " else:\n", - " pass\n", + " ST_mat=scipy.sparse.diags(p, range(len(p)), shape=(S, S)).toarray()\n", + " ST_mat[-2:,-1] = [1 - p[1], 1]\n", " \n", " R_mat = np.vstack((np.ones((1, S)),np.zeros((S-1, S)))) \n", " \n", @@ -1211,15 +1203,8 @@ " # A (SxS) matrix indicating the probability of a bus transitioning\n", " # from a state s to a state s' (used to compute maintenance utility)\n", " \n", - " self.trans_mat = np.zeros((S, S))\n", - " for i in range(S):\n", - " for j, _p in enumerate(self.p):\n", - " if i + j < S-1:\n", - " self.trans_mat[i+j][i] = _p\n", - " elif i + j == S-1:\n", - " self.trans_mat[S-1][i] = p[j:].sum()\n", - " else:\n", - " pass\n", + " self.trans_mat=scipy.sparse.diags(p, range(len(p)), shape=(S, S)).toarray()\n", + " self.trans_mat[-2:,-1] = [1 - p[1], 1]\n", "\n", " # A second (SxS) matrix which regenerates the bus' state to 0 with\n", " # certainty (used to compute the replacement utility)\n", @@ -2141,21 +2126,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.4.3" + "pygments_lexer": "ipython2", + "version": "2.7.13" } }, "nbformat": 4, From 252987d3ed93aa6530559b189954260bfaa670bc Mon Sep 17 00:00:00 2001 From: alphagammamle Date: Sun, 2 Apr 2017 02:24:56 -0400 Subject: [PATCH 2/2] Update Data Generation and Likelihood Fit.ipynb --- Data Generation and Likelihood Fit.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Data Generation and Likelihood Fit.ipynb b/Data Generation and Likelihood Fit.ipynb index 0136a1e..b65c144 100644 --- a/Data Generation and Likelihood Fit.ipynb +++ b/Data Generation and Likelihood Fit.ipynb @@ -2126,21 +2126,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.13" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4,