Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdfish committed Feb 18, 2024
1 parent 60e76dd commit 888f9f2
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/Manifest.toml
*Manifest.toml
temp.jl
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AdaptiveDesignOptimization"
uuid = "5f210a59-4c6a-4c2f-87d5-5cd98d51789a"
authors = ["itsdfish"]
version = "0.1.3"
version = "0.1.4"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
17 changes: 17 additions & 0 deletions examples/delay_discounting/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[deps]
AdaptiveDesignOptimization = "5f210a59-4c6a-4c2f-87d5-5cd98d51789a"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Pigeons = "0eb8d820-af6a-4919-95ae-11206f830c31"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
DataFrames = "1"
Distributions = "0.25.0"
Random = "1"
Revise = "3"
StatsPlots = "0.15.0"
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ cd(@__DIR__)
# load the package manager
using Pkg
# activate the project environment
Pkg.activate("../../")
using Revise, AdaptiveDesignOptimization, Random, Distributions
Pkg.activate("")
using Revise
using AdaptiveDesignOptimization
using Distributions
using Random
includet("Delay_Discounting.jl")
#######################################################################################
# Define Experiment Design
#######################################################################################
Random.seed!(120341)
prior = [Uniform(-5, 5), Uniform(-5, 50)]

model = Model(;prior, loglike)
model = Model(; prior, loglike)

parm_list == range(-5, 0, length=50) .|> x->10^x,
τ = range(0, 5, length=11)[2:end])
parm_list = (
κ = range(-5, 0, length=50) .|> x->10^x,
τ = range(0, 5, length=11)[2:end]
)

# parm_list = (κ = [.1],
# τ = [.2,.5])
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions examples/monetary_gambles/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[deps]
AdaptiveDesignOptimization = "5f210a59-4c6a-4c2f-87d5-5cd98d51789a"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
UtilityModels = "bddc258c-9485-48da-848b-3e4c1ee8966c"

[compat]
DataFrames = "1"
Distributions = "0.25.0"
Revise = "3"
StatsPlots = "0.15.0"
UtilityModels = "0.3.0"
Random = "1"
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ cd(@__DIR__)
# load the package manager
using Pkg
# activate the project environment
Pkg.activate("../../")
using Revise, AdaptiveDesignOptimization, Random, UtilityModels, Distributions
Pkg.activate("")
using Revise
using AdaptiveDesignOptimization
using Distributions
using Random
using UtilityModels
includet("TAX_Model.jl")
Random.seed!(25974)
#######################################################################################
Expand All @@ -26,8 +30,8 @@ parm_list = (
dist = Normal(0,10)
n_vals = 3
n_choices = 2
design_vals = map(x->random_design(dist, n_vals, n_choices), 1:1000)
filter!(x->abs_zscore(x) .4, design_vals)
design_vals = map(x -> random_design(dist, n_vals, n_choices), 1:1000)
filter!(x -> abs_zscore(x) .4, design_vals)
design_names = (:p1,:v1,:p2,:v2)
design_list = (design_names,design_vals[1:100])

Expand Down
File renamed without changes.
File renamed without changes
20 changes: 10 additions & 10 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Computes a grid of prior probabilities over model parameters.
- `parm_grid`: a grid of parameter values
"""
function prior_probs(prior, parm_grid)
dens = [mapreduce((θ,d)->pdf(d, θ), *, g, prior) for g in parm_grid]
return dens/sum(dens)
dens = [mapreduce((θ,d) -> pdf(d, θ), *, g, prior) for g in parm_grid]
return dens / sum(dens)
end

"""
Expand All @@ -32,7 +32,7 @@ Computes a grid of uniform prior probabilities over model parameters.
- `parm_grid`: a grid of parameter values
"""
function prior_probs(prior::Nothing, parm_grid)
return fill(1/length(parm_grid), size(parm_grid))
return fill(1 / length(parm_grid), size(parm_grid))
end

"""
Expand Down Expand Up @@ -200,11 +200,11 @@ end
# """
# function marginal_posterior(optimizer)
# @unpack posteriors = optimizer
# return map(d->sum(posterior, dims=d), ndims(posterior):-1:1)
# return map(d-> sum(posterior, dims=d), ndims(posterior):-1:1)
# end

function compute_entropy(log_like)
return -1*sum(exp.(log_like) .* log_like, dims=3)[:,:]
return -1 * sum(exp.(log_like) .* log_like, dims=3)[:,:]
end

function conditional_entropy(entropy, post)
Expand All @@ -223,7 +223,7 @@ function marginal_entropy!(optimizer::Optimizer)
end

function marginal_entropy(marg_log_like)
return -sum(exp.(marg_log_like).*marg_log_like, dims=3)[:]
return -sum(exp.(marg_log_like) .* marg_log_like, dims=3)[:]
end

"""
Expand Down Expand Up @@ -344,7 +344,7 @@ function to_grid(vals::NamedTuple)
end

function to_grid(vals)
k = [Symbol(string("v",i)) for i in 1:length(vals[1])]
k = [Symbol(string("v", i)) for i in 1:length(vals[1])]
return (k...,),vals
end

Expand All @@ -368,12 +368,12 @@ function mean_post(optimizer)
end

function mean_post(post, parm_grid)
return mapreduce((p,v)->p.*v, .+, post, parm_grid)
return mapreduce((p,v) -> p .* v, .+, post, parm_grid)
end

function std_post(post, parm_grid)
mu = mean_post(post, parm_grid)
return mapreduce((p,v)->p.*(v .- mu).^2, .+, post, parm_grid) .|> sqrt
return mapreduce((p,v) -> p .* (v .- mu).^2, .+, post, parm_grid) .|> sqrt
end

function std_post(optimizer)
Expand Down Expand Up @@ -415,7 +415,7 @@ end
# end

# # function update_model_posterior(ps, es)
# # map(i->ps[i]./(sum(ps.*(es./es[i]))),1:length(ps))
# # map(i-> ps[i]./(sum(ps.*(es./es[i]))),1:length(ps))
# # end

# using Distributions
Expand Down
12 changes: 10 additions & 2 deletions src/structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ mutable struct Model{F<:Function,P}
end

function Model(args...; prior=nothing, loglike, kwargs...)
return Model(prior, (x...)->loglike(x..., args...; kwargs...))
return Model(prior, (x...) -> loglike(x..., args...; kwargs...))
end

"""
Optimizer(;task, model, grid_design, grid_parms, grid_response)
`Optimizer` constructs a model object for adaptive design optimization
# Fields
- `model`: a model object
- `grid_design`:a grid of design parameters
- `grid_parms`: a grid of model parameters
Expand All @@ -74,7 +76,13 @@ end
- `priors`: a multidimensional array of prior probabilities for parameters
- `log_post`: a one dimensional array of log posterior probabilities for parameters
- `entropy`: a two dimensional array of entropy values for parameter and design combinations
-
- `marg_entropy`:
- `cond_entropy`:
- `mutual_info`:
- `best_design`:
- `parm_names`:
- `model_state`:
- `update_state!`:
"""
mutable struct Optimizer{A,MT,M<:Model,T1,T2,T3,T4,T5,T6,T7,T8,T9,
T10,T11}
Expand Down

0 comments on commit 888f9f2

Please sign in to comment.