Esempio n. 1
0
 def test_SolveEquation(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     obj.SolveStep(3)
     obj2 = EquationSolver()
     obj2.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj2.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj2.SolveEquation()
     self.assertEqual(obj.TimeSeries['x'], obj2.TimeSeries['x'])
Esempio n. 2
0
 def test_solver_logging(self):
     Logger.cleanup()
     mock = MockFile()
     Logger.log_file_handles = {'step': mock}
     obj = EquationSolver()
     obj.TraceStep = 2
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
          x=t
          z=x+1
          z(0) = 2.
          exogenous
          t=[10.]*20
          MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     self.assertEqual([], mock.buffer)
     obj.SolveStep(1)
     self.assertEqual([], mock.buffer)
     obj.SolveStep(2)
     # I do not care what the contents are; I just want to validate
     # that it triggers
     self.assertTrue(len(mock.buffer) > 0)
     # Reset the buffer
     mock.buffer = []
     obj.SolveStep(3)
     self.assertEqual([], mock.buffer)
Esempio n. 3
0
 def test_InitialEquilibrium(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
          x=t
          z=x+1
          w=z(k-1)
          z(0) = 2.
          exogenous
          t=[10.]*20
          MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.ParameterInitialSteadyStateMaxTime = 3
     copied_solver = obj.CalculateInitialSteadyState()
     self.assertEqual([
         10.,
     ], obj.TimeSeries['x'])
     self.assertEqual([
         11.,
     ], obj.TimeSeries['z'])
     self.assertEqual([
         11.,
     ], obj.TimeSeries['w'])
     self.assertEqual([2., 11., 11., 11.], copied_solver.TimeSeries['z'])
     self.assertEqual([0., 2., 11., 11.], copied_solver.TimeSeries['w'])
Esempio n. 4
0
 def test_ExtractVariableList_3(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x
      exogenous
      t=[10.]*20""")
     obj.ExtractVariableList()
     self.assertEqual(['t', 'x', 'z'], obj.VariableList)
Esempio n. 5
0
 def test_initial_conditions_bad_ic2(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x(k-1)
      exogenous
      t=kaboom
      MaxTime=3""")
     obj.ExtractVariableList()
     with self.assertRaises(ValueError):
         obj.SetInitialConditions()
Esempio n. 6
0
 def test_FailLog0(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     obj.ParseString("""
      x = log10(0)
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     with self.assertRaises(ValueError):
         obj.SolveStep(1)
Esempio n. 7
0
    def test_csv_text2(self):
        obj = EquationSolver()
        # Make the time series int so we do not test how floats are formatted...
        tmp = {
            'k': [0, 1],
            'foo': [10, 11],
            't': [0, 3],
        }
        for k, v in tmp.items():
            obj.TimeSeries[k] = v
        targ = """k\tt\tfoo
0\t0\t10
1\t3\t11
"""
        self.assertEqual(targ, obj.GenerateCSVtext())
Esempio n. 8
0
 def test_InitialEquilibrium_fail_bad_eqn(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
          x=foo
          exogenous
          t=[10.]*20
          MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     Parameters.InitialEquilbriumMaxTime = 3
     obj.MaxIterations = 2
     with self.assertRaises(NameError):
         obj.CalculateInitialSteadyState()
Esempio n. 9
0
 def test_SolveStep_3(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x(k-1)
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.MaxIterations = 0
     with self.assertRaises(ConvergenceError):
         obj.SolveStep(1)
Esempio n. 10
0
 def test_DivideZeroSkip(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     obj.ParseString("""
      z = t
      x=1/z
      exogenous
      t=[0., 10., 10., 10.]
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     self.assertEqual([0., .1, .1], obj.TimeSeries['x'])
Esempio n. 11
0
 def test_decoration_fail(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
        x=2*t
        y=z
        z=x
        exogenous
        t=1.
        MaxTime=3""")
     # Need to break the decoration values;
     obj.Parser.Decoration = [('y', 'x'), ('z', 'foo')]
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     with self.assertRaises(ValueError):
         obj.SolveStep(1)
Esempio n. 12
0
 def test_SolveEquation_with_initial_equilibrium(self):
     obj2 = EquationSolver()
     obj2.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj2.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj2.ParameterSolveInitialSteadyState = True
     obj2.SolveEquation()
     # Must be reset
     self.assertFalse(Parameters.SolveInitialEquilibrium)
     # The Initial equilibrium calculation overrides the initial condition.
     self.assertEqual([11., 11., 11., 11.], obj2.TimeSeries['z'])
Esempio n. 13
0
 def test_InitialEquilibrium_no_convergence(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
          x=t
          u = v+1
          v = u(k-1)
          exogenous
          t=[10.]*20
          MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     Parameters.InitialEquilbriumMaxTime = 3
     Parameters.InitialEquilibriumExcludedVariables = ['x']
     with self.assertRaises(NoEquilibriumError):
         obj.CalculateInitialSteadyState()
Esempio n. 14
0
def generate_model(eqns, use_control=False):
    if use_control:
        eqns += """
        u = (-.7 * x + .75 * r)/.05
        u(0) = 1.0
        exogenous
        r = [1.] * 10 + [2.]*21
            """
    else:
        eqns += """
    exogenous
    u = [1.] * 10 + [2.]*21
        """
    print(eqns)
    out = EquationSolver(eqns)
    out.Parser.MaxTime = 30
    out.SolveEquation()
    return out
Esempio n. 15
0
 def test_initial_conditions_const_endo(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
        x=1.
        z=2
        exogenous
        t=1.0
        MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     self.assertEqual([
         1.,
     ], obj.TimeSeries['x'])
     self.assertEqual([
         2.,
     ], obj.TimeSeries['z'])
Esempio n. 16
0
 def test_initial_conditions_with_float_exo(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
        x=1.
        z=y
        y =x
        exogenous
        t=1.0
        MaxTime=3""")
     obj.ExtractVariableList()
     self.assertEqual([
         ('t', '1.0'),
     ], obj.Parser.Exogenous)
     # Turn it into a float.
     obj.Parser.Exogenous = [('t', 1.)]
     obj.SetInitialConditions()
     self.assertEqual([1., 1., 1., 1.], obj.TimeSeries['t'])
Esempio n. 17
0
 def test_decoration1(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
       x=t
       w=y
       y=z
       z=x
       exogenous
       t=1.
       MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     self.assertEqual([1., 1., 1.], obj.TimeSeries['w'])
     self.assertEqual([1., 1., 1.], obj.TimeSeries['y'])
     self.assertEqual([1., 1., 1.], obj.TimeSeries['z'])
Esempio n. 18
0
 def test_SetInitialConditions_1(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x
      z(0) = sqrt(20.)
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     v = obj.TimeSeries
     self.assertEqual([
         10.,
     ] * 4, v['t'])
     self.assertEqual([0, 1, 2, 3], v['k'])
     self.assertEqual([10.], v['x'])
     self.assertEqual([math.sqrt(20)], v['z'])
Esempio n. 19
0
 def test_FailLog0_maxiter(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     # This test is used to hit the case where we do not converge
     obj.ParseString("""
      z = t
      y = z
      w = y
      x = log10(0)
      exogenous
      t=[10., 11., 12.]
      MaxTime=2""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.MaxIterations = 1
     #obj.SolveStep(1)
     with self.assertRaises(ValueError):
         obj.SolveStep(1)
Esempio n. 20
0
 def test_SolveStep_1(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     self.assertEqual([10., 10.], obj.TimeSeries['x'])
     # Note that equation does not hold at t=0
     self.assertEqual([2., 11.], obj.TimeSeries['z'])
     obj.SolveStep(2)
     self.assertEqual([10., 10., 10.], obj.TimeSeries['x'])
     # Note that equation does not hold at t=0
     self.assertEqual([2., 11., 11.], obj.TimeSeries['z'])
Esempio n. 21
0
    def test_SolveWithFunction(self):
        obj = EquationSolver()
        obj.RunEquationReduction = False
        # By forcing 't' into the variable list, no automatic creation of time variables
        obj.ParseString("""
          x=t
          z=f(x)
          exogenous
          t=[10.]*20
          MaxTime=3""")
        obj.ExtractVariableList()
        obj.SetInitialConditions()

        def squarer(x):
            return x * x

        obj.AddFunction('f', squarer)
        obj.SolveStep(1)
        self.assertEqual([10., 10.], obj.TimeSeries['x'])
        # Note that equation does not hold at t=0
        self.assertEqual([0., 100.], obj.TimeSeries['z'])
        obj.SolveStep(2)
Esempio n. 22
0
 def test_initial_conditions_const_decoration(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
        x=1.
        z=y
        y =x
        exogenous
        t=1.0
        MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     self.assertTrue(len(obj.Parser.Decoration) > 0)
     self.assertEqual([
         1.,
     ], obj.TimeSeries['x'])
     self.assertEqual([
         1.,
     ], obj.TimeSeries['z'])
     self.assertEqual([
         1.,
     ], obj.TimeSeries['y'])
Esempio n. 23
0
 def test_init2(self):
     obj = EquationSolver('x=1.')
     self.assertIn('x', obj.Parser.AllEquations)
Esempio n. 24
0
 def test_ParseString(self):
     obj = EquationSolver()
     obj.ParseString('x=1.')
     self.assertIn('x', obj.Parser.AllEquations)
Esempio n. 25
0
 def test_csv_text_empty(self):
     obj = EquationSolver()
     # Make the time series int so we do not test how floats are formatted...
     obj.TimeSeries = sfc_models.utils.TimeSeriesHolder('k')
     targ = ''
     self.assertEqual(targ, obj.GenerateCSVtext())
Esempio n. 26
0
 def test_ParseString_2(self):
     obj = EquationSolver()
     if is_python_3:
         with self.assertWarns(expected_warning=SyntaxWarning):
             obj.ParseString('x= y = 1')
Esempio n. 27
0
 def test_init(self):
     obj = EquationSolver()
     self.assertEqual(0, len(obj.Parser.AllEquations))
Esempio n. 28
0
 def test_ExtractVariableList_1(self):
     obj = EquationSolver()
     obj.ExtractVariableList()
     self.assertEqual([], obj.VariableList)