On Saturday, 20 August 2022 at 16:09:14 UTC+1 [email protected] wrote:
> Most welcome!
> Like I said, passing args = (L, K , VA) to your function F may take a
> little 'playing around'.
>
This is one way to do it:
import numpy as np
import sympy as sp
from scipy.optimize import minimize, curve_fit
L_vals = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262,
0.256, 0.250])
K_vals = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410,
3.446, 3.416])
VA_vals = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, 0.981,
0.908, 0.794])
gamma, alpha, beta, eta, L, K, VA = sp.symbols('gamma, alpha, beta, eta, L,
K, VA')
Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K ** -eta)
# Note that the first entry in the args tuple is a 4 tuple
# That is needed to unpack the arguments from an array
f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est)
def f(param):
Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals)
return np.sum((np.log(VA_vals) - Vi_est_vals) ** 2)
bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)]
x0 = (1, 0.01, 0.98, 1)
result = minimize(f, x0, bounds=bnds)
print(result.message)
print(result.x[0], result.x[1], result.x[2], result.x[3])
# Output:
# CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL
# 1.0 0.5587146900213987 0.9371431014439708 5.87304177555323
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/0d41fe02-9bba-41e8-83d0-5971f646475dn%40googlegroups.com.