On Fri, 30 Dec 2022 at 20:00, jesse grabowski <jessegrabow...@gmail.com> wrote: > > Hello everyone! > > I am (trying) to use Sympy to automatically derive first order conditions and > a log-linear approximation to non-linear systems of equations from user > provided problem descriptions. An example system might look something like > this: > > -C_t - I_t + K_t-1*r_t + L_t*w_t > I_t - K_t + K_t-1*(1 - delta) > -lambda_t + C_t**(-sigma_C) > -L_t**sigma_L + lambda_t*w_t > beta*(lambda_t+1*r_t+1 + lambda_t+1*(1 - delta)) - lambda_t > A_t*K_t-1**alpha*L_t**(1 - alpha) - Y_t > alpha*A_t*K_t-1**(alpha - 1)*L_t**(1 - alpha) - r_t > A_t*K_t-1**alpha*(1 - alpha)/L_t**alpha - w_t > rho_A*log(A_t-1) + epsilon_A_t - log(A_t) > > A priori I know that this particular system can be written in reduced form in > only two state variables, A_t and K_t. At the bare minimum, many equations > can be eliminated via substitution. For example, we clearly see that lambda_t > = C_t ** (-sigma_C), and we could make that substitutions everywhere to > eliminate the equation. Less obviously, I can combine equations -4, -3 and -2 > to get a single expression for w_t as a function of r_t, eliminating that > variable. > > If the system were linear, I could easily obtain the reduced form in Sympy > with something like this: > > A, b = sp.linear_eq_to_matrix(system) > sp.rref(sp.hstack(A, b)) > > But in the non-linear case I understand there is no "nice" way to do this. > > I guess I am asking if anyone knows of tricks (or better yet algorithms) to > attack a problem like this? I currently use a big stack of heuristics, for > example looking for equations with only 2 non-parameter atoms in the form y = > f(x), then recursively making substitutions. These heuristics are quite flaky > though, because I'm quite out of my depth.
The generalisation of the RREF (reduced row echelon form) from linear systems of equations to polynomial systems is known as a Groebner basis. SymPy has a function groebner that can compute this. Note though that computing Groebner bases can be an extremely expensive operation depending on the exact structure of your equations and the number of symbols. It can still be used on part of your system though and potentially also an RREF can be used for a subset of equations and variables appearing linearly. It's not clear to me which of the symbols in your equations are supposed to be unknowns and which are supposed to be parameters so I won't demonstrate how to do any of this directly. If you show complete code to make all the symbols and also say which are the unknowns then it might be possible for someone to suggest something. -- Oscar -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQ_fjpS15ZbYtCiD_eSRwQudhHo5tLbHDtsta-vnWTa%3DA%40mail.gmail.com.