Created
January 18, 2023 09:23
-
-
Save JeffreySarnoff/901b6819bbc4790bdceacd004af314f6 to your computer and use it in GitHub Desktop.
Optim test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# very small subset of data, for testing | |
N = 5 # people | |
T = 3 # times | |
data = Float32.([ | |
# x y z | |
1.75 0.0 0.75 # person 1 time 1 | |
2.75 1.75 0.75 # person 1 time 2 | |
1.25 2.75 -0.75 # person 1 time 3 | |
1.75 0.0 0.75 # person 2 time 1 | |
2.0 1.75 0.75 # person 2 time 2 | |
1.75 2.0 -0.75 # person 2 time 3 | |
2.25 0.0 0.75 # person 3 time 1 | |
1.25 2.25 -0.75 # person 3 time 2 | |
1.5 1.25 0.75 # person 3 time 3 | |
2.0 0.0 -0.75 # person 4 time 1 | |
1.75 2.0 -0.75 # person 4 time 2 | |
1.5 1.75 0.75 # person 4 time 3 | |
1.25 0.0 0.75 # person 5 time 1 | |
2.25 1.25 -0.75 # person 5 time 2 | |
1.0 2.25 0.75 # person 5 time 3 | |
]); | |
# each person for all the times | |
person1 = data[1:3,:]; | |
person2 = data[4:6,:]; | |
person3 = data[7:9,:]; | |
person4 = data[10:12,:]; | |
person5 = data[13:15,:]; | |
persons = [person1,person2,person3,person4,person5]; | |
# each time for all the people | |
time1 = data[[1,4,7,10,13],:]; | |
time2 = data[[1+1,4+1,7+1,10+1,13+1],:]; | |
time3 = data[[1+2,4+2,7+2,10+2,13+2],:]; | |
times = [time1,time2,time3]; | |
# xs ys zs for all people all times | |
xs = data[:,1]; | |
ys = data[:,2]; | |
zs = data[:,3]; | |
# optimization | |
using Optim, LinearAlgebra, StatsBase | |
using Optim:minimizer | |
begin | |
function f(β, α, xi) | |
dot(α, xi) * sum(β)/T | |
end; | |
function get_β(α, x) | |
to_min = β -> begin | |
sum(f.(Ref(β), Ref(α), x)) | |
end | |
β_0 = zeros(Float32,N) | |
res = optimize(to_min, β_0) | |
return minimizer(res) | |
end; | |
function get_α(x) | |
to_min = α -> begin | |
β = get_β(α, x) | |
# negative cause we want to max | |
-mean(f.(Ref(β), Ref(α), x)) | |
end | |
α_0 = zeros(Float32,T) | |
res = optimize(to_min, α_0) | |
return minimizer(res) | |
end | |
end | |
optimal_βs = get_β(get_α(persons), persons) | |
ERROR: DimensionMismatch: x and y are of different lengths! | |
Stacktrace: | |
[1] dot | |
@ C:\Users\MrJSa\AppData\Local\Programs\Julia-1.10\share\julia\stdlib\v1.10\LinearAlgebra\src\generic.jl:871 [inlined] | |
[2] f(β::Vector{Float32}, α::Vector{Float32}, xi::Float32) | |
@ Main .\REPL[70]:3 | |
[3] _broadcast_getindex_evalf | |
@ .\broadcast.jl:683 [inlined] | |
[4] _broadcast_getindex | |
@ .\broadcast.jl:656 [inlined] | |
[5] getindex | |
@ .\broadcast.jl:610 [inlined] | |
[6] copy | |
@ .\broadcast.jl:912 [inlined] | |
[7] materialize | |
@ .\broadcast.jl:873 [inlined] | |
[8] (::var"#14#16"{Vector{Float32}, Vector{Float32}})(β::Vector{Float32}) | |
@ Main .\REPL[70]:8 | |
[9] value!!(obj::NonDifferentiable{Float32, Vector{Float32}}, x::Vector{Float32}) | |
@ NLSolversBase C:\Users\MrJSa\.julia\packages\NLSolversBase\kavn7\src\interface.jl:9 | |
[10] initial_state(method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, Nothing}, d::NonDifferentiable{Float32, Vector{Float32}}, initial_x::Vector{Float32}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\solvers\zeroth_order\nelder_mead.jl:171 | |
[11] optimize(d::NonDifferentiable{Float32, Vector{Float32}}, initial_x::Vector{Float32}, method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, Nothing}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\optimize.jl:36 | |
[12] optimize(f::Function, initial_x::Vector{Float32}; inplace::Bool, autodiff::Symbol, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\interface.jl:90 | |
[13] optimize(f::Function, initial_x::Vector{Float32}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\interface.jl:83 | |
[14] get_β(α::Vector{Float32}, x::Vector{Float32}) | |
@ Main .\REPL[70]:11 | |
[15] (::var"#15#17"{Vector{Float32}})(α::Vector{Float32}) | |
@ Main .\REPL[70]:17 | |
[16] value!!(obj::NonDifferentiable{Float32, Vector{Float32}}, x::Vector{Float32}) | |
@ NLSolversBase C:\Users\MrJSa\.julia\packages\NLSolversBase\kavn7\src\interface.jl:9 | |
[17] initial_state(method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, Nothing}, d::NonDifferentiable{Float32, Vector{Float32}}, initial_x::Vector{Float32}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\solvers\zeroth_order\nelder_mead.jl:171 | |
[18] optimize(d::NonDifferentiable{Float32, Vector{Float32}}, initial_x::Vector{Float32}, method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, Nothing}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\optimize.jl:36 | |
[19] optimize(f::Function, initial_x::Vector{Float32}; inplace::Bool, autodiff::Symbol, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\interface.jl:90 | |
[20] optimize(f::Function, initial_x::Vector{Float32}) | |
@ Optim C:\Users\MrJSa\.julia\packages\Optim\tP8PJ\src\multivariate\optimize\interface.jl:83 | |
[21] get_α(x::Vector{Float32}) | |
@ Main .\REPL[70]:22 | |
[22] top-level scope | |
@ REPL[71]:1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment