forked from QuantEcon/QuantEcon.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_fp.py
35 lines (28 loc) · 1011 Bytes
/
compute_fp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Origin: QE by John Stachurski and Thomas J. Sargent
Filename: compute_fp.py
Authors: John Stachurski and Thomas Sargent
LastModified: 11/08/2013
Compute the fixed point of a given operator T, starting from
specified initial condition v.
"""
import numpy as np
def compute_fixed_point(T, specs, v, error_tol=1e-3, max_iter=50, verbose=1):
"""
Computes and returns T^k v, where T is an operator, v is an initial
condition and k is the number of iterates. Provided that T is a
contraction mapping or similar, T^k v will be an approximation to the
fixed point.
The convention for using this function is that T can be called as
new_v = T(specs, v).
"""
iterate = 0
error = error_tol + 1
while iterate < max_iter and error > error_tol:
new_v = T(specs, v)
iterate += 1
error = np.max(np.abs(new_v - v))
if verbose:
print "Computed iterate %d with error %f" % (iterate, error)
v = new_v
return v