| 1 |  |  | import functools | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from ... import problems | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class RamseyCassKoopmansModel(problems.TwoPointBVP): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     Class representing a generic Solow growth model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     Attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     equilibrium_capital : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         Equilibrium value for capital (per unit effective labor). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     equilibrium_consumption : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         Equilibrium value for consumption (per unit effective labor). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     intensive_output : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         Output (per unit effective labor supply). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     marginal_product_capital : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         Marginal product of capital (i.e., first derivative of intensive output). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     params : dict(str: float) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         Dictionary of model parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     pratt_arrow_risk_aversion : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         Pratt-Arrow relative risk aversion function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def __init__(self, ARA, f, k_star, mpk, params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         Initialize an instance of the RamseyCassKoopmans class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ARA : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             Pratt-Arrow absolute risk aversion function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         f : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             Output (per unit effective labor supply). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         k_star : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             Equilibrium (i.e., steady-state) value for capital stock (per unit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             effective labor supply). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         mpk : function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             Marginal product of capital (per unit effective labor supply). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         params : dict(str: float) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             Dictionary of model parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self._equilibrium_capital = k_star | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self._intensive_output = f | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         self._marginal_product_capital = mpk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         self._pratt_arrow_risk_aversion = ARA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         # construct the terminal condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         c_star = self._c_star_factory(k_star) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         terminal_condition = self._terminal_condition_factory(c_star) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self._equilibrium_consumption = c_star | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         # construct the RHS of the system of ODEs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         rhs = self._rhs_factory(ARA, f, mpk) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         super(RamseyCassKoopmansModel, self).__init__(self._initial_condition, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                                                       terminal_condition, 1, 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                                                       params, rhs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     def equilibrium_capital(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return self._equilibrium_capital | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     @property | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     def equilibrium_consumption(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return self._equilibrium_consumption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     def intensive_output(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return self._intensive_output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     def marginal_product_capital(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return self._marginal_product_capital | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     def pratt_arrow_risk_aversion(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return self._pratt_arrow_risk_aversion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     def _actual_investment(k_tilde, c_tilde, f, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return f(k_tilde, **params) - c_tilde | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     def _breakeven_investment(k_tilde, delta, g, n, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return (g + n + delta) * k_tilde | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     def _c_tilde_dot(cls, t, k_tilde, c_tilde, ARA, mpk, A0, delta, g, rho, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         A = cls._technology(t, A0, g) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return ((mpk(k_tilde, **params) - delta - rho) / (A * ARA(t, A * c_tilde, **params))) - g * c_tilde | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     def _initial_condition(t, k_tilde, c_tilde, A0, K0, N0, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return [k_tilde - (K0 / (A0 * N0))] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     def _technology(t, A0, g): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return A0 * np.exp(g * t) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     def _k_dot(cls, t, k_tilde, c_tilde, f, delta, g, n, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         k_dot = (cls._actual_investment(k_tilde, c_tilde, f, **params) - | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                  cls._breakeven_investment(k_tilde, delta, g, n)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return k_dot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     def _ramsey_model(cls, t, k_tilde, c_tilde, ARA, f, mpk, A0, delta, g, n, rho, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         out = [cls._k_dot(t, k_tilde, c_tilde, f, delta, g, n, **params), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                cls._c_tilde_dot(t, k_tilde, c_tilde, ARA, mpk, A0, delta, g, rho, **params)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return out | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     def _rhs_factory(cls, ARA, f, mpk): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return functools.partial(cls._ramsey_model, ARA=ARA, f=f, mpk=mpk) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     def _terminal_condition(t, k_tilde, c_tilde, c_star, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return [c_tilde - c_star(**params)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     def _terminal_condition_factory(cls, c_star): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return functools.partial(cls._terminal_condition, c_star=c_star) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     def _c_star(self, k_star, **params): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         k_tilde = k_star(**params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         c_star = (self.intensive_output(k_tilde, **params) - | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                   self._breakeven_investment(k_tilde, **params)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return c_star | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     def _c_star_factory(self, k_star): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |         return functools.partial(self._c_star, k_star=k_star) | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  |