nx = 3 
 
ny = 6 
 
ns = 2 
 
nv = 9 
 
ne = 9 
 
jumps_to_approximate = [1, 2, 4, 5, 6] 
 
eqns_to_approximate = [7] 
 
variables = ["z", "b", "k", "y", "c", "l", "i", "ac", "acdev"] 
 
function nlsolve_static_equations(f::Array{T,1},x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f[1] = x[1] - (p[1] * x[1] + p[2] * 0.0)
  f[2] = x[2] - (p[3] * x[2] + p[4] * 0.0)
  f[3] = x[8] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[3]/x[3] - 1)) + p[11]*(x[3]/x[3] - 1) - 1))
  f[4] = x[4] - (p[12] * exp(x[1]) * x[3]^p[7] * x[6]^(1-p[7]))
  f[5] = x[5] - (x[4] - x[7] - x[3]*x[8])
  f[6] = x[9] - (p[10]/p[11] * (1-exp(-p[11]*(x[3]/x[3]-1))))
  f[7] = x[5]^(-p[6]) * (1+ x[9]) - (1/(1 + p[5]/400) * x[5]^(-p[6]) * ( p[7] * x[4]/x[3] + 1 - p[8] - x[8] + x[9]*( x[7]/x[3]+ 1-p[8] ) ))
  f[8] = (1.0 - p[7]) * x[4] / x[6] - (p[13] * exp(x[2]) * x[5]^p[6] * x[6]^(1/p[9]))
  f[9] = x[7] - (x[3] - (1-p[8])*x[3])

end 
 
function static_equations(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = Array{T,1}(undef,length(x)) 
 
  f[1] = x[1] - (p[1] * x[1] + p[2] * 0.0)
  f[2] = x[2] - (p[3] * x[2] + p[4] * 0.0)
  f[3] = x[8] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[3]/x[3] - 1)) + p[11]*(x[3]/x[3] - 1) - 1))
  f[4] = x[4] - (p[12] * exp(x[1]) * x[3]^p[7] * x[6]^(1-p[7]))
  f[5] = x[5] - (x[4] - x[7] - x[3]*x[8])
  f[6] = x[9] - (p[10]/p[11] * (1-exp(-p[11]*(x[3]/x[3]-1))))
  f[7] = x[5]^(-p[6]) * (1+ x[9]) - (1/(1 + p[5]/400) * x[5]^(-p[6]) * ( p[7] * x[4]/x[3] + 1 - p[8] - x[8] + x[9]*( x[7]/x[3]+ 1-p[8] ) ))
  f[8] = (1.0 - p[7]) * x[4] / x[6] - (p[13] * exp(x[2]) * x[5]^p[6] * x[6]^(1/p[9]))
  f[9] = x[7] - (x[3] - (1-p[8])*x[3])

  return f 
 
end 
 
function dynamic_equations(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = Array{T,1}(undef,9) 
 
  f[1] = x[10] - (p[1] * x[1] + p[2] * x[19])
  f[2] = x[11] - (p[3] * x[2] + p[4] * x[20])
  f[3] = x[8] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[12]/x[3] - 1)) + p[11]*(x[12]/x[3] - 1) - 1))
  f[4] = x[4] - (p[12] * exp(x[1]) * x[3]^p[7] * x[6]^(1-p[7]))
  f[5] = x[5] - (x[4] - x[7] - x[3]*x[8])
  f[6] = x[9] - (p[10]/p[11] * (1-exp(-p[11]*(x[12]/x[3]-1))))
  f[7] = x[5]^(-p[6]) * (1+ x[9]) - (1/(1 + p[5]/400) * x[14]^(-p[6]) * ( p[7] * x[13]/x[12] + 1 - p[8] - x[17] + x[18]*( x[16]/x[12]+ 1-p[8] ) ))
  f[8] = (1.0 - p[7]) * x[4] / x[6] - (p[13] * exp(x[2]) * x[5]^p[6] * x[6]^(1/p[9]))
  f[9] = x[7] - (x[12] - (1-p[8])*x[3])

  return f 
 
end 

function dynamic_eqn_1(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[10] - (p[1] * x[1] + p[2] * x[19])

  return f 
 
end 

function dynamic_eqn_2(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[11] - (p[3] * x[2] + p[4] * x[20])

  return f 
 
end 

function dynamic_eqn_3(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[8] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[12]/x[3] - 1)) + p[11]*(x[12]/x[3] - 1) - 1))

  return f 
 
end 

function dynamic_eqn_4(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[4] - (p[12] * exp(x[1]) * x[3]^p[7] * x[6]^(1-p[7]))

  return f 
 
end 

function dynamic_eqn_5(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[5] - (x[4] - x[7] - x[3]*x[8])

  return f 
 
end 

function dynamic_eqn_6(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[9] - (p[10]/p[11] * (1-exp(-p[11]*(x[12]/x[3]-1))))

  return f 
 
end 

function dynamic_eqn_7(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[5]^(-p[6]) * (1+ x[9]) - (1/(1 + p[5]/400) * x[14]^(-p[6]) * ( p[7] * x[13]/x[12] + 1 - p[8] - x[17] + x[18]*( x[16]/x[12]+ 1-p[8] ) ))

  return f 
 
end 

function dynamic_eqn_8(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = (1.0 - p[7]) * x[4] / x[6] - (p[13] * exp(x[2]) * x[5]^p[6] * x[6]^(1/p[9]))

  return f 
 
end 

function dynamic_eqn_9(x::Array{T,1},p::Array{T1,1}) where {T<:Number, T1<:Real} 
 
  f = x[7] - (x[12] - (1-p[8])*x[3])

  return f 
 
end 

individual_equations = Array{Function}(undef,9) 
individual_equations[1] = dynamic_eqn_1
individual_equations[2] = dynamic_eqn_2
individual_equations[3] = dynamic_eqn_3
individual_equations[4] = dynamic_eqn_4
individual_equations[5] = dynamic_eqn_5
individual_equations[6] = dynamic_eqn_6
individual_equations[7] = dynamic_eqn_7
individual_equations[8] = dynamic_eqn_8
individual_equations[9] = dynamic_eqn_9

function closure_projection_equations(state,scaled_weights,order,domain,approximate,p) 
 
  function projection_equations(f::Array{T,1},x::Array{T,1}) where {T<:Number} 
 
    approx1 = approximate(scaled_weights[1],x[6+1:end],order,domain)
    approx2 = approximate(scaled_weights[2],x[6+1:end],order,domain)
    approx4 = approximate(scaled_weights[3],x[6+1:end],order,domain)
    approx5 = approximate(scaled_weights[4],x[6+1:end],order,domain)
    approx6 = approximate(scaled_weights[5],x[6+1:end],order,domain)

    #f = Array{T,1}(undef,9) 
 
    f[1] = x[7] - (p[1] * state[1] + p[2] * 0.0)
    f[2] = x[8] - (p[3] * state[2] + p[4] * 0.0)
    f[3] = x[5] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[9]/state[3] - 1)) + p[11]*(x[9]/state[3] - 1) - 1))
    f[4] = x[1] - (p[12] * exp(state[1]) * state[3]^p[7] * x[3]^(1-p[7]))
    f[5] = x[2] - (x[1] - x[4] - state[3]*x[5])
    f[6] = x[6] - (p[10]/p[11] * (1-exp(-p[11]*(x[9]/state[3]-1))))
    f[7] = x[2]^(-p[6]) * (1+ x[6]) - (1/(1 + p[5]/400) * approx2^(-p[6]) * ( p[7] * approx1/x[9] + 1 - p[8] - approx5 + approx6*( approx4/x[9]+ 1-p[8] ) ))
    f[8] = (1.0 - p[7]) * x[1] / x[3] - (p[13] * exp(state[2]) * x[2]^p[6] * x[3]^(1/p[9]))
    f[9] = x[4] - (x[9] - (1-p[8])*state[3])

    #return f 
 
  end 
 
  return projection_equations 
 
end 

function closure_projection_equations_pl(variables,grid,state,integrals,approximate,p) 
 
  function projection_equations_pl(f::Array{T,1},x::Array{T,1}) where {T<:Number} 
 
    approx1 = approximate(variables[1],grid,x[6+1:end],integrals)
    approx2 = approximate(variables[2],grid,x[6+1:end],integrals)
    approx4 = approximate(variables[4],grid,x[6+1:end],integrals)
    approx5 = approximate(variables[5],grid,x[6+1:end],integrals)
    approx6 = approximate(variables[6],grid,x[6+1:end],integrals)

    #f = Array{T,1}(undef,9) 
 
    f[1] = x[7] - (p[1] * state[1] + p[2] * 0.0)
    f[2] = x[8] - (p[3] * state[2] + p[4] * 0.0)
    f[3] = x[5] - (p[10] / p[11]^2 * ( exp(-p[11]*(x[9]/state[3] - 1)) + p[11]*(x[9]/state[3] - 1) - 1))
    f[4] = x[1] - (p[12] * exp(state[1]) * state[3]^p[7] * x[3]^(1-p[7]))
    f[5] = x[2] - (x[1] - x[4] - state[3]*x[5])
    f[6] = x[6] - (p[10]/p[11] * (1-exp(-p[11]*(x[9]/state[3]-1))))
    f[7] = x[2]^(-p[6]) * (1+ x[6]) - (1/(1 + p[5]/400) * approx2^(-p[6]) * ( p[7] * approx1/x[9] + 1 - p[8] - approx5 + approx6*( approx4/x[9]+ 1-p[8] ) ))
    f[8] = (1.0 - p[7]) * x[1] / x[3] - (p[13] * exp(state[2]) * x[2]^p[6] * x[3]^(1/p[9]))
    f[9] = x[4] - (x[9] - (1-p[8])*state[3])

    #return f 
 
  end 
 
  return projection_equations_pl 
 
end 

unassigned_parameters = ["ρz", "σz", "ρb", "σb", "r", "τ", "α", "δ", "ν", "ϕ1", "ϕ2", "Zstar", "Bstar"]