Problem 3 - Practical HBM

Joint Orbital Period Breakpoint and Eccentricity Distribution Hierarchical Bayesian Model for Eclipsing Binaries with PyJAGS

Simulating a joint eccentricity and Period distribution of Eclpising Binaries from the Kepler Mission

LSSTC DSFP Session 4, September 21st, 2017

Author: Megan I. Shabram, PhD, NASA Postdoctoral Program Fellow, mshabram@gmail.com

Using the JAGS model code block for a break-point HBM provided below, set up and evaluate the break-point HBM on a real Kepler eclipsing binary data set. Some code and the datafile is provided for analysis, but you may also want to copy code from previous notebooks, and be sure to adjust the number of traceplots and 2d marginals for latent variables (e.g., don't plot all ~700 latent variable marginal posteriors etc.). There is some wait time involved during the MCMC computation.


In [2]:
import numpy as np
import scipy.stats as stats
import pandas as pd
import matplotlib.pyplot as plt
import pyjags
import pystan
import pickle
import triangle_linear

from IPython.display import display, Math, Latex
from __future__ import division, print_function
from pandas.tools.plotting import *
from matplotlib import rcParams
rcParams["savefig.dpi"] = 100
rcParams["font.size"] = 20
#plt.style.use('ggplot')

%matplotlib inline
%config InlineBackend.figure_format = 'retina'
#%qtconsole

In [3]:
## Read in Eclipsing Binary dataset containing projected eccentricity and orbital periods.
df_EB = pd.read_csv('EBs_for_jags.txt', delimiter=r"\s+")
print(df_EB)

print(len(df_EB.loc[:, "H_OBS"]))

## the only columns you need are P, H_OBS, K_OBS, H_SIGMA, and K_SIGMA


           ID           P     H_OBS   H_SIGMA     K_OBS   K_SIGMA  x1  x2  \
0     1026032    8.460438 -0.041576  0.000035  0.002119  0.004618   1   1   
1     1433980    1.592634 -0.000387  0.000103  0.014743  0.001303   1   1   
2     1571511   14.022451 -0.042219  0.000566  0.231536  0.023805   1   1   
3     1995732   77.362721 -0.131261  0.000044  0.079461  0.004085   1   1   
4     2019076    7.129228  0.000058  0.000020 -0.003283  0.003716   1   1   
5     2161623    2.283470  0.001422  0.000052 -0.040365  0.002232   1   1   
6     2162994    4.101596  0.000202  0.000010 -0.001038  0.000444   1   1   
7     2305372    1.404692  0.004280  0.000024 -0.028196  0.000683   1   1   
8     2306740   10.306987 -0.023318  0.000008  0.295810  0.000564   1   1   
9     2307206  204.031379 -0.010914  0.000035  0.199013  0.020766   1   1   
10    2442084   49.788598 -0.502625  0.000490 -0.304308  0.002906   1   1   
11    2445134    8.412009  0.000959  0.000219 -0.006187  0.008268   1   1   
12    2447893    0.661620 -0.000363  0.000023 -0.017731  0.000682   1   1   
13    2576692   87.878533 -0.142055  0.000025  0.144125  0.001178   1   1   
14    2580872   15.926628 -0.052978  0.000013  0.193953  0.000885   1   1   
15    2693092   39.841524 -0.247333  0.000554 -0.075968  0.031460   1   1   
16    2708156    1.891272  0.000080  0.000043 -0.075032  0.001477   1   1   
17    2719873   17.279290 -0.001701  0.000004  0.026354  0.000527   1   1   
18    2852560   11.961308 -0.332958  0.000087 -0.244139  0.001007   1   1   
19    2860594    5.499945  0.001205  0.000872 -0.023981  0.002110   1   1   
20    2860788    5.259742 -0.000058  0.000027  0.001229  0.001094   1   1   
21    2973509    6.627611 -0.102516  0.000015  0.105207  0.000808   1   1   
22    2998124   28.597884 -0.043639  0.000023  0.029829  0.001949   1   1   
23    3003991    7.244779 -0.000266  0.001066  0.010253  0.020675   1   1   
24    3098194   30.476531 -0.200501  0.000046 -0.284654  0.000743   1   1   
25    3102024   13.782521 -0.283510  0.000245  0.448874  0.001533   1   1   
26    3115480    3.693703  0.000198  0.000080  0.002189  0.007745   1   1   
27    3120320   10.265613 -0.036189  0.000062  0.011831  0.006757   1   1   
28    3127817    4.327139  0.000186  0.000042  0.004331  0.001738   1   1   
29    3218683    0.771670 -0.000934  0.000017  0.005321  0.000483   1   1   
..        ...         ...       ...       ...       ...       ...  ..  ..   
765  11966557   60.297611 -0.138028  0.000130  0.201512  0.004331   1   1   
766  11967004    1.941636 -0.000125  0.000312  0.024066  0.020470   1   1   
767  11968490    1.078890 -0.000360  0.000093 -0.007769  0.004658   1   1   
768  11975363    3.518419  0.000059  0.000006  0.002388  0.000499   1   1   
769  12004679    5.042490  0.001767  0.000004  0.000597  0.000531   1   1   
770  12004834    0.262317  0.000429  0.000028  0.003621  0.000887   1   1   
771  12013615    8.202869  0.005458  0.000007  0.010300  0.000500   1   1   
772  12017140   22.845242 -0.079474  0.000117  0.049485  0.006665   1   1   
773  12022517    3.442713  0.002830  0.000056 -0.016472  0.004666   1   1   
774  12022718    9.255331  0.000927  0.000116  0.044196  0.032330   1   1   
775  12023089    0.623441  0.000281  0.000141 -0.006365  0.006262   1   1   
776  12071006    6.096026 -0.000099  0.000017  0.006597  0.000797   1   1   
777  12105785   31.953029 -0.070169  0.000207 -0.340198  0.007628   1   1   
778  12108333    0.705448  0.000143  0.000011 -0.008580  0.000264   1   1   
779  12164634   89.331217 -0.307283  0.000040 -0.019576  0.003719   1   1   
780  12164751    2.630088  0.007104  0.000030 -0.022053  0.000734   1   1   
781  12208887   53.501801 -0.013160  0.000019 -0.381525  0.002676   1   1   
782  12217907   43.204586 -0.254297  0.000018 -0.317951  0.000191   1   1   
783  12251779   14.844233 -0.385100  0.000073  0.121534  0.001258   1   1   
784  12257908    2.615917  0.002578  0.000015  0.053444  0.000046   1   1   
785  12302391   25.321733 -0.096111  0.000109  0.311804  0.003272   1   1   
786  12306808   37.878484 -0.087349  0.000009 -0.050849  0.001298   1   1   
787  12316447   17.907700 -0.368418  0.000114  0.009599  0.009989   1   1   
788  12356746    1.004908 -0.000154  0.000059 -0.003463  0.002433   1   1   
789  12367310    8.627487  0.011192  0.000205  0.067714  0.021508   1   1   
790  12418816    1.521870  0.000171  0.000006  0.000155  0.000421   1   1   
791  12470530    8.207257 -0.372782  0.000041  0.066512  0.001601   1   1   
792  12557713    7.214723 -0.016362  0.000077  0.006720  0.014730   1   1   
793  12644769  225.885000 -0.018199  0.000006 -0.123151  0.000867   1   1   
794  12645761    5.419138  0.000141  0.000105 -0.008132  0.009109   1   1   

            y1         y2  
0     0.146300   0.041934  
1     0.104319   0.051596  
2     0.022875   0.208284  
3     0.050191   0.059772  
4     0.100236   0.068105  
5     0.089601   0.044812  
6     0.128129   0.070751  
7     0.155182   0.062688  
8     0.036220   0.061645  
9    11.264874  12.926236  
10    0.068825   0.073659  
11    0.174629   0.117760  
12    0.124905   0.050477  
13    0.075480   0.062568  
14    0.055871   0.031242  
15    1.041663   0.125298  
16    0.121354   0.070802  
17    0.082409   0.064280  
18    0.040490   0.048260  
19    0.116823   0.062170  
20    0.118522   0.052731  
21    0.063796   0.072269  
22    0.122161   0.084230  
23    0.032950   0.125550  
24    0.051825   0.039367  
25    0.056179   0.064228  
26    0.135031   0.039131  
27    0.110694   0.033165  
28    0.085770   0.062816  
29    0.084725   0.062895  
..         ...        ...  
765   0.062334   0.085173  
766   0.176906   0.139312  
767   0.074325   0.051825  
768   0.102202   0.064987  
769   0.083881   0.066227  
770   0.153350   0.064305  
771   0.118875   0.064274  
772   0.078911   0.140997  
773   0.184569   0.043749  
774   0.086056   0.087227  
775   0.126911   0.071687  
776   0.125346   0.053073  
777   0.061013   0.065657  
778   0.150961   0.050850  
779   0.076679   0.092546  
780   0.135851   0.066083  
781   0.074421   0.048687  
782   0.079889   0.050307  
783   0.047107   0.055622  
784   0.124073   0.087630  
785   0.039701   0.058951  
786   0.050034   0.055919  
787   0.090560   0.083616  
788   0.124039   0.053020  
789   0.144165   0.048859  
790   0.124191   0.059909  
791   0.056014   0.070205  
792   0.136694   0.046721  
793   0.082166   0.046151  
794   0.104456   0.061957  

[795 rows x 10 columns]
795

In [4]:
code = '''

model {
    
    # Period break point hyper parameter:
    p_break ~ dunif(0.001,20)

    #Population parameters
    for (j in 1:Nm) {
        e_sigma_a[j] ~ dunif(0, 1)
        e_phi_a[j] <- 1/(e_sigma_a[j]*e_sigma_a[j])
        a[j] <- 1;
    }
    
    e_sigma_b ~ dunif(0.,1.)
    e_phi_b <- 1/(e_sigma_b*e_sigma_b)
    
    f_a ~ ddirch(a[])

    for (n in 1:Ndata){

        c_a[n] ~ dcat(f_a[])

        e_phi_ref[n] <- ifelse(P[n] < p_break, e_phi_b ,e_phi_a[c_a[n]])

        #True planet properties
        h[n] ~ dnorm(0, e_phi_ref[n]) T(-1,1)
        k[n] ~ dnorm(0, e_phi_ref[n]) T(-sqrt(1-h[n]*h[n]),sqrt(1-h[n]*h[n]))
    
        #Observed planet properties
        hhat[n] ~ dnorm(h[n], 1.0/(hhat_sigma[n]*hhat_sigma[n])) T(-1,1)
        khat[n] ~ dnorm(k[n], 1.0/(khat_sigma[n]*khat_sigma[n])) T(-sqrt(1-hhat[n]*hhat[n]),sqrt(1-hhat[n]*hhat[n]))
    }
        
}
'''

In [5]:
## Load additional JAGS module
pyjags.load_module('glm')
pyjags.load_module('dic')


## See blog post for origination of the adapted analysis tools used here and below:
## https://martynplummer.wordpress.com/2016/01/11/pyjags/

num_chains = 4
iterations = 10000


## data list include only variables in the model
model = pyjags.Model(code, data=dict(Nm=2, Ndata=len(df_EB.loc[:, "P"]), P=df_EB.loc[:, "P"], 
                                     hhat_sigma=df_EB.loc[:, "H_SIGMA"], khat=df_EB.loc[:, "K_OBS"], 
                                     khat_sigma=df_EB.loc[:, "K_SIGMA"]), 
                     chains=num_chains, adapt=1000)

## Code to speed up compute time. This feature might not be 
## well tested in pyjags at this time. 
## threads=4, chains_per_thread=1 

## 500 warmup / burn-in iterations, not used for inference.
model.sample(500, vars=[])

## Run model for desired steps, monitoring hyperparameter variables, and latent variables
## for hierarchical Bayesian model.
## Returns a dictionary with numpy array for each monitored variable.
## Shapes of returned arrays are (... shape of variable ..., iterations, chains).
## samples = model.sample(#iterations per chain here, vars=['e_sigma', 'h'])
samples_JAGS = model.sample(iterations, vars=['p_break','e_sigma_a', 'e_sigma_b', 'f_a' ])

## Code to save, open and use pickled dictionary of samples:
## -- Pickle the data --
#with open('ecc_1_test.pkl', 'wb') as handle:
#   pickle.dump(samples, handle)
## -- Retrieve pickled data --
#with open('ecc_1_test.pkl', 'rb') as handle:
#   retrieved_results = pickle.load(handle)


adapting: iterations 88 of 4000, elapsed 0:00:06, remaining 0:04:31
adapting: iterations 240 of 4000, elapsed 0:00:13, remaining 0:03:22
adapting: iterations 440 of 4000, elapsed 0:00:18, remaining 0:02:25
adapting: iterations 692 of 4000, elapsed 0:00:25, remaining 0:01:58
adapting: iterations 976 of 4000, elapsed 0:00:32, remaining 0:01:38
adapting: iterations 1284 of 4000, elapsed 0:00:42, remaining 0:01:29
adapting: iterations 1436 of 4000, elapsed 0:00:48, remaining 0:01:25
adapting: iterations 1584 of 4000, elapsed 0:00:53, remaining 0:01:21
adapting: iterations 1732 of 4000, elapsed 0:01:00, remaining 0:01:18
adapting: iterations 1876 of 4000, elapsed 0:01:07, remaining 0:01:16
adapting: iterations 2012 of 4000, elapsed 0:01:13, remaining 0:01:12
adapting: iterations 2148 of 4000, elapsed 0:01:19, remaining 0:01:08
adapting: iterations 2284 of 4000, elapsed 0:01:24, remaining 0:01:03
adapting: iterations 2420 of 4000, elapsed 0:01:29, remaining 0:00:58
adapting: iterations 2552 of 4000, elapsed 0:01:34, remaining 0:00:53
adapting: iterations 2684 of 4000, elapsed 0:01:39, remaining 0:00:49
adapting: iterations 2948 of 4000, elapsed 0:01:49, remaining 0:00:39
adapting: iterations 3080 of 4000, elapsed 0:01:54, remaining 0:00:34
adapting: iterations 3344 of 4000, elapsed 0:02:01, remaining 0:00:24
adapting: iterations 3620 of 4000, elapsed 0:02:07, remaining 0:00:13
adapting: iterations 3900 of 4000, elapsed 0:02:17, remaining 0:00:04
adapting: iterations 4000 of 4000, elapsed 0:02:21, remaining 0:00:00
sampling: iterations 312 of 2000, elapsed 0:00:08, remaining 0:00:44
sampling: iterations 500 of 2000, elapsed 0:00:14, remaining 0:00:42
sampling: iterations 676 of 2000, elapsed 0:00:19, remaining 0:00:38
sampling: iterations 848 of 2000, elapsed 0:00:25, remaining 0:00:33
sampling: iterations 1020 of 2000, elapsed 0:00:31, remaining 0:00:29
sampling: iterations 1352 of 2000, elapsed 0:00:40, remaining 0:00:19
sampling: iterations 1692 of 2000, elapsed 0:00:47, remaining 0:00:09
sampling: iterations 1868 of 2000, elapsed 0:00:53, remaining 0:00:04
sampling: iterations 2000 of 2000, elapsed 0:00:58, remaining 0:00:00
sampling: iterations 268 of 40000, elapsed 0:00:08, remaining 0:19:03
sampling: iterations 440 of 40000, elapsed 0:00:13, remaining 0:19:31
sampling: iterations 608 of 40000, elapsed 0:00:20, remaining 0:21:38
sampling: iterations 916 of 40000, elapsed 0:00:27, remaining 0:19:25
sampling: iterations 1080 of 40000, elapsed 0:00:33, remaining 0:19:40
sampling: iterations 1412 of 40000, elapsed 0:00:41, remaining 0:18:32
sampling: iterations 1760 of 40000, elapsed 0:00:49, remaining 0:17:44
sampling: iterations 2116 of 40000, elapsed 0:00:57, remaining 0:17:03
sampling: iterations 2484 of 40000, elapsed 0:01:06, remaining 0:16:37
sampling: iterations 2860 of 40000, elapsed 0:01:15, remaining 0:16:18
sampling: iterations 3236 of 40000, elapsed 0:01:26, remaining 0:16:13
sampling: iterations 3424 of 40000, elapsed 0:01:33, remaining 0:16:33
sampling: iterations 3608 of 40000, elapsed 0:01:38, remaining 0:16:33
sampling: iterations 3968 of 40000, elapsed 0:01:51, remaining 0:16:51
sampling: iterations 4144 of 40000, elapsed 0:01:57, remaining 0:16:49
sampling: iterations 4320 of 40000, elapsed 0:02:02, remaining 0:16:49
sampling: iterations 4496 of 40000, elapsed 0:02:09, remaining 0:16:59
sampling: iterations 4668 of 40000, elapsed 0:02:16, remaining 0:17:08
sampling: iterations 4836 of 40000, elapsed 0:02:22, remaining 0:17:15
sampling: iterations 5004 of 40000, elapsed 0:02:30, remaining 0:17:32
sampling: iterations 5168 of 40000, elapsed 0:02:37, remaining 0:17:39
sampling: iterations 5496 of 40000, elapsed 0:02:51, remaining 0:17:51
sampling: iterations 5656 of 40000, elapsed 0:02:57, remaining 0:17:57
sampling: iterations 5968 of 40000, elapsed 0:03:07, remaining 0:17:45
sampling: iterations 6124 of 40000, elapsed 0:03:12, remaining 0:17:43
sampling: iterations 6280 of 40000, elapsed 0:03:17, remaining 0:17:40
sampling: iterations 6436 of 40000, elapsed 0:03:23, remaining 0:17:40
sampling: iterations 6748 of 40000, elapsed 0:03:31, remaining 0:17:19
sampling: iterations 7064 of 40000, elapsed 0:03:38, remaining 0:16:56
sampling: iterations 7384 of 40000, elapsed 0:03:45, remaining 0:16:33
sampling: iterations 7712 of 40000, elapsed 0:03:52, remaining 0:16:11
sampling: iterations 8040 of 40000, elapsed 0:03:59, remaining 0:15:50
sampling: iterations 8376 of 40000, elapsed 0:04:06, remaining 0:15:29
sampling: iterations 8712 of 40000, elapsed 0:04:13, remaining 0:15:10
sampling: iterations 9052 of 40000, elapsed 0:04:21, remaining 0:14:52
sampling: iterations 9396 of 40000, elapsed 0:04:28, remaining 0:14:34
sampling: iterations 9740 of 40000, elapsed 0:04:37, remaining 0:14:20
sampling: iterations 10088 of 40000, elapsed 0:04:45, remaining 0:14:05
sampling: iterations 10440 of 40000, elapsed 0:04:52, remaining 0:13:48
sampling: iterations 10792 of 40000, elapsed 0:05:00, remaining 0:13:33
sampling: iterations 10968 of 40000, elapsed 0:05:05, remaining 0:13:29
sampling: iterations 11144 of 40000, elapsed 0:05:11, remaining 0:13:24
sampling: iterations 11320 of 40000, elapsed 0:05:17, remaining 0:13:22
sampling: iterations 11496 of 40000, elapsed 0:05:22, remaining 0:13:20
sampling: iterations 11672 of 40000, elapsed 0:05:29, remaining 0:13:18
sampling: iterations 11848 of 40000, elapsed 0:05:36, remaining 0:13:17
sampling: iterations 12024 of 40000, elapsed 0:05:42, remaining 0:13:16
sampling: iterations 12196 of 40000, elapsed 0:05:48, remaining 0:13:14
sampling: iterations 12540 of 40000, elapsed 0:05:58, remaining 0:13:03
sampling: iterations 12884 of 40000, elapsed 0:06:08, remaining 0:12:55
sampling: iterations 13056 of 40000, elapsed 0:06:14, remaining 0:12:53
sampling: iterations 13400 of 40000, elapsed 0:06:23, remaining 0:12:39
sampling: iterations 13744 of 40000, elapsed 0:06:30, remaining 0:12:26
sampling: iterations 14096 of 40000, elapsed 0:06:40, remaining 0:12:16
sampling: iterations 14448 of 40000, elapsed 0:06:49, remaining 0:12:04
sampling: iterations 14800 of 40000, elapsed 0:06:58, remaining 0:11:52
sampling: iterations 15152 of 40000, elapsed 0:07:07, remaining 0:11:40
sampling: iterations 15504 of 40000, elapsed 0:07:16, remaining 0:11:29
sampling: iterations 15856 of 40000, elapsed 0:07:25, remaining 0:11:18
sampling: iterations 16208 of 40000, elapsed 0:07:35, remaining 0:11:08
sampling: iterations 16560 of 40000, elapsed 0:07:43, remaining 0:10:56
sampling: iterations 16912 of 40000, elapsed 0:07:51, remaining 0:10:43
sampling: iterations 17264 of 40000, elapsed 0:07:59, remaining 0:10:30
sampling: iterations 17624 of 40000, elapsed 0:08:06, remaining 0:10:18
sampling: iterations 17984 of 40000, elapsed 0:08:14, remaining 0:10:05
sampling: iterations 18344 of 40000, elapsed 0:08:22, remaining 0:09:53
sampling: iterations 18704 of 40000, elapsed 0:08:30, remaining 0:09:41
sampling: iterations 19064 of 40000, elapsed 0:08:38, remaining 0:09:29
sampling: iterations 19432 of 40000, elapsed 0:08:46, remaining 0:09:17
sampling: iterations 19800 of 40000, elapsed 0:08:54, remaining 0:09:05
sampling: iterations 20168 of 40000, elapsed 0:09:02, remaining 0:08:53
sampling: iterations 20536 of 40000, elapsed 0:09:10, remaining 0:08:41
sampling: iterations 20904 of 40000, elapsed 0:09:19, remaining 0:08:31
sampling: iterations 21088 of 40000, elapsed 0:09:24, remaining 0:08:26
sampling: iterations 21456 of 40000, elapsed 0:09:34, remaining 0:08:16
sampling: iterations 21640 of 40000, elapsed 0:09:39, remaining 0:08:11
sampling: iterations 21824 of 40000, elapsed 0:09:44, remaining 0:08:06
sampling: iterations 22008 of 40000, elapsed 0:09:49, remaining 0:08:01
sampling: iterations 22192 of 40000, elapsed 0:09:54, remaining 0:07:57
sampling: iterations 22376 of 40000, elapsed 0:09:59, remaining 0:07:52
sampling: iterations 22560 of 40000, elapsed 0:10:05, remaining 0:07:48
sampling: iterations 22744 of 40000, elapsed 0:10:10, remaining 0:07:43
sampling: iterations 22928 of 40000, elapsed 0:10:15, remaining 0:07:38
sampling: iterations 23296 of 40000, elapsed 0:10:25, remaining 0:07:28
sampling: iterations 23664 of 40000, elapsed 0:10:34, remaining 0:07:18
sampling: iterations 24032 of 40000, elapsed 0:10:44, remaining 0:07:08
sampling: iterations 24400 of 40000, elapsed 0:10:54, remaining 0:06:58
sampling: iterations 24584 of 40000, elapsed 0:10:59, remaining 0:06:53
sampling: iterations 24768 of 40000, elapsed 0:11:04, remaining 0:06:48
sampling: iterations 25136 of 40000, elapsed 0:11:14, remaining 0:06:39
sampling: iterations 25320 of 40000, elapsed 0:11:19, remaining 0:06:34
sampling: iterations 25688 of 40000, elapsed 0:11:29, remaining 0:06:24
sampling: iterations 25872 of 40000, elapsed 0:11:34, remaining 0:06:19
sampling: iterations 26056 of 40000, elapsed 0:11:39, remaining 0:06:14
sampling: iterations 26240 of 40000, elapsed 0:11:44, remaining 0:06:09
sampling: iterations 26424 of 40000, elapsed 0:11:49, remaining 0:06:04
sampling: iterations 26608 of 40000, elapsed 0:11:55, remaining 0:06:00
sampling: iterations 26792 of 40000, elapsed 0:12:00, remaining 0:05:55
sampling: iterations 27160 of 40000, elapsed 0:12:09, remaining 0:05:44
sampling: iterations 27528 of 40000, elapsed 0:12:17, remaining 0:05:34
sampling: iterations 27896 of 40000, elapsed 0:12:26, remaining 0:05:23
sampling: iterations 28264 of 40000, elapsed 0:12:35, remaining 0:05:14
sampling: iterations 28632 of 40000, elapsed 0:12:44, remaining 0:05:04
sampling: iterations 29000 of 40000, elapsed 0:12:53, remaining 0:04:53
sampling: iterations 29368 of 40000, elapsed 0:13:02, remaining 0:04:43
sampling: iterations 29736 of 40000, elapsed 0:13:11, remaining 0:04:33
sampling: iterations 30112 of 40000, elapsed 0:13:22, remaining 0:04:23
sampling: iterations 30480 of 40000, elapsed 0:13:30, remaining 0:04:13
sampling: iterations 30668 of 40000, elapsed 0:13:37, remaining 0:04:08
sampling: iterations 31036 of 40000, elapsed 0:13:45, remaining 0:03:58
sampling: iterations 31412 of 40000, elapsed 0:13:56, remaining 0:03:48
sampling: iterations 31596 of 40000, elapsed 0:14:01, remaining 0:03:44
sampling: iterations 31780 of 40000, elapsed 0:14:06, remaining 0:03:39
sampling: iterations 32148 of 40000, elapsed 0:14:14, remaining 0:03:29
sampling: iterations 32524 of 40000, elapsed 0:14:24, remaining 0:03:19
sampling: iterations 32712 of 40000, elapsed 0:14:30, remaining 0:03:14
sampling: iterations 32896 of 40000, elapsed 0:14:37, remaining 0:03:09
sampling: iterations 33264 of 40000, elapsed 0:14:45, remaining 0:02:59
sampling: iterations 33636 of 40000, elapsed 0:14:53, remaining 0:02:49
sampling: iterations 34012 of 40000, elapsed 0:15:01, remaining 0:02:39
sampling: iterations 34388 of 40000, elapsed 0:15:08, remaining 0:02:28
sampling: iterations 34764 of 40000, elapsed 0:15:16, remaining 0:02:18
sampling: iterations 35140 of 40000, elapsed 0:15:24, remaining 0:02:08
sampling: iterations 35516 of 40000, elapsed 0:15:32, remaining 0:01:58
sampling: iterations 35892 of 40000, elapsed 0:15:40, remaining 0:01:48
sampling: iterations 36268 of 40000, elapsed 0:15:47, remaining 0:01:37
sampling: iterations 36644 of 40000, elapsed 0:15:55, remaining 0:01:27
sampling: iterations 37024 of 40000, elapsed 0:16:03, remaining 0:01:17
sampling: iterations 37408 of 40000, elapsed 0:16:11, remaining 0:01:07
sampling: iterations 37792 of 40000, elapsed 0:16:19, remaining 0:00:57
sampling: iterations 38176 of 40000, elapsed 0:16:27, remaining 0:00:47
sampling: iterations 38560 of 40000, elapsed 0:16:35, remaining 0:00:37
sampling: iterations 38944 of 40000, elapsed 0:16:43, remaining 0:00:27
sampling: iterations 39328 of 40000, elapsed 0:16:51, remaining 0:00:17
sampling: iterations 39712 of 40000, elapsed 0:16:59, remaining 0:00:07
sampling: iterations 40000 of 40000, elapsed 0:17:05, remaining 0:00:00
sampling: iterations 40000 of 40000, elapsed 0:17:05, remaining 0:00:00

In [6]:
iters=iterations
chain_thin = 100
start = int(iters-1000)
esigma_low = np.where(samples_JAGS['e_sigma_a'][0,start::,:] <= samples_JAGS['e_sigma_a'][1,start::,:], samples_JAGS['e_sigma_a'][0,start::,:], samples_JAGS['e_sigma_a'][1,start::,:])
esigma_hi = np.where(samples_JAGS['e_sigma_a'][0,start::,:] > samples_JAGS['e_sigma_a'][1,start::,:], samples_JAGS['e_sigma_a'][0,start::,:], samples_JAGS['e_sigma_a'][1,start::,:])
f_low = np.where(samples_JAGS['e_sigma_a'][0,start::,:] <= samples_JAGS['e_sigma_a'][1,start::,:], samples_JAGS['f_a'][0,start::,:], samples_JAGS['f_a'][1,start::,:])
f_hi = np.where(samples_JAGS['e_sigma_a'][0,start::,:] > samples_JAGS['e_sigma_a'][1,start::,:], samples_JAGS['f_a'][0,start::,:], samples_JAGS['f_a'][1,start::,:])
print(np.min(f_hi))
plt.hist(f_low)


0.316326554665
Out[6]:
([array([   0.,   10.,  259.,  528.,  191.,   12.,    0.,    0.,    0.,    0.]),
  array([   0.,    0.,  101.,  564.,  322.,   13.,    0.,    0.,    0.,    0.]),
  array([   0.,    0.,    0.,    0.,    0.,    0.,    0.,   98.,  753.,  149.]),
  array([ 124.,  456.,  376.,   44.,    0.,    0.,    0.,    0.,    0.,    0.])],
 array([ 0.25117101,  0.29442125,  0.3376715 ,  0.38092174,  0.42417198,
         0.46742223,  0.51067247,  0.55392271,  0.59717296,  0.6404232 ,
         0.68367345]),
 <a list of 4 Lists of Patches objects>)

In [7]:
## Scatter matrix plot:

## Redefine the trace so that we only vizualize every 10th latent variable element in 
## the scatter_matrix plot below. Vizualizing all 50 is too cumbersome for the scatter
## matrix. 

samples_EB_for_scatter_matrix = {}
numHyperParams = 6
dim = numHyperParams
print(dim)

samples_EB_for_scatter_matrix.update({'$f_{a_{low}}$': f_low})
samples_EB_for_scatter_matrix.update({'$e_{\sigma_{a_{low}}}$': esigma_low})
samples_EB_for_scatter_matrix.update({'$f_{a_{high}}$': f_low})
samples_EB_for_scatter_matrix.update({'$e_{\sigma_{a_{high}}}$': esigma_hi})
samples_EB_for_scatter_matrix.update({'$e_{\sigma_{b}}$': samples_JAGS['e_sigma_b'][0,start::,:]})
samples_EB_for_scatter_matrix.update({'$P_{break}$': samples_JAGS['p_break'][0,start::,:]})

for j, i in samples_EB_for_scatter_matrix.items():
    print(j)
#    print(i)

trace_2 = pd.Panel({k: v for k, v in samples_EB_for_scatter_matrix.items()})

sm = scatter_matrix(trace_2.to_frame(),  color="darkturquoise", alpha=0.2, figsize=(dim*2, dim*2), diagonal='hist',hist_kwds={'bins':25,'histtype':'step', 'edgecolor':'r','linewidth':2})
## y labels size
[plt.setp(item.yaxis.get_label(), 'size', 20) for item in sm.ravel()]
## x labels size 
[plt.setp(item.xaxis.get_label(), 'size', 20) for item in sm.ravel()]
## Change label rotation
## This is helpful for very long labels
#[s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)]
[s.xaxis.label.set_rotation(0) for s in sm.reshape(-1)]
[s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)]
## May need to offset label when rotating to prevent overlap of figure
[s.get_yaxis().set_label_coords(-0.5,0.5) for s in sm.reshape(-1)]
## Hide all ticks
#[s.set_xticks(()) for s in sm.reshape(-1)]
#[s.set_yticks(()) for s in sm.reshape(-1)]

plt.savefig('scatter_matrix_period_break_point.png')


6
$e_{\sigma_{b}}$
$f_{a_{low}}$
$e_{\sigma_{a_{high}}}$
$P_{break}$
$e_{\sigma_{a_{low}}}$
$f_{a_{high}}$

In [8]:
## Use pandas three dimensional Panel to represent the trace:

trace = pd.Panel({k: v for k, v in samples_EB_for_scatter_matrix.items()})
trace.axes[0].name = 'Variable'
trace.axes[1].name = 'Iteration'
trace.axes[2].name = 'Chain'
 
## Point estimates:
print(trace.to_frame().mean())
 
## Bayesian equal-tailed 95% credible intervals:
print(trace.to_frame().quantile([0.05, 0.95]))
  ## ^ entering the values here could be a good question part
    
def plot(trace, var):
    fig, axes = plt.subplots(1, 3, figsize=(9, 3))
    fig.suptitle(var, y=0.95, fontsize='xx-large')
 
    ## Marginal posterior density estimate:
    trace[var].plot.density(ax=axes[0])
    axes[0].set_xlabel('Parameter value')
    axes[0].locator_params(tight=True)
 
    ## Autocorrelation for each chain:
    axes[1].set_xlim(0, 100)
    for chain in trace[var].columns:
        autocorrelation_plot(trace[var,:,chain], axes[1], label=chain)
 
    ## Trace plot:
    axes[2].set_ylabel('Parameter value')
    trace[var].plot(ax=axes[2])
 
    ## Save figure
    filename = var.replace("\\", "") 
    filename = filename.replace("/", "") 
    filename = filename.replace("$", "") 
    filename = filename.replace("}", "") 
    filename = filename.replace("{", "") 
    plt.tight_layout(pad=3)
    fig.savefig('Break_point_'+'{}.png'.format(filename))

    
## Display diagnostic plots
for var in trace:
    plot(trace, var)


Variable
$P_{break}$                5.567903
$e_{\sigma_{a_{high}}}$    0.151951
$e_{\sigma_{a_{low}}}$     0.009854
$e_{\sigma_{b}}$           0.141988
$f_{a_{high}}$             0.441038
$f_{a_{low}}$              0.441038
dtype: float64
Variable  $P_{break}$  $e_{\sigma_{a_{high}}}$  $e_{\sigma_{a_{low}}}$  \
0.05         0.047869                 0.135590                0.007182   
0.95         9.045642                 0.168916                0.013782   

Variable  $e_{\sigma_{b}}$  $f_{a_{high}}$  $f_{a_{low}}$  
0.05              0.020716        0.303350       0.303350  
0.95              0.805281        0.636499       0.636499  

After evaluating the diagnostics, what would your next steps be, given you had more computing resources?