Autodifferentiation of model with fixed and mutable parameters

Hi @EminentCoder, welcome to the community!

What the answer above (ChatGPT ?) means to say is that your vector all_params is initialized to be full of Float64. Thus, ForwardDiff cannot use it in differentiation because it needs to work on Dual numbers (see limitations of ForwardDiff).
I think something along this line would work, where you re-create a new vector of global parameters with the same element type as the variable you’re using in differentiation:

function separator(obj_func, selector_array, all_params, mutable_params)
    new_all_params = similar(all_params, eltype(mutable_params))  # can accommodate Dual numbers if necessary
    new_all_params[selector_array] .= mutable_params
    new_all_params[.!selector_array] = all_params[.!selector_array]
    return obj_func(all_params)
end

If that code is too slow for you, you may also want take a look at the performance tips. In particular, using of global variables that are not passed as function arguments is often a bad idea performance-wise.

1 Like