|
15 | 15 | from __future__ import division
|
16 | 16 | import unittest
|
17 | 17 | import numpy as np
|
| 18 | +from nose.tools import ok_, raises |
18 | 19 | from quantecon import compute_fixed_point
|
19 | 20 |
|
20 | 21 |
|
@@ -91,3 +92,53 @@ def test_imitation_game_method(self):
|
91 | 92 | abs(self.T(fp_computed, mu=mu) - fp_computed).max() <=
|
92 | 93 | error_tol
|
93 | 94 | )
|
| 95 | + |
| 96 | + |
| 97 | +class TestComputeFPContraction(): |
| 98 | + def setUp(self): |
| 99 | + self.coeff = 0.5 |
| 100 | + self.methods = ['iteration', 'imitation_game'] |
| 101 | + |
| 102 | + def f(self, x): |
| 103 | + return self.coeff * x |
| 104 | + |
| 105 | + def test_num_iter_one(self): |
| 106 | + init = 1. |
| 107 | + error_tol = self.coeff |
| 108 | + |
| 109 | + for method in self.methods: |
| 110 | + fp_computed = compute_fixed_point(self.f, init, |
| 111 | + error_tol=error_tol, |
| 112 | + method=method) |
| 113 | + ok_(fp_computed <= error_tol * 2) |
| 114 | + |
| 115 | + def test_num_iter_large(self): |
| 116 | + init = 1. |
| 117 | + buff_size = 2**8 # buff_size in 'imitation_game' |
| 118 | + max_iter = buff_size + 2 |
| 119 | + error_tol = self.coeff**max_iter |
| 120 | + |
| 121 | + for method in self.methods: |
| 122 | + fp_computed = compute_fixed_point(self.f, init, |
| 123 | + error_tol=error_tol, |
| 124 | + max_iter=max_iter, method=method, |
| 125 | + print_skip=max_iter) |
| 126 | + ok_(fp_computed <= error_tol * 2) |
| 127 | + |
| 128 | + def test_2d_input(self): |
| 129 | + error_tol = self.coeff**4 |
| 130 | + |
| 131 | + for method in self.methods: |
| 132 | + init = np.array([[-1, 0.5], [-1/3, 0.1]]) |
| 133 | + fp_computed = compute_fixed_point(self.f, init, |
| 134 | + error_tol=error_tol, |
| 135 | + method=method) |
| 136 | + ok_((fp_computed <= error_tol * 2).all()) |
| 137 | + |
| 138 | + |
| 139 | +@raises(ValueError) |
| 140 | +def test_raises_value_error_nonpositive_max_iter(): |
| 141 | + f = lambda x: 0.5*x |
| 142 | + init = 1. |
| 143 | + max_iter = 0 |
| 144 | + fp = compute_fixed_point(f, init, max_iter=max_iter) |
0 commit comments