Update ConsRiskyAssetModel.py
optional implementation with root finder
@@ -8,7 +8,7 @@ risky assets that will be useful to models what will inherit from it.
from dataclasses import dataclass
import numpy as np
-from scipy.optimize import minimize_scalar
+from scipy.optimize import minimize_scalar, root_scalar
from HARK import make_one_period_oo_solver
from HARK.ConsumptionSaving.ConsIndShockModel import (
@@ -336,6 +336,7 @@ class ConsRiskySolver(ConsIndShockSolver):
"""
Solver for an agent that can save in an asset that has a risky return.
"""
+
solution_next: ConsumerSolution
IncShkDstn: DiscreteDistribution
TranShkDstn: DiscreteDistribution
@@ -490,7 +491,8 @@ class ConsRiskySolver(ConsIndShockSolver):
# if zero is BoroCnstNat, do not evaluate at 0.0
aNrmNow = self.aXtraGrid
bNrmNext = np.append(
- aNrmNow[0] * self.RiskyDstn.X.min(), aNrmNow * self.RiskyDstn.X.max(),
+ aNrmNow[0] * self.RiskyDstn.X.min(),
+ aNrmNow * self.RiskyDstn.X.max(),
)
wNrmNext = np.append(
bNrmNext[0] / (self.PermGroFac * self.PermShkDstn.X.max()),
@@ -782,6 +784,8 @@ class ConsBasicPortfolioSolver(ConsRiskySolver):
b_nrm = a_nrm * r_port
return a_nrm * r_diff * preIncShkvPfunc(b_nrm)
+ if True:
+
EndOfPrddvds = calc_expectation(
self.RiskyDstn, endOfPrddvds, self.aMat, self.sMat
)
@@ -789,6 +793,33 @@ class ConsBasicPortfolioSolver(ConsRiskySolver):
self.opt_share = self.optimize_share(EndOfPrddvds)
+ else:
+
+ def obj(share, a_nrm):
+ return calc_expectation(self.RiskyDstn, endOfPrddvds, a_nrm, share)
+
+ opt_share = np.empty_like(self.aNrmNow)
+
+ for ai in range(self.aNrmNow.size):
+ a_nrm = self.aNrmNow[ai]
+ if a_nrm == 0:
+ opt_share[ai] = 1.0
+ else:
+ try:
+ sol = root_scalar(
+ obj, bracket=[self.ShareLimit, 1.0], args=(a_nrm,)
+ )
+
+ if sol.converged:
+ opt_share[ai] = sol.root
+ else:
+ opt_share[ai] = 1.0
+
+ except ValueError:
+ opt_share[ai] = 1.0
+
+ self.opt_share = opt_share
+
def endOfPrddvda(risky_shk, a_nrm, share):
r_diff = risky_shk - self.Rfree
r_port = self.Rfree + r_diff * share
