I need some help to write my code more efficiently. The below code aims to solve a non-linear equation for a variable hi
given the elements in some arrays. For example:
import numpy as np
import tensorflow as tf
import scipy.optimize
kon = 0.01
mu = 1.5
fi = 0.5
kappa = 22
n =100
xs = tf.random.normal(shape=(n,), stddev=0.2)
eps = tf.random.normal(shape=(n,), stddev=0.17)
z = tf.sigmoid(tf.random.normal(shape=(n,), stddev=0.22))
# non-linear equation to solve for leisure
def F(hi):
return (mu/fi)*np.log(hi) -(1-mu)*kappa*(hi)**(1+(1/fi))-mu*(np.log(w*ei*xs)-np.log(kon))-np.log(ze)
hvec = np.empty((0,))
# leisure today
for ze,ei,xs in zip(z, eps, x0):
ei=tf.exp(ei)
xs=tf.exp(xs)
htemp = scipy.optimize.newton_krylov(F, 0.5)
hvec = np.append(hvec, htemp)
Here, z
,eps
,x0
are arrays of size n
(e.g. n=100
)
Is there a more efficient way to do the same thing? for example, by avoiding the loop ?
I basically want to solve for hi
for each element in these three arrays.