Skip to content

Commit

Permalink
swapped to Abstract types
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJeeOnGitHub committed Nov 5, 2024
1 parent b8ef222 commit 43e1ecd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/functions_estimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ end


Base.@kwdef mutable struct GMMFit
theta0::Vector # initial conditions (vector of size P or K x P matrix for K sets of initial conditions)
theta_hat::Vector # estimated parameters (vector of size P)
theta0::AbstractVector # initial conditions (vector of size P or K x P matrix for K sets of initial conditions)
theta_hat::AbstractVector # estimated parameters (vector of size P)
theta_names::Union{Vector{String}, Nothing}
theta_factors::Union{Vector{Float64}, Nothing} = nothing # nothing or a vector of length P with factors for each parameter. Parameter theta[i] was replaced by theta[i] * theta_factors[i] before optimization

Expand Down Expand Up @@ -180,9 +180,9 @@ function fit(

# checks # TODO: add more
@assert isa(mom_fn, Function) "mom_fn must be a function"
@assert isa(theta0, Vector) || isa(theta0, Matrix) "theta0 must be a Vector (P) or a Matrix (K x P)"
@assert isa(W, Matrix) || isa(W, UniformScaling) "W must be a Matrix or UniformScaling (e.g. I)"
@assert isa(weights, Vector) || isnothing(weights) "weights must be a Vector or nothing"
@assert isa(theta0, AbstractVector) || isa(theta0, AbstractMatrix) "theta0 must be a Vector (P) or a Matrix (K x P)"
@assert isa(W, AbstractMatrix) || isa(W, UniformScaling) "W must be a Matrix or UniformScaling (e.g. I)"
@assert isa(weights, AbstractVector) || isnothing(weights) "weights must be a Vector or nothing"
@assert mode == :onestep || mode == :twostep "mode must be :onestep or :twostep"

# one-step or two-step GMM
Expand Down Expand Up @@ -321,7 +321,7 @@ function fit_onestep(

### initial conditions
# number of initial conditions (and always format as matrix, rows=iterations, columns=paramters)
isa(theta0, Vector) && (theta0 = Matrix(transpose(theta0)))
isa(theta0, AbstractVector) && (theta0 = Matrix(transpose(theta0)))
nic = size(theta0, 1)

# number of parameters
Expand Down
4 changes: 2 additions & 2 deletions src/optimization_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ end
### OPTIM.JL BACKEND
####################

function gmm_objective(theta::Vector, data, mom_fn::Function, W, weights; trace=0)
function gmm_objective(theta::AbstractVector, data, mom_fn::Function, W, weights; trace=0)

t1 = @elapsed m = mom_fn(data, theta)

@assert isa(m, Matrix) "m(data, theta) must return a Matrix (rows = observations, columns = moments)"
@assert isa(m, AbstractMatrix) "m(data, theta) must return a Matrix (rows = observations, columns = moments)"

(trace > 1) && println("Evaluation took ", t1)

Expand Down
7 changes: 4 additions & 3 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"""
Creates a random matrix of initial conditions, taking bounds into account
"""
function random_initial_conditions(theta0::Vector, nic::Int; theta_lower=nothing, theta_upper=nothing)

function random_initial_conditions(theta0::AbstractVector, nic::Int; theta_lower=nothing, theta_upper=nothing)
n_params = length(theta0)
theta0_mat = zeros(nic, n_params)
theta0_mat = ones(nic) * theta0'
isnothing(theta_lower) && (theta_lower = fill(-Inf, n_params))
isnothing(theta_upper) && (theta_upper = fill( Inf, n_params))

Expand Down Expand Up @@ -36,6 +35,8 @@ function random_initial_conditions(theta0::Vector, nic::Int; theta_lower=nothing
return theta0_mat
end



"""
theta_fix is a vector with the fixed values of the parameters, and missing at the other locations
this function fills in the missing values with values from theta_small
Expand Down

0 comments on commit 43e1ecd

Please sign in to comment.