In [1]:
using Gadfly
using DataFrames
using Distributions
using Compose


INFO: Precompiling module Compose.
INFO: Recompiling stale cache file C:\Users\pkillewald\.julia\lib\v0.6\Gadfly.ji for module Gadfly.

In [2]:
function simulate(n::Int, p_occupied::Number, p_conscious::Number)
    occupied = rand(Bool, n)
    sgn = falses(n)
    
    r = rand(Float64, n)
    conscious = (r .< p_conscious)
    occed = (r .< p_conscious + p_occupied) .& .~conscious
    
    if occupied[1] && (conscious[1] || occed[1])
        sgn[1] = true
    end
    
    for i in 2:n
        if occupied[i] && (conscious[i] || occed[i])
            sgn[i] = true
        elseif ~occupied[i] && conscious[i-1]
            sgn[i] = false
        else
            sgn[i] = sgn[i-1]
        end
    end
    return (occupied, sgn, r, conscious, occed)
end


Out[2]:
simulate (generic function with 1 method)

In [3]:
N = 1000000
M = 100
occ_given_occ = zeros(Float64, M)
occ_given_upper = zeros(Float64, M)
occ_given_lower = zeros(Float64, M)
vac_given_vac = zeros(Float64, M)
vac_given_upper = zeros(Float64, M)
vac_given_lower = zeros(Float64, M)
X = collect(float(linspace(0, 2//3, M)))
target_x = findfirst(x -> x>=1//3, X)
for (i, p) in enumerate(X)
    occ, sgn = simulate(N, float(1//3), p)
    
    occ_bin = Binomial(N, sum(occ .& sgn) / N)
    occ_given_occ[i] = mean(occ_bin) / N
    occ_given_upper[i] = quantile(occ_bin, .9) / N
    occ_given_lower[i] = quantile(occ_bin, .1) / N
    
    vac_bin = Binomial(N, sum(.~occ .& .~sgn) / N)
    vac_given_vac[i] = mean(vac_bin) / N
    vac_given_upper[i] = quantile(vac_bin, .9) / N
    vac_given_lower[i] = quantile(vac_bin, .1) / N
end

In [4]:
df1 = DataFrame(x=X, y=occ_given_occ, yu=occ_given_upper, yl=occ_given_lower, t=repeat(["p(Occ|S=Occ)"], outer=[M]))
append!(df1, DataFrame(x=X, y=vac_given_vac, yu=vac_given_upper, yl=vac_given_lower, t=repeat(["p(Vac|S=Vac)"], outer=[M])))


Out[4]:
xyyuylt
10.00.4994480.5000890.498807p(Occ|S=Occ)
20.0067340067340067350.4943350.4949760.493694p(Occ|S=Occ)
30.013468013468013470.4870530.4876940.486412p(Occ|S=Occ)
40.02020202020202020.4830230.4836630.482383p(Occ|S=Occ)
50.026936026936026940.4790140.4796540.478374p(Occ|S=Occ)
60.033670033670033670.4724590.4730990.471819p(Occ|S=Occ)
70.04040404040404040.4698480.4704880.469208p(Occ|S=Occ)
80.047138047138047130.4647370.4653760.464098p(Occ|S=Occ)
90.053872053872053880.4605880.4612270.459949p(Occ|S=Occ)
100.060606060606060610.4582640.4589030.457625p(Occ|S=Occ)
110.067340067340067340.4557160.4563540.455078p(Occ|S=Occ)
120.074074074074074070.4519240.4525620.451286p(Occ|S=Occ)
130.08080808080808080.450340.4509780.449702p(Occ|S=Occ)
140.087542087542087550.4476130.448250.446976p(Occ|S=Occ)
150.094276094276094260.4445970.4452340.44396p(Occ|S=Occ)
160.101010101010101010.4437920.4444290.443155p(Occ|S=Occ)
170.107744107744107750.4423030.442940.441667p(Occ|S=Occ)
180.114478114478114470.4391830.4398190.438547p(Occ|S=Occ)
190.121212121212121220.4394070.4400430.438771p(Occ|S=Occ)
200.127946127946127920.4379770.4386130.437341p(Occ|S=Occ)
210.134680134680134680.4369280.4375640.436292p(Occ|S=Occ)
220.14141414141414140.4353090.4359440.434674p(Occ|S=Occ)
230.148148148148148140.4354730.4361080.434838p(Occ|S=Occ)
240.154882154882154870.4339390.4345740.433304p(Occ|S=Occ)
250.16161616161616160.4342210.4348560.433586p(Occ|S=Occ)
260.168350168350168360.4333450.433980.43271p(Occ|S=Occ)
270.17508417508417510.4329350.433570.4323p(Occ|S=Occ)
280.18181818181818180.4329260.4335610.432291p(Occ|S=Occ)
290.188552188552188530.4329030.4335380.432268p(Occ|S=Occ)
300.195286195286195290.431910.4325450.431275p(Occ|S=Occ)
&vellip&vellip&vellip&vellip&vellip&vellip

In [5]:
myplot = plot(df1, x=:x, y=:y, color=:t, ymin=:yl, ymax=:yu, xintercept=[1//3],
    Geom.smooth,
    Geom.ribbon,
    Geom.vline(color=[colorant"red"], style=[[1mm, 1mm]]),
    Guide.xlabel("p_conscious"), Guide.ylabel("prob"), Guide.colorkey(""),
    Scale.x_continuous(minvalue=0, maxvalue=2/3),
    Guide.annotation(
         compose(context(), text(1//3, occ_given_occ[target_x], string(occ_given_occ[target_x])))),
    Guide.annotation(
         compose(context(), text(1//3, vac_given_vac[target_x], string(vac_given_vac[target_x]))))
)


Out[5]:
p_conscious -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 -1 0 1 2 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 p(Occ|S=Occ) p(Vac|S=Vac) 0.295211 0.4414 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 -0.62 -0.60 -0.58 -0.56 -0.54 -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30 -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06 -0.04 -0.02 0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84 0.86 0.88 0.90 0.92 0.94 0.96 0.98 1.00 1.02 1.04 1.06 1.08 1.10 1.12 1.14 1.16 1.18 1.20 1.22 -1 0 1 2 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 prob

In [10]:
draw(PNG("solution.png", 5inch, 4inch), myplot)