In [1]:
push!(LOAD_PATH, "/Users/farr/Documents/Research/GaussianNoiseModeling/PopeFilters/code")


Out[1]:
3-element Array{ByteString,1}:
 "/Users/farr/Documents/code/julia/usr/local/share/julia/site/v0.4"     
 "/Users/farr/Documents/code/julia/usr/share/julia/site/v0.4"           
 "/Users/farr/Documents/Research/GaussianNoiseModeling/PopeFilters/code"

In [2]:
using Ensemble
using Kalman
using PyCall
using PyPlot

In [3]:
@pyimport corner
@pyimport seaborn as sns
sns.set_style("ticks")

In [4]:
data = readdlm("../epic210969800.txt")
errorbar(data[:,1], data[:,2], data[:,3], fmt=".")
xlabel(L"$t$ ($\mathrm{d}$)")
ylabel(L"$F$ ($\mathrm{cts} \, \mathrm{s}^{-1}$)")


Out[4]:
PyObject <matplotlib.text.Text object at 0x32bcccb90>

In [5]:
post = Kalman.CARMAKalmanPosterior(data[:,1], data[:,2], data[:,3], 3, 2)
function logpost(x)
    lp = Kalman.log_prior(post, x)
    if lp == -Inf
        lp
    else
        lp + Kalman.log_likelihood(post, x)
    end
end


Out[5]:
logpost (generic function with 1 method)

In [6]:
pts = Kalman.init(post, 128)
lnprobs = EnsembleSampler.lnprobs(pts, logpost)


Out[6]:
128-element Array{Float64,1}:
 -41945.2      
 -62056.0      
 -35916.0      
 -25517.0      
     -4.08674e7
     -4.7375e7 
     -5.34167e5
 -44540.5      
 -31295.0      
 -34304.1      
     -2.8481e7 
     -1.85556e6
 -49191.9      
      ⋮        
     -1.34875e5
     -1.285e6  
 -29816.5      
     -6.25265e6
 -29948.4      
 -65657.0      
 -67040.4      
     -2.07928e6
     -1.69e5   
 -77016.3      
 -45134.6      
     -3.74378e6

In [7]:
ps_save = nothing
lnps_save = nothing

In [8]:
function neff_cb(ps, lnps, n, thin)
    println("Iterated by $(n)")
    println("  (max, mean, var)(log(L)) = $((maximum(lnps), mean(lnps), var(lnps)))")
    println("  max ACL = $(maximum(Acor.acl(ps)))")
    ps_save = ps
    lnps_save = lnps
end
@time ens, enslnps = EnsembleSampler.run_to_neff(pts, lnprobs, logpost, 16, callback=neff_cb)


Iterated by 128
  (max, mean, var)(log(L)) = (-21153.13147692399,-1.4257865329221431e6,4.643237346406703e15)
  max ACL = Inf
Iterated by 128
  (max, mean, var)(log(L)) = (-21082.43954426243,-21574.934408668218,555947.2682454146)
  max ACL = Inf
Iterated by 128
  (max, mean, var)(log(L)) = (-21040.562828933766,-21317.238220444808,334987.40247866197)
  max ACL = Inf
Iterated by 128
  (max, mean, var)(log(L)) = (-21012.708401071264,-21029.469341050455,92.60424830603714)
  max ACL = Inf
Iterated by 128
  (max, mean, var)(log(L)) = (-21011.638656434814,-21016.880417541608,6.371531188491934)
  max ACL = Inf
Iterated by 256
  (max, mean, var)(log(L)) = (-21011.62914359321,-21015.585643850347,4.223501624118955)
  max ACL = Inf
Iterated by 512
  (max, mean, var)(log(L)) = (-21011.53852763555,-21015.503683155694,4.143543136448006)
  max ACL = Inf
Iterated by 1024
  (max, mean, var)(log(L)) = (-21011.520760118827,-21015.568556392922,4.245965313296669)
  max ACL = 10.582183130320947
Iterated by 2048
  (max, mean, var)(log(L)) = (-21011.671480513603,-21015.547762156028,4.199277797526738)
  max ACL = 7.264376797806088
1276.106813 seconds (83.74 M allocations: 36.582 GB, 0.27% gc time)
Out[8]:
(
8x128x128 Array{Float64,3}:
[:, :, 1] =
     1.42069e6      1.41604e6      1.41964e6  …      1.41753e6      1.41888e6
 11916.2        10755.9        11894.6           12005.9        11547.0      
     0.64512        0.584016       0.867895          0.904343       0.773515 
     0.351102       0.595708       0.58033           0.438997       0.598434 
     3.11649        2.9741         2.85888           2.9987         3.01701  
     1.14416        1.41085        1.39084    …      0.988568       1.09991  
     3.15257        3.20547        3.26182           3.2486         3.21245  
     6.32672        6.36191        6.41024           6.26178        6.31968  

[:, :, 2] =
     1.41998e6      1.41684e6      1.41908e6  …      1.41809e6      1.41889e6
 12509.3        10282.1        11459.4           12665.2        11755.4      
     0.780701       0.571495       0.790028          0.920748       0.813251 
     0.382263       0.778257       0.625235          0.321371       0.628077 
     3.13111        2.95462        2.88142           2.97517        2.97734  
     0.916318       1.38792        1.43446    …      0.97345        1.13038  
     3.16979        3.15598        3.24528           3.2214         3.20138  
     6.31027        6.34793        6.39735           6.23705        6.33733  

[:, :, 3] =
     1.4191e6      1.41715e6      1.4182e6  …      1.41633e6      1.4186e6
 11799.2       10377.8        11020.1          11054.0        11710.2     
     0.939738      0.641394       0.643984         0.806615       0.82161 
     0.632481      0.737868       0.725173         0.666838       0.540411
     3.12989       2.97863        2.99037          3.08492        3.01614 
     0.763415      1.32446        1.19848   …      0.804153       1.03424 
     3.25407       3.17349        3.18399          3.19161        3.19626 
     6.34039       6.33821        6.3063           6.2575         6.28751 

...

[:, :, 126] =
     1.41902e6      1.41857e6      1.41945e6  …      1.41971e6      1.41864e6
 10958.6        11323.4        10578.8           10611.1        10969.9      
     1.20398        0.978678       0.845273          1.1716         0.640881 
     0.749406       0.347429       0.478493          0.711689       0.456458 
     3.12597        2.99342        3.11086           3.15442        3.00029  
     0.79912        1.17565        1.19038    …      0.827235       1.35729  
     3.31485        3.3019         3.33649           3.3868         3.17664  
     6.35828        6.28069        6.32689           6.38618        6.30281  

[:, :, 127] =
     1.41888e6      1.4172e6      1.41904e6  …      1.41903e6      1.41888e6
 11845.1        12742.2       11195.2           11119.4        10299.4      
     0.854483       1.18811       0.991272          1.12838        0.591708 
     0.648033       0.190978      0.36442           0.531514       0.609374 
     3.09035        3.08758       3.041             3.13614        2.97455  
     0.821061       0.760667      1.19947    …      0.902838       1.43445  
     3.21079        3.38484       3.38466           3.32812        3.1769   
     6.28088        6.28889       6.34988           6.38098        6.31333  

[:, :, 128] =
     1.41869e6      1.41732e6      1.41852e6  …      1.41896e6      1.41866e6
 11568.8        12147.7        10891.5           11123.8        10249.3      
     0.74626        1.09026        0.726339          1.07912        0.608279 
     0.597071       0.288636       0.420553          0.535          0.605229 
     3.05968        3.10628        3.08184           3.06151        3.01269  
     0.90552        0.904934       1.19365    …      1.05014        1.32314  
     3.22846        3.34781        3.28156           3.33177        3.19239  
     6.26244        6.33195        6.29334           6.3656         6.31802  ,

128x128 Array{Float64,2}:
 -21021.4  -21019.0  -21014.8  -21014.1  …  -21015.5  -21015.0  -21013.0
 -21019.1  -21017.2  -21015.3  -21020.6     -21013.2  -21017.0  -21014.8
 -21016.2  -21016.0  -21014.6  -21016.6     -21018.1  -21014.6  -21015.0
 -21013.4  -21014.3  -21016.9  -21016.5     -21017.3  -21017.1  -21015.6
 -21013.7  -21015.2  -21013.4  -21014.2     -21015.1  -21014.1  -21015.2
 -21016.0  -21015.4  -21013.5  -21014.5  …  -21015.2  -21015.7  -21016.4
 -21014.5  -21015.0  -21015.5  -21018.3     -21015.5  -21014.8  -21015.4
 -21014.4  -21014.8  -21018.3  -21018.0     -21017.5  -21014.8  -21013.9
 -21013.1  -21014.8  -21013.4  -21015.1     -21016.1  -21013.3  -21014.4
 -21016.4  -21016.2  -21014.3  -21015.5     -21017.7  -21015.3  -21015.6
 -21015.8  -21017.4  -21015.8  -21016.5  …  -21016.4  -21015.9  -21014.1
 -21015.6  -21019.6  -21016.1  -21014.5     -21013.3  -21014.0  -21013.4
 -21016.1  -21016.6  -21014.9  -21012.3     -21013.9  -21015.5  -21013.5
      ⋮                                  ⋱       ⋮                      
 -21013.6  -21015.4  -21014.2  -21013.5     -21013.9  -21016.0  -21014.4
 -21013.4  -21017.5  -21015.4  -21013.1     -21019.5  -21016.0  -21017.4
 -21016.6  -21014.7  -21013.7  -21014.2     -21015.1  -21015.0  -21021.2
 -21016.7  -21014.0  -21016.6  -21013.5     -21017.1  -21013.8  -21013.1
 -21015.7  -21013.2  -21014.1  -21015.8  …  -21014.6  -21016.5  -21015.0
 -21015.1  -21014.8  -21016.7  -21017.6     -21013.9  -21013.7  -21013.9
 -21015.7  -21014.9  -21014.6  -21017.6     -21014.8  -21013.7  -21013.9
 -21014.1  -21020.2  -21014.1  -21014.3     -21013.8  -21014.8  -21012.3
 -21014.5  -21015.4  -21014.6  -21014.5     -21014.9  -21013.9  -21016.9
 -21014.3  -21014.6  -21018.7  -21015.0  …  -21015.6  -21017.9  -21016.0
 -21013.8  -21016.0  -21020.1  -21017.3     -21014.4  -21014.0  -21012.0
 -21012.8  -21013.7  -21013.2  -21016.4     -21014.3  -21015.8  -21015.5)

The above run involved $\mathcal{O}(3000)$ ensemble steps, which is $\mathcal{O}(300000)$ likelihood evaluations. The equivalent nested sampling run retired $33792$ points, with each one taking $\mathcal{O}(100)$ likelihood evaluations, which is a factor of ten or more longer!


In [9]:
Acor.gelman_rubin_rs(ens)


Out[9]:
8-element Array{Float64,1}:
 1.04463
 1.05402
 1.12172
 1.06895
 1.04643
 1.06124
 1.08663
 1.05366

In [10]:
for i in 1:128
    plot(vec(enslnps[i,:]))
end



In [14]:
for i in 1:8
    subplot(3,3,i)
    plot(vec(mean(ens[i,:,:], 2)))
    for j in 1:10
        k = rand(1:128)
        plot(vec(ens[i,k,:]), alpha=0.1)
    end
end



In [19]:
r, sr = Kalman.residuals(post, ens[:,1,128])
plot(data[:,1], r./sr)
figure()
sns.kdeplot(r./sr)
xs = collect(-3:0.01:3)
plot(xs, 1.0/sqrt(2.0*pi)*exp(-0.5*xs.*xs))


Out[19]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x32ed5ad50>

In [21]:
ts = sort(vcat(data[:,1], collect(linspace(minimum(data[:,1]), maximum(data[:,1]), 1000))))
ys, vys = Kalman.predict(post, ens[:,1,128], ts)
plot(ts, ys)
plot(data[:,1], data[:,2], ".k")
fill_between(ts, ys+sqrt(vys), ys-sqrt(vys), alpha=0.5)


Out[21]:
PyObject <matplotlib.collections.PolyCollection object at 0x32f36b390>

In [25]:
fs = Kalman.psdfreq(post)
psds = Kalman.psd(post, reshape(ens, (size(ens,1), size(ens,2)*size(ens,3))), fs)
mid = zeros(size(psds,1))
low = zeros(size(psds,1))
high = zeros(size(psds,1))
for i in 1:size(psds,1)
    mid[i] = median(vec(psds[i,:]))
    low[i] = quantile(vec(psds[i,:]), 0.16)
    high[i] = quantile(vec(psds[i,:]), 0.84)
end
plot(fs, mid)
fill_between(fs, high, low, alpha=0.5)
xscale("log")
yscale("log")



In [ ]: