Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorDouven committed Dec 26, 2020
1 parent c32915f commit 182ab66
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 110 deletions.
199 changes: 156 additions & 43 deletions Ch3.ipynb

Large diffs are not rendered by default.

35 changes: 24 additions & 11 deletions Ch6.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@
"outputs": [],
"source": [
"# needed packages\n",
"@everywhere using Distributions, Bootstrap, Statistics, LinearAlgebra, SharedArrays\n",
"using DataFrames, HypothesisTests, DelimitedFiles, StatsBase, Colors, Gadfly, Cairo, Fontconfig, Compose"
"@everywhere begin\n",
" using Distributions\n",
" using Bootstrap\n",
" using Statistics\n",
" using LinearAlgebra\n",
" using SharedArrays\n",
"end\n",
"\n",
"using DataFrames\n",
"using HypothesisTests\n",
"using DelimitedFiles\n",
"using StatsBase\n",
"using Colors\n",
"using Gadfly\n",
"using Compose"
]
},
{
Expand Down Expand Up @@ -70,7 +83,7 @@
"outputs": [],
"source": [
"# EXPL\n",
"@everywhere function expl_upd(probs::Array{Float32,1}, dat::Array{Bool, 1}, toss_num::Int64, bonus::Float32=0.1)\n",
"@everywhere function expl_upd(probs::Array{Float32,1}, dat::Array{Bool,1}, toss_num::Int64, bonus::Float32=0.1)\n",
" val::Float32 = mean(dat[1:toss_num]) * 10 + 1\n",
" vec::Array{Float32,1} = if dat[toss_num] == true\n",
" @. (probs * likelihood_heads) / $dot(probs, likelihood_heads)\n",
Expand Down Expand Up @@ -253,26 +266,26 @@
" right = rand(Uniform(1, 10)) # effect of right intervention\n",
" wrong = rand(Uniform(1, 10)) # effect of wrong intervention\n",
"\n",
" data::Array{Bool, 1} = datFunc((rand_hyp - 1) / (numb_hyp - 1)) # generate synthetic data for this pick (the test results for the patient)\n",
" data::Array{Bool,1} = datFunc((rand_hyp - 1) / (numb_hyp - 1)) # generate synthetic data for this pick (the test results for the patient)\n",
"\n",
" updates = Array{Float32,2}(undef, numb_toss + 1, numb_hyp) # initialize array for probabilities\n",
"\n",
" updates[1, :] = prior # set prior\n",
"\n",
" if rule_index == 1.0f0\n",
" @fastmath @inbounds for t in 1:numb_toss # generate updates\n",
" @inbounds for t in 1:numb_toss # generate updates\n",
" updates[t + 1, :] = b_upd(updates[t, :], data, t)\n",
" end\n",
" elseif rule_index == 2.0f0\n",
" @fastmath @inbounds for t in 1:numb_toss # generate updates\n",
" @inbounds for t in 1:numb_toss # generate updates\n",
" updates[t + 1, :] = expl_upd(updates[t, :], data, t, c_value)\n",
" end\n",
" elseif rule_index == 3.0f0\n",
" @fastmath @inbounds for t in 1:numb_toss # generate updates\n",
" @inbounds for t in 1:numb_toss # generate updates\n",
" updates[t + 1, :] = good_upd(updates[t, :], data, t, c_value)\n",
" end\n",
" else\n",
" @fastmath @inbounds for t in 1:numb_toss # generate updates\n",
" @inbounds for t in 1:numb_toss # generate updates\n",
" updates[t + 1, :] = pop_upd(updates[t, :], data, t, c_value)\n",
" end\n",
" end\n",
Expand Down Expand Up @@ -394,9 +407,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.4.0",
"display_name": "Julia 1.5.3",
"language": "julia",
"name": "julia-1.4"
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
Expand All @@ -407,4 +420,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
28 changes: 22 additions & 6 deletions Ch7Sct3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@
"outputs": [],
"source": [
"using Distributed\n",
"addprocs(12); # adjust depending on the number of cores available"
"addprocs(...); # set number depending on number of cores available on your computer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@everywhere begin\n",
" using Distributions\n",
" using StatsBase\n",
" using Distances\n",
" using LinearAlgebra\n",
" using SharedArrays\n",
" using JuliennedArrays\n",
" using Random\n",
" using DelimitedFiles\n",
"end"
]
},
{
Expand All @@ -30,8 +48,6 @@
"metadata": {},
"outputs": [],
"source": [
"@everywhere using Distributions, StatsBase, Distances, LinearAlgebra, SharedArrays, JuliennedArrays, Random, DelimitedFiles\n",
"\n",
"@everywhere begin\n",
" const numb_hyp = 11\n",
" const numb_agent = 50\n",
Expand Down Expand Up @@ -802,9 +818,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.1",
"display_name": "Julia 1.5.3",
"language": "julia",
"name": "julia-1.3"
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
Expand All @@ -815,4 +831,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
109 changes: 59 additions & 50 deletions Ch7Sct4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@
"metadata": {},
"outputs": [],
"source": [
"using Gadfly, Colors, Cairo, DataFrames, Distributed\n",
"addprocs(10);"
"using Gadfly\n",
"using Colors\n",
"using DataFrames\n",
"using Distributed\n",
"addprocs(...); # set number depending on number of cores available on your computer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@everywhere begin\n",
" using Distributions\n",
" using StatsBase\n",
" using Distances\n",
" using LinearAlgebra\n",
" using SharedArrays\n",
" using JuliennedArrays\n",
" using Random\n",
" using Bootstrap\n",
" using DelimitedFiles\n",
"end"
]
},
{
Expand All @@ -16,8 +38,6 @@
"metadata": {},
"outputs": [],
"source": [
"@everywhere using Distributions, StatsBase, Distances, LinearAlgebra, SharedArrays, JuliennedArrays, Random, Bootstrap, DelimitedFiles\n",
"\n",
"@everywhere begin\n",
" const numb_hyp = 11\n",
" const numb_agent = 50\n",
Expand All @@ -34,10 +54,6 @@
"metadata": {},
"outputs": [],
"source": [
"# default graphics\n",
"Gadfly.push_theme(:default)\n",
"set_default_plot_size(9inch, 9inch/MathConstants.golden)\n",
"\n",
"function gen_brew_colors(n) # to create your own colors, here based on one of the brewer series\n",
" cs = distinguishable_colors(n, \n",
" [colorant\"#66c2a5\", colorant\"#fc8d62\", colorant\"#8da0cb\", colorant\"#e78ac3\",\n",
Expand Down Expand Up @@ -212,21 +228,44 @@
"source": [
"@everywhere function survWei(res::Tuple{Int64,Int64},\n",
" hyp::Int64,\n",
" a::Float64=rand(Uniform(1, 10)), \n",
" a::Float64=rand(Uniform(1, 10)),\n",
" b::Float64=rand(Uniform(1, 10)),\n",
" shape::Float64=rand(Uniform(.5, 5)), \n",
" shape::Float64=rand(Uniform(.5, 5)),\n",
" scale::Float64=rand(Uniform(50, 250)))\n",
" \n",
" # cdf(Weibull(shape, scale), p) below gives the probability of death at the relevant time\n",
" \n",
"\n",
" p = first(res)\n",
"\n",
" if last(res) == hyp\n",
" 1 - (cdf(Weibull(shape, scale), p) / a) # probability goes down if right intervention is made (which is made when the truth is assigned a probability above thresh)\n",
" elseif last(res) == -1\n",
" 1 - cdf(Weibull(shape, scale), numb_toss + 1) # if no intervention is made, output survival probability at last time step\n",
" else\n",
" (1 + (b - 1) * cdf(Weibull(shape, scale), p)) / b # probability goes down if wrong intervention is made (which happens if a false hypothesis is assigned a probabilty above thresh)\n",
" (1 - cdf(Weibull(shape, scale), p)) / b # probability goes down if wrong intervention is made (which happens if a false hypothesis is assigned a probabilty above thresh)\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@everywhere function survGam(res::Tuple{Int64,Int64},\n",
" hyp::Int64,\n",
" a::Float64=rand(Uniform(1, 10)),\n",
" b::Float64=rand(Uniform(1, 10)),\n",
" shape::Float64=rand(Uniform(10, 16)),\n",
" scale::Float64=rand(Uniform(10, 16)))\n",
"\n",
" p = first(res)\n",
"\n",
" if last(res) == hyp\n",
" 1 - (cdf(Gamma(shape, scale), p) / a) # probability goes down if right intervention is made (which is made when the truth is assigned a probability above thresh)\n",
" elseif last(res) == -1\n",
" 1 - cdf(Gamma(shape, scale), numb_toss + 1) # if no intervention is made, output survival probability at last time step\n",
" else\n",
" (1 - cdf(Gamma(shape, scale), p)) / b # probability goes down if wrong intervention is made (which happens if a false hypothesis is assigned a probabilty above thresh)\n",
" end\n",
"end"
]
Expand Down Expand Up @@ -403,17 +442,7 @@
" Guide.xlabel(\"Rule\"),\n",
" Guide.ylabel(\"Count\"),\n",
" Scale.x_discrete,\n",
" style(default_color=colorant\"#66c2a5\", minor_label_font_size=11pt, minor_label_color=colorant\"black\", grid_color=colorant\"#E8E8E8\", minor_label_font=\"DeJaVu Sans\", major_label_font=\"DeJaVu Sans\", major_label_font_size=15pt, major_label_color=colorant\"black\",\n",
" bar_spacing=35pt))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"draw(PDF(\"barplot_addendum1.pdf\", 7inch, 7inch/MathConstants.golden), p);"
" style(bar_spacing=35pt))"
]
},
{
Expand Down Expand Up @@ -462,20 +491,7 @@
" Coord.cartesian(xmin=-.0075, xmax=1.1),\n",
" Scale.color_discrete_manual(hcat(gen_brew_colors(4)[2:4][1], gen_brew_colors(4)[2:4][3])...),\n",
" Guide.xlabel(\"Bonus value\"),\n",
" Guide.ylabel(\"Density\"),\n",
" style(line_width=2.65pt, minor_label_font_size=11pt, minor_label_color=colorant\"black\", minor_label_font=\"DeJaVu Sans\", key_title_font_size=14pt, \n",
" key_label_color=colorant\"black\", grid_color=colorant\"#E8E8E8\", key_title_color=colorant\"black\", key_label_font=\"DeJaVu Sans\", \n",
" major_label_font=\"DeJaVu Sans\", key_label_font_size=12pt, major_label_font_size=15pt, major_label_color=colorant\"black\",\n",
" colorkey_swatch_shape=:square))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"draw(PDF(\"densplot_addendum1.pdf\", 7inch, 7inch/MathConstants.golden), p);"
" Guide.ylabel(\"Density\"))"
]
},
{
Expand Down Expand Up @@ -718,7 +734,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Same for $\\epsilon$ values."
"Same for ϵ values."
]
},
{
Expand Down Expand Up @@ -757,20 +773,13 @@
" writedlm(io, eps_gen)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.1",
"display_name": "Julia 1.5.3",
"language": "julia",
"name": "julia-1.3"
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
Expand All @@ -781,4 +790,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit 182ab66

Please sign in to comment.