<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Implementing McLennan-Tourky in Python
Daisuke Oyama
Faculty of Economics, University of Tokyo
Step1:  Univariate example
Step2:  Let us try the logistic function which is well known to generate chaotic behavior.
Step3:  Comare compute_fixed_point from quantecon
Step4:  Example 4.6
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import time
import numpy as np
import matplotlib.pyplot as plt
import quantecon as qe
from quantecon.compute_fp import _print_after_skip
from quantecon.game_theory import Player, NormalFormGame, lemke_howson
def compute_fixed_point_ig(f, x_init, error_tol=1e-3, max_iter=50, verbose=1,
                           *args, **kwargs):
    _skip = 1
    if verbose:
        start_time = time.time()
        _print_after_skip(_skip, it=None)
        
    x_new = x_init
    iterate = 0
    y_new = f(x_new, *args, **kwargs)
    error = np.max(np.abs(y_new - x_new))
    
    if error <= error_tol or iterate >= max_iter:
        if verbose:
            etime = time.time() - start_time
            _print_after_skip(_skip, iterate, error, etime)
        return x_new
    
    X = np.array([x_new])
    Y = np.array([y_new])
    x_new = Y[0]
    iterate += 1
    
    while True:
        y_new = f(x_new, *args, **kwargs)
        error = np.max(np.abs(y_new - x_new))
        if error <= error_tol or iterate >= max_iter:
            break
            
        X = np.append(X, np.expand_dims(x_new, axis=0), axis=0)
        Y = np.append(Y, np.expand_dims(y_new, axis=0), axis=0)
        
        m = len(X)
        D = np.expand_dims(X, axis=1) - Y
        D *= D
        A = np.add.reduce(np.atleast_3d(D), axis=-1) * (-1)
        B = np.identity(m)
        g = NormalFormGame((Player(A), Player(B)))
        _, rho = lemke_howson(g, init_pivot=m-1)
        
        x_new = rho.dot(Y)
        iterate += 1
    
    if verbose:
        etime = time.time() - start_time
        _print_after_skip(_skip, iterate, error, etime)
        
    return x_new
Explanation: Implementing McLennan-Tourky in Python
Daisuke Oyama
Faculty of Economics, University of Tokyo
End of explanation
# Just a warmup
compute_fixed_point_ig(lambda x: 0.5*x, 1)
Explanation: Univariate example
End of explanation
def logistic(x, r):
    return r * x * (1 - x)
x = np.linspace(0, 1, 100)
y = logistic(x, r=4)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.plot([0, 1], [0, 1], ':', color='k')
ax.set_aspect(1)
plt.show()
tol = 1e-5
x_init = 0.99
compute_fixed_point_ig(logistic, x_init, error_tol=tol, r=4)
Explanation: Let us try the logistic function which is well known to generate chaotic behavior.
End of explanation
qe.compute_fixed_point(logistic, x_init, error_tol=tol, r=4)
Explanation: Comare compute_fixed_point from quantecon:
End of explanation
def f(x, M, c):
    return -np.arctan(np.dot(M, (x - c)**3)) + c
x_min, x_max = -np.pi/2, np.pi/2
x = np.linspace(x_min, x_max, 100)
M = np.abs(np.random.randn())
c = 0
y = f(x, M, c)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlim(x_min, x_max)
ax.set_ylim(x_min, x_max)
ax.plot([x_min, x_max], [x_min, x_max], ':', color='k')
ax.set_aspect(1)
plt.show()
n = 500
tol = 1e-5
max_iter = 200
num_trials = 3
for i in range(num_trials):
    print("===== Experiment {} =====\n".format(i))
    c = np.random.standard_normal(n)
    M = np.abs(np.random.standard_normal(size=(n, n)))
    x_init = (np.random.rand(n)-1/2)*np.pi + c
    
    print("***Imitation game***")
    x0 = compute_fixed_point_ig(f, x_init, tol, max_iter, M=M, c=c)
    print("")
    
    print("***Function iteration***")
    x1 = qe.compute_fixed_point(f, x_init, tol, max_iter, verbose=1, print_skip=200, M=M, c=c)
    print("")
num_trials = 3
for i in range(num_trials):
    print("===== Experiment {} =====\n".format(i))
    c = np.random.standard_normal(n)
    M = np.random.normal(0, 1/13, size=(n, n))
    x_init = (np.random.rand(n)-1/2)*np.pi + c
    
    print("***Imitation game***")
    x0 = compute_fixed_point_ig(f, x_init, tol, max_iter, M=M, c=c)
    print("")
    
    print("***Function iteration***")
    x1 = qe.compute_fixed_point(f, x_init, tol, max_iter, verbose=1, print_skip=200, M=M, c=c)
    print("")
Explanation: Example 4.6: 500-variable example
End of explanation
<END_TASK>