Skip to content

Commit 61bbe9c

Browse files
author
Junpeng Lao
committed
PEP8 and add doc to examples.rst
1 parent 9b646cc commit 61bbe9c

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

docs/source/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Howto
1212
notebooks/Diagnosing_biased_Inference_with_Divergences.ipynb
1313
notebooks/posterior_predictive.ipynb
1414
notebooks/howto_debugging.ipynb
15+
notebooks/PyMC3_tips_and_heuristic.ipynb
1516
notebooks/LKJ.ipynb
1617
notebooks/live_sample_plots.ipynb
1718

docs/source/notebooks/PyMC3_tips_and_heuristic.ipynb

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@
332332
}
333333
],
334334
"source": [
335-
"value=np.asarray(np.random.randn(N,), dtype=theano.config.floatX)\n",
335+
"value = np.asarray(np.random.randn(N,), dtype=theano.config.floatX)\n",
336336
"\n",
337337
"maxwz = max([sum(w) for w in weights])\n",
338338
"N = len(weights)\n",
@@ -351,6 +351,7 @@
351351
"a = tt.matrix(\"a\", dtype='int32')\n",
352352
"a.tag.test_value = amat\n",
353353
"\n",
354+
"\n",
354355
"def get_mu(w, a):\n",
355356
" a1 = tt.cast(a, 'int32')\n",
356357
" return tt.sum(w*x[a1])/tt.sum(w)\n",
@@ -360,9 +361,10 @@
360361
"\n",
361362
"print(compute_elementwise(value, wmat, amat))\n",
362363
"\n",
364+
"\n",
363365
"def mu_phi(value):\n",
364-
" N=len(weights)\n",
365-
" # Calculate mu based on average of neighbours \n",
366+
" N = len(weights)\n",
367+
" # Calculate mu based on average of neighbours\n",
366368
" mu = np.array([np.sum(weights[i]*value[adj[i]])/Wplus[i] for i in range(N)])\n",
367369
" return mu\n",
368370
"\n",
@@ -402,7 +404,7 @@
402404
"class CAR(distribution.Continuous):\n",
403405
" \"\"\"\n",
404406
" Conditional Autoregressive (CAR) distribution\n",
405-
" \n",
407+
"\n",
406408
" Parameters\n",
407409
" ----------\n",
408410
" a : list of adjacency information\n",
@@ -415,18 +417,18 @@
415417
" self.w = w = tt.as_tensor_variable(w)\n",
416418
" self.tau = tau*tt.sum(w, axis=1)\n",
417419
" self.mode = 0.\n",
418-
" \n",
420+
"\n",
419421
" def get_mu(self, x):\n",
420-
" \n",
422+
"\n",
421423
" def weigth_mu(w, a):\n",
422424
" a1 = tt.cast(a, 'int32')\n",
423425
" return tt.sum(w*x[a1])/tt.sum(w)\n",
424426
"\n",
425-
" mu_w, _ = scan(fn=weigth_mu, \n",
427+
" mu_w, _ = scan(fn=weigth_mu,\n",
426428
" sequences=[self.w, self.a])\n",
427-
" \n",
429+
"\n",
428430
" return mu_w\n",
429-
" \n",
431+
"\n",
430432
" def logp(self, x):\n",
431433
" mu_w = self.get_mu(x)\n",
432434
" tau = self.tau\n",
@@ -458,7 +460,7 @@
458460
}
459461
],
460462
"source": [
461-
"with pm.Model() as model:\n",
463+
"with pm.Model() as model1:\n",
462464
" # Vague prior on intercept\n",
463465
" beta0 = pm.Normal('beta0', mu=0.0, tau=1.0e-5)\n",
464466
" # Vague prior on covariate effect\n",
@@ -488,7 +490,7 @@
488490
" sd_c = pm.Deterministic('sd_c', tt.std(phi))\n",
489491
" # Proportion sptial variance\n",
490492
" alpha = pm.Deterministic('alpha', sd_c/(sd_h+sd_c))\n",
491-
" \n",
493+
"\n",
492494
" trace1 = pm.sample(3e3, njobs=2, tune=1000)"
493495
]
494496
},
@@ -602,14 +604,15 @@
602604
"for i, a in enumerate(adj):\n",
603605
" amat2[i, a] = 1\n",
604606
" wmat2[i, a] = weights[i]\n",
605-
" \n",
606-
"value=np.asarray(np.random.randn(N,), dtype=theano.config.floatX)\n",
607+
"\n",
608+
"value = np.asarray(np.random.randn(N,), dtype=theano.config.floatX)\n",
607609
"\n",
608610
"print(np.sum(value*amat2, axis=1)/np.sum(wmat2, axis=1))\n",
609611
"\n",
612+
"\n",
610613
"def mu_phi(value):\n",
611-
" N=len(weights)\n",
612-
" # Calculate mu based on average of neighbours \n",
614+
" N = len(weights)\n",
615+
" # Calculate mu based on average of neighbours\n",
613616
" mu = np.array([np.sum(weights[i]*value[adj[i]])/Wplus[i] for i in range(N)])\n",
614617
" return mu\n",
615618
"\n",
@@ -634,7 +637,7 @@
634637
"class CAR2(distribution.Continuous):\n",
635638
" \"\"\"\n",
636639
" Conditional Autoregressive (CAR) distribution\n",
637-
" \n",
640+
"\n",
638641
" Parameters\n",
639642
" ----------\n",
640643
" a : adjacency matrix\n",
@@ -648,12 +651,12 @@
648651
" self.w = w = tt.as_tensor_variable(w)\n",
649652
" self.tau = tau*tt.sum(w, axis=1)\n",
650653
" self.mode = 0.\n",
651-
" \n",
654+
"\n",
652655
" def logp(self, x):\n",
653656
" tau = self.tau\n",
654657
" w = self.w\n",
655658
" a = self.a\n",
656-
" \n",
659+
"\n",
657660
" mu_w = tt.sum(x*a, axis=1)/tt.sum(w, axis=1)\n",
658661
" return tt.sum(continuous.Normal.dist(mu=mu_w, tau=tau).logp(x))"
659662
]
@@ -676,7 +679,7 @@
676679
}
677680
],
678681
"source": [
679-
"with pm.Model() as model:\n",
682+
"with pm.Model() as model2:\n",
680683
" # Vague prior on intercept\n",
681684
" beta0 = pm.Normal('beta0', mu=0.0, tau=1.0e-5)\n",
682685
" # Vague prior on covariate effect\n",
@@ -706,7 +709,7 @@
706709
" sd_c = pm.Deterministic('sd_c', tt.std(phi))\n",
707710
" # Proportion sptial variance\n",
708711
" alpha = pm.Deterministic('alpha', sd_c/(sd_h+sd_c))\n",
709-
" \n",
712+
"\n",
710713
" trace2 = pm.sample(3e3, njobs=2, tune=1000)"
711714
]
712715
},
@@ -807,10 +810,10 @@
807810
},
808811
"outputs": [],
809812
"source": [
810-
"X = np.hstack((np.ones((N,1)), stats.zscore(aff, ddof=1)[:,None]))\n",
813+
"X = np.hstack((np.ones((N, 1)), stats.zscore(aff, ddof=1)[:, None]))\n",
811814
"W = wmat2\n",
812815
"D = np.diag(W.sum(axis=1))\n",
813-
"log_offset = logE[:,None]"
816+
"log_offset = logE[:, None]"
814817
]
815818
},
816819
{
@@ -845,7 +848,7 @@
845848
}
846849
],
847850
"source": [
848-
"with pm.Model() as model:\n",
851+
"with pm.Model() as model3:\n",
849852
" # Vague prior on intercept and effect\n",
850853
" beta = pm.Normal('beta', mu=0.0, tau=1.0, shape=(2, 1))\n",
851854
"\n",
@@ -859,7 +862,7 @@
859862
"\n",
860863
" # Likelihood\n",
861864
" Yi = pm.Poisson('Yi', mu=mu.ravel(), observed=O)\n",
862-
" \n",
865+
"\n",
863866
" trace3 = pm.sample(3e3, njobs=2, tune=1000)"
864867
]
865868
},
@@ -1025,10 +1028,12 @@
10251028
"outputs": [],
10261029
"source": [
10271030
"import scipy\n",
1031+
"\n",
1032+
"\n",
10281033
"class Sparse_CAR(distribution.Continuous):\n",
10291034
" \"\"\"\n",
10301035
" Sparse Conditional Autoregressive (CAR) distribution\n",
1031-
" \n",
1036+
"\n",
10321037
" Parameters\n",
10331038
" ----------\n",
10341039
" alpha : spatial smoothing term\n",
@@ -1044,31 +1049,31 @@
10441049
" self.n = n\n",
10451050
" self.median = self.mode = self.mean = 0\n",
10461051
" super(Sparse_CAR, self).__init__(*args, **kwargs)\n",
1047-
" \n",
1052+
"\n",
10481053
" # eigenvalues of D^−1/2 * W * D^−1/2\n",
10491054
" Dinv_sqrt = np.diag(1 / np.sqrt(D))\n",
10501055
" DWD = np.matmul(np.matmul(Dinv_sqrt, W), Dinv_sqrt)\n",
10511056
" self.lam = scipy.linalg.eigvalsh(DWD)\n",
1052-
" \n",
1057+
"\n",
10531058
" # sparse representation of W\n",
10541059
" w_sparse = scipy.sparse.csr_matrix(W)\n",
10551060
" self.W = theano.sparse.as_sparse_variable(w_sparse)\n",
10561061
" self.D = tt.as_tensor_variable(D)\n",
1057-
" \n",
1062+
"\n",
10581063
" # Presicion Matrix (inverse of Covariance matrix)\n",
10591064
" # d_sparse = scipy.sparse.csr_matrix(np.diag(D))\n",
1060-
" # self.D = theano.sparse.as_sparse_variable(d_sparse)\n",
1065+
" # self.D = theano.sparse.as_sparse_variable(d_sparse)\n",
10611066
" # self.Phi = self.tau * (self.D - self.alpha*self.W)\n",
1062-
" \n",
1067+
"\n",
10631068
" def logp(self, x):\n",
10641069
" logtau = self.n * tt.log(tau)\n",
10651070
" logdet = tt.log(1 - self.alpha * self.lam).sum()\n",
1066-
" \n",
1071+
"\n",
10671072
" # tau * ((phi .* D_sparse)' * phi - alpha * (phit_W * phi))\n",
10681073
" Wx = theano.sparse.dot(self.W, x)\n",
10691074
" tau_dot_x = self.D * x.T - self.alpha * Wx.ravel()\n",
10701075
" logquad = self.tau * tt.dot(x.ravel(), tau_dot_x.ravel())\n",
1071-
" \n",
1076+
"\n",
10721077
" # logquad = tt.dot(x.T, theano.sparse.dot(self.Phi, x)).sum()\n",
10731078
" return 0.5*(logtau + logdet - logquad)"
10741079
]
@@ -1093,21 +1098,21 @@
10931098
}
10941099
],
10951100
"source": [
1096-
"with pm.Model() as model:\n",
1101+
"with pm.Model() as model4:\n",
10971102
" # Vague prior on intercept and effect\n",
10981103
" beta = pm.Normal('beta', mu=0.0, tau=1.0, shape=(2, 1))\n",
10991104
"\n",
11001105
" # Priors for spatial random effects\n",
11011106
" tau = pm.Gamma('tau', alpha=2., beta=2.)\n",
11021107
" alpha = pm.Uniform('alpha', lower=0, upper=1)\n",
1103-
" phi = Sparse_CAR('phi', alpha, W, tau, shape=(N,1))\n",
1104-
" \n",
1108+
" phi = Sparse_CAR('phi', alpha, W, tau, shape=(N, 1))\n",
1109+
"\n",
11051110
" # Mean model\n",
11061111
" mu = pm.Deterministic('mu', tt.exp(tt.dot(X, beta) + phi + log_offset))\n",
11071112
"\n",
11081113
" # Likelihood\n",
11091114
" Yi = pm.Poisson('Yi', mu=mu.ravel(), observed=O)\n",
1110-
" \n",
1115+
"\n",
11111116
" trace4 = pm.sample(3e3, njobs=2, tune=1000)"
11121117
]
11131118
},

0 commit comments

Comments
 (0)