def solve(self): ''' Solves the cstwMPCmarket. ''' if self.AggShockBool: for agent in self.agents: agent.getEconomyData(self) Market.solve(self) else: self.solveAgents() self.makeHistory()
def main(): from time import clock from HARK import Market mystr = lambda number : "{:.4f}".format(number) import matplotlib.pyplot as plt from copy import deepcopy do_many_types = True # Make a test case and solve the micro model TestType = FashionVictimType(**Params.default_params) print('Utility function:') plotFuncs(TestType.conformUtilityFunc,0,1) t_start = clock() TestType.solve() t_end = clock() print('Solving a fashion victim micro model took ' + mystr(t_end-t_start) + ' seconds.') print('Jock value function:') plotFuncs(TestType.VfuncJock,0,1) print('Punk value function:') plotFuncs(TestType.VfuncPunk,0,1) print('Jock switch probability:') plotFuncs(TestType.switchFuncJock,0,1) print('Punk switch probability:') plotFuncs(TestType.switchFuncPunk,0,1) # Make a list of different types AltType = deepcopy(TestType) AltType(uParamA = Params.uParamB, uParamB = Params.uParamA, seed=20) AltType.update() AltType.solve() type_list = [TestType,AltType] u_vec = np.linspace(0.02,0.1,5) if do_many_types: for j in range(u_vec.size): ThisType = deepcopy(TestType) ThisType(punk_utility=u_vec[j]) ThisType.solve() type_list.append(ThisType) ThisType = deepcopy(AltType) ThisType(punk_utility=u_vec[j]) ThisType.solve() type_list.append(ThisType) for j in range(u_vec.size): ThisType = deepcopy(TestType) ThisType(jock_utility=u_vec[j]) ThisType.solve() type_list.append(ThisType) ThisType = deepcopy(AltType) ThisType(jock_utility=u_vec[j]) ThisType.solve() type_list.append(ThisType) # Now run the simulation inside a Market TestMarket = Market(agents = type_list, sow_vars = ['pNow'], reap_vars = ['sNow'], track_vars = ['pNow'], dyn_vars = ['pNextIntercept','pNextSlope','pNextWidth'], millRule = calcPunkProp, calcDynamics = calcFashionEvoFunc, act_T = 1000, tolerance = 0.01) TestMarket.pNow_init = 0.5 TestMarket.solve() plt.plot(TestMarket.pNow_hist) plt.show()
type_list.append(ThisType) # - # ## Market class illustration with FashionVictimModel # # The search for a dynamic general equilibrium is implemented in HARK's $\texttt{Market}$ class with the following definitions: # + from HARK import Market from HARK.FashionVictim.FashionVictimModel import * TestMarket = Market(agents = type_list, sow_vars = ['pNow'], reap_vars = ['sNow'], track_vars = ['pNow'], dyn_vars = ['pNextIntercept','pNextSlope','pNextWidth'], millRule = calcPunkProp, calcDynamics = calcFashionEvoFunc, act_T = 1000, tolerance = 0.01) TestMarket.pNow_init = 0.5 # - # The $\texttt{agents}$ attribute has a list of 22 $\texttt{FashionVictimType}$s, which vary in their values of $U_p$ and $U_j$, and their $f$ functions. The $\texttt{marketAction}$ method of $\texttt{FashionVictimType}$ simulates one period of the microeconomic model: each agent receives style preference shocks $\eta_0$ and $\eta_1$, sees the current proportion of punks $p_t$ (sown to them as $\texttt{pNow}$), and chooses which style to wear, storing it in the binary array $\texttt{sNow}$, an attribute of $\texttt{self}$. # # The $\texttt{millRule}$ for this market is extremely simple: it flattens the list of binary arrays of individual style choices (gathered in the $\texttt{reap}$ step) and averages them into a new value of $p_t$, to be tracked as a history and $\texttt{sow}$n back to the $\texttt{agents}$ to begin the cycle again. Once a history of 1000 values of $p_t$ has been generated with the $\texttt{makeHistory}$ method, we can calculate a new dynamic fashion rule with $\texttt{calcFashionEvoFunc}$ by regressing $p_t$ on $p_{t-1}$, approximating $w$ as twice the standard deviation of prediction errors. The new fashion rule is an instance of the simple $\text{FashionEvoFunc}$ class, whose only methods are inherited from $\texttt{HARKobject}$. # # When the $\texttt{solve}$ method is run, the solver successively solves each agent's microeconomic problem, runs the $\texttt{makeHistory}$ method to generate a 1000 period history of $p_t$, and calculates a new punk evolution rule based on this history; the solver terminates when consecutive rules differ by less than 0.01 in any dimension. # TestMarket.solve()