implement UtilityFuncCRRA
@@ -531,6 +531,121 @@ def CARAutility_invP(u, alpha):
return 1.0 / (alpha * (1.0 - u))
+class UtilityFuncCRRA:
+ """
+ A class for representing a CRRA utility function.
+
+ Parameters
+ ----------
+ CRRA : float
+ The coefficient of constant relative risk aversion.
+ """
+
+ def __init__(self, CRRA):
+ self.CRRA = CRRA
+
+ def __call__(self, c, order=0):
+ """
+ Evaluate the utility function at a given level of consumption c.
+
+ Parameters
+ ----------
+ c : float or np.ndarray
+ Consumption level(s).
+ order : int, optional
+ Order of derivative. For example, `order == 1` returns the
+ first derivative of utility of consumption, and so on. By default 0.
+
+ Returns
+ -------
+ float or np.ndarray
+ Utility (or its derivative) evaluated at given consumption level(s).
+ """
+ if order == 0:
+ return CRRAutility(c, self.CRRA)
+ else: # order >= 1
+ return self.derivative(c, order)
+
+ def derivative(self, c, order=1):
+ """
+ The derivative of the utility function at a given level of consumption c.
+
+ Parameters
+ ----------
+ c : float or np.ndarray
+ Consumption level(s).
+ order : int, optional
+ Order of derivative. For example, `order == 1` returns the
+ first derivative of utility of consumption, and so on. By default 1.
+
+ Returns
+ -------
+ float or np.ndarray
+ Derivative of CRRA utility evaluated at given consumption level(s).
+
+ Raises
+ ------
+ ValueError
+ Derivative of order higher than 4 is not supported.
+ """
+ if order == 1:
+ return CRRAutilityP(c, self.CRRA)
+ elif order == 2:
+ return CRRAutilityPP(c, self.CRRA)
+ elif order == 3:
+ return CRRAutilityPPP(c, self.CRRA)
+ elif order == 4:
+ return CRRAutilityPPPP(c, self.CRRA)
+ else:
+ raise ValueError("Derivative of order {} not supported".format(order))
+
+ def inverse(self, u, order=(0, 0)):
+ """
+ The inverse of the utility function at a given level of utility u.
+
+ Parameters
+ ----------
+ u : float or np.ndarray
+ Utility level(s).
+ order : tuple, optional
+ Order of derivatives. For example, `order == (1,1)` represents
+ the first derivative of utility, inversed, and then differenciated
+ once. By default (0, 0), which is just the inverse of utility.
+
+ Returns
+ -------
+ float or np.ndarray
+ Inverse of CRRA utility evaluated at given utility level(s).
+
+ Raises
+ ------
+ ValueError
+ Higher order derivatives are not supported.
+ """
+ if order == (0, 0):
+ return CRRAutility_inv(u, self.CRRA)
+ elif order == (1, 0):
+ return CRRAutilityP_inv(u, self.CRRA)
+ elif order == (0, 1):
+ return CRRAutility_invP(u, self.CRRA)
+ elif order == (1, 1):
+ return CRRAutilityP_invP(u, self.CRRA)
+ else:
+ raise ValueError("Inverse of order {} not supported".format(order))
+
+ def der(self, c, order=1):
+ """
+ Short alias for derivative. See `self.derivative`.
+ """
+ return self.derivative(c, order)
+
+ def inv(self, c, order=(0, 0)):
+ """
+ Short alias for inverse. See `self.inverse`.
+ """
+ return self.inverse(c, order)
+
+
# ==============================================================================
# ============== Functions for generating state space grids ===================
# ==============================================================================
