Skip to content

Commit

Permalink
renamed most files to simpler things; test and tested notebooks; remo…
Browse files Browse the repository at this point in the history
…ved old dx bins methods and types; cut commented sections in notebooks;
  • Loading branch information
briandepasquale committed Aug 30, 2024
1 parent 9a0cb9c commit 8a1ad7a
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 263 deletions.
Binary file modified examples/data/example_model.mat
Binary file not shown.
20 changes: 0 additions & 20 deletions examples/fit_choice_synthetic_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,6 @@
"Create an instance of the `choiceoptions` composite type, using `lb`, `ub`, and `fit`. `choiceoptions` specifies which parameters to fit, and the lower and upper bounds of the optimization."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"#options = choiceoptions(fit=fit, lb=lb, ub=ub)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#options = choiceoptions(lb=vcat([0., 2., -5., 0., 0., 0., 0.005, -5.0, 0.0]),\n",
"# ub = [30., 100., 5., 200., 10., 1.2, 1., 5.0, 1.0], \n",
"# fit = trues(9))"
]
},
{
"cell_type": "code",
"execution_count": 16,
Expand Down
138 changes: 59 additions & 79 deletions examples/fit_joint_synthetic_data.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/fit_neural_real_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"(numbers below include barrier contribution)\n",
"Iter Function value Gradient norm \n",
" 0 1.214329e+05 9.804548e+02\n",
" * time: 0.029542207717895508\n",
" * time: 0.015299797058105469\n",
"\n",
"Exiting inner optimizer with x = [0.16252272717900118, 20.03605663315885, 0.14271078200888496, 20.009599383205845, 1.0937330142867439, 0.7755103122118522, 0.9654754019793073, 1.7487100729426448, -0.5224949966918031, 1.3023422724530407, 0.6142154691698731, 1.272268011036716, 0.3360566806700428]\n",
"Current distance to box: 0.0345246\n",
Expand Down Expand Up @@ -360,7 +360,7 @@
" |g(x)| = 2.98e+01 ≰ 1.0e-03\n",
"\n",
" * Work counters\n",
" Seconds run: 28 (vs limit 170000)\n",
" Seconds run: 45 (vs limit 170000)\n",
" Iterations: 1\n",
" f(x) calls: 2\n",
" ∇f(x) calls: 2\n",
Expand Down
67 changes: 14 additions & 53 deletions examples/loading_and_saving.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,18 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Distributed\n",
"addprocs(8);"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: method definition for CIs at /Users/briandepasquale/Documents/GitHub/PulseInputDDM.jl/src/base_model.jl:13 declares type variable T but does not use it.\n",
"WARNING: method definition for loglikelihood at /Users/briandepasquale/Documents/GitHub/PulseInputDDM.jl/src/neural_model/RBF_model.jl:326 declares type variable T2 but does not use it.\n"
"WARNING: method definition for CIs at /Users/briandepasquale/Documents/GitHub/PulseInputDDM.jl/src/utils.jl:9 declares type variable T but does not use it.\n",
"WARNING: method definition for loglikelihood at /Users/briandepasquale/Documents/GitHub/PulseInputDDM.jl/src/neural/RBF_model.jl:326 declares type variable T2 but does not use it.\n"
]
}
],
"source": [
"@everywhere using PulseInputDDM, Flatten"
"using PulseInputDDM"
]
},
{
Expand All @@ -40,34 +30,25 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"θ_syn = θchoice(θz=θz(σ2_i = 1., B = 13., λ = -0.5, σ2_a = 10., σ2_s = 1.0,\n",
" ϕ = 0.4, τ_ϕ = 0.02), bias=0.1, lapse=0.1);"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"xgen = collect(Flatten.flatten(θ_syn));"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate 20K trials of the synthetic data using those parameters. change `rng` to get a different set with the same parameters. `dt` specifies the temporal binning of the data. `1e-2` has worked well."
"Generate 80 trials of the synthetic data using those parameters. change `rng` to get a different set with the same parameters. `dt` specifies the temporal binning of the data. `1e-2` has worked well."
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -76,14 +57,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"choiceDDM{θchoice{θz{Float64}, Float64}}\n",
" θ: θchoice{θz{Float64}, Float64}\n",
"choiceDDM{θchoice{θz{Float64}, Float64, Float64}}\n",
" θ: θchoice{θz{Float64}, Float64, Float64}\n",
" n: Int64 53\n",
" cross: Bool false\n",
" fit: Array{Bool}((9,)) Bool[1, 1, 1, 1, 1, 1, 1, 1, 1]\n",
Expand All @@ -99,17 +80,6 @@
"model = choiceDDM(θ=θ_syn)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"#options = choiceoptions(lb=vcat([0., 2., -5., 0., 0., 0., 0.005, -5.0, 0.0]),\n",
"# ub = [30., 100., 5., 200., 10., 1.2, 1., 5.0, 1.0], \n",
"# fit = trues(9))"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -122,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -140,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -157,16 +127,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import PulseInputDDM: save_choice_data"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -175,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -203,7 +164,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand Down
36 changes: 18 additions & 18 deletions src/PulseInputDDM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ export train_and_test, all_Softplus, save_choice_data
export load_neural_data

include("types.jl")
include("choice_model/types.jl")
include("neural_model/types.jl")
include("neural-choice_model/types.jl")
include("choice/types.jl")
include("neural/types.jl")
include("joint/types.jl")

include("base_model.jl")
include("core.jl")
include("utils.jl")
include("optim_funcs.jl")
include("sample_model.jl")
include("optimization.jl")
include("sampling.jl")

include("choice_model/choice_model.jl")
include("choice_model/sample_model.jl")
include("choice_model/process_data.jl")
include("choice_model/IO.jl")
include("choice/choice.jl")
include("choice/sampling.jl")
include("choice/preprocessing.jl")
include("choice/IO.jl")

include("neural_model/neural_model.jl")
include("neural_model/sample_model.jl")
include("neural_model/process_data.jl")
include("neural_model/initalize.jl")
include("neural_model/RBF_model.jl")
include("neural/neural.jl")
include("neural/sampling.jl")
include("neural/preprocessing.jl")
include("neural/initalize.jl")
include("neural/RBF_model.jl")

include("neural-choice_model/neural-choice_model.jl")
include("neural-choice_model/sample_model.jl")
include("neural-choice_model/neural-choice_model-ALT.jl")
include("joint/joint.jl")
include("joint/sampling.jl")
include("joint/joint-ALT.jl")

end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
64 changes: 0 additions & 64 deletions src/base_model.jl → src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@
const dimz = 7


"""
CIs(H)
Given a Hessian matrix `H`, compute the 2 std confidence intervals based on the Laplace approximation.
If `H` is not positive definite (which it should be, but might not be due numerical round off, etc.) compute
a close approximation to it by adding a correction term. The magnitude of this correction is reported.
"""
function CIs(H::Array{Float64,2}) where T <: DDM

HPSD = Matrix(cholesky(Positive, H, Val{false}))

if !isapprox(HPSD,H)
norm_ϵ = norm(HPSD - H)/norm(H)
@warn "Hessian is not positive definite. Approximated by closest PSD matrix.
||ϵ||/||H|| is $norm_ϵ"
end

CI = 2*sqrt.(diag(inv(HPSD)))

return CI, HPSD

end


"""
P, M, xc, dx = initialize_latent_model(σ2_i, B, λ, σ2_a, n, dt)
Expand Down Expand Up @@ -74,18 +49,6 @@ function initialize_latent_model(σ2_i::TT, B::TT, λ::TT, σ2_a::TT,
end


function initialize_latent_model(σ2_i::TT, B::TT, λ::TT, σ2_a::TT,
dx::Float64, dt::Float64) where {TT <: Any}

xc,n = bins(B,dx)
P = P0(σ2_i,n,dx,xc,dt)
M = transition_M(σ2_a*dt,λ,zero(TT),dx,xc,n,dt)

return P, M, xc, n

end


"""
P0(σ2_i, n dx, xc, dt)
Expand Down Expand Up @@ -215,33 +178,6 @@ function bins(B::TT, n::Int) where {TT}
end


"""
bins(B, dx)
Computes the bin center locations and number of bins, given the boundary and desired (average) bin spacing.
### Examples
```jldoctest
julia> xc,n = pulse_input_DDM.bins(10.,0.25)
([-10.25, -9.75, -9.5, -9.25, -9.0, -8.75, -8.5, -8.25, -8.0, -7.75 … 7.75, 8.0, 8.25, 8.5, 8.75, 9.0, 9.25, 9.5, 9.75, 10.25], 81)
```
"""
function bins(B::TT, dx::Float64) where {TT}

xc = collect(0.:dx:floor(value(B)/dx)*dx)

if xc[end] == B
xc = vcat(xc[1:end-1], B + dx)
else
xc = vcat(xc, 2*B - xc[end])
end

xc = vcat(-xc[end:-1:2], xc)
n = length(xc)

return xc, n

end


"""
expm1_div_x(x)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 0 additions & 19 deletions src/neural_model/RBF_model.jl → src/neural/RBF_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,6 @@ mutable struct θμ_RBF{T1} <: DDMθ
end



function train_and_test(data, options::μ_RBF_options; seed::Int=1, nRBFs = 2:10)

ntrials = length(data)
train = sample(Random.seed!(seed), 1:ntrials, ceil(Int, 0.9 * ntrials), replace=false)
test = setdiff(1:ntrials, train)

ncells = options.ncells;
model = pmap(nRBF-> optimize([data[train]], μ_RBF_options(ncells=ncells, nRBFs=nRBF); show_trace=false)[1], nRBFs)
testLL = map(model-> loglikelihood(model.θ, [data[test]]), model)

return nRBFs, model, testLL

end


function optimize(data, options::μ_RBF_options;
x_tol::Float64=1e-10, f_tol::Float64=1e-6, g_tol::Float64=1e-3,
iterations::Int=Int(2e3), show_trace::Bool=true,
Expand Down Expand Up @@ -301,9 +285,6 @@ function loglikelihood(x::Vector{T1}, data::Vector{Vector{T2}}, θ::θμ_RBF) wh
end





"""
"""
function θμ_RBF(x::Vector{T}, ncells::Vector{Int}, nRBFs::Int) where {T <: Real}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,4 @@ end
nT::Int
nL::Vector{Int}
nR::Vector{Int}
end


@with_kw struct bins
#clicks::T
xc::Vector{Real}
dx::Real
n::Int
end
Loading

0 comments on commit 8a1ad7a

Please sign in to comment.