Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/analytical derivatives #48

Open
wants to merge 2 commits into
base: analytical_derivatives
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion test/test_analytical_derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Dynare.FastLapackInterface
using Test
using SparseArrays
using SuiteSparse
using GeneralizedSylvesterSolver

#Model
context = @dynare "models/analytical_derivatives/fs2000_sa.mod" "params_derivs_order=1" "notmpterms";
Expand Down Expand Up @@ -168,7 +169,6 @@ workspace2 = init_derivatives2_workspace(n, m)
workspace3 = init_derivatives3_workspace(n, m)
workspace4 = init_derivatives4_workspace(n, m)

# Luego puedes utilizar workspace para llamar a la función Derivatives con tus matrices df_dx y df_dp
@time sol1 = Derivatives(workspace1, df_dx, df_dp);
@time sol2 = Derivatives2(workspace2, df_dx, df_dp);
@time sol3 = Derivatives3(workspace3, df_dx, df_dp);
Expand Down Expand Up @@ -227,5 +227,55 @@ X = zeros(n, n)
# set nonzero columns
X[:, k1] .= context.results.model_results[1].linearrationalexpectations.g1_1

#Generalized Sylvester: ax + bxc = d
a = A*X + B
b = Matrix(A)
c = X
X2 = X*X
d = zeros(n, n, m)

a_orig = copy(a)
b_orig = copy(b)
c_orig = copy(c)
d_orig = copy(d)

order=1
ws = GeneralizedSylvesterWs(n,n,n,order)
#Solve UQME using generalized_sylvester_solver!
for i in 1:m
@views begin
mul!(d[:,:,i], dA_dp[:,:,i], X2)
mul!(d[:,:,i], dB_dp[:,:,i], X, true, true)
d[:,:,i] .+= dC_dp[:,:,i]
end
generalized_sylvester_solver!(a, b, c, d[:,:,i], order, ws)
end

#Test 1
for i in 1:m
@test a_orig*d[:,:,i] + b_orig*d[:,:,i]*c_orig ≈ d_orig[:,:,i] #Fail
end

#Test 2
for i in 1:m
@test d[:,:,i] ≈ reshape((kron(I(n^order),a_orig) + kron(c_orig',b_orig))\vec(d_orig[:,:,i]),n,n^order) #Fail
end

#Test 3
using FiniteDifferences

function funX(params)
X = zeros(n, n)
X[:, k1] .= context.results.model_results[1].linearrationalexpectations.g1_1
return X #vec()
end

fd = central_fdm(5, 1)
dX_dz_tuple = jacobian(fd, funX, params)
dX_dz_matrix = dX_dz_tuple[1]
dX_dz = permutedims(reshape(dX_dz_matrix, m, n, n), (2, 3, 1))

@test d ≈ dX_dz #Fail

end # end module