The purpose of the present exercise is to generate a fractal with the method of Iterated Maps. This method is demonstrated in the Game of Chaos for an example.
Consider the initial point with coordinates (, ) = (0,0). We will produce 10,100 other points in sequence using the following procedure: the coordinates (, ) of a generic point will be computed using the coordinates (, ) of the previous point in the sequence using the following formulas:
and are coefficient that are assigned in the table below.
A | .85 | .04 | -.04 | .85 | .00 | 1.60 |
B | .20 | -.26 | .23 | .22 | .00 | 1.60 |
C | -.15 | .28 | .26 | .24 | .00 | .44 |
D | .00 | .00 | .00 | .16 | .00 | .00 |
Every time we generate a new point we select one of the rows at random in the table above and use the coefficients in that row to compute the coordinates of the point. The rows are not be selected with uniform probability, but with the following ones:
Finally, we then draw the last 10,000 point you generated.
Below is the code to execute the above detail exercise.
x1from numpy.random import choice
2
3def select_locator():
4 """Method to select the row randomly (w/ weights)"""
5 locator_list = ["A", "B", "C", "D"]
6 locator = choice(locator_list, 1, p=[0.85, 0.07, 0.07, 0.01])
7 return locator[0]
8
9def get_coefficients(locator):
10 """Method to return the coefficients to use"""
11 if locator == "A":
12 a = .85
13 b = .04
14 c = -.04
15 d = .85
16 e = 0.0
17 f = 1.6
18 return a,b,c,d,e,f
19 elif locator == "B":
20 a = .2
21 b = -.26
22 c = .23
23 d = .22
24 e = 0.0
25 f = 1.6
26 return a,b,c,d,e,f
27 elif locator == "C":
28 a = -.15
29 b = .28
30 c = .26
31 d = .24
32 e = 0.0
33 f = .44
34 return a,b,c,d,e,f
35 elif locator == "D":
36 a = 0.0
37 b = 0.0
38 c = 0.0
39 d = .16
40 e = 0.0
41 f = 0.0
42 return a,b,c,d,e,f
43
44def get_new_points(a,b,c,d,current_x,current_y):
45 """A function to calculate the new points location
46 based on the previous points location."""
47
48 new_x = a * current_x + b * current_y + e
49 new_y = c * current_x + d * current_y + f
50
51 return new_x, new_y
select_locator()
function works properly via quick simulation...xxxxxxxxxx
141import pandas as pd
2import matplotlib.pyplot as plt
3import seaborn as sns
4
5# Simulate 10000 draws and plot the histgram
6options = []
7for x in range(10000):
8 options.append(select_locator())
9
10plt.figure(figsize=(6,6))
11_ = pd.Series(options).value_counts().plot(kind = 'bar', zorder=3)
12
13plt.grid()
14sns.despine()
xxxxxxxxxx
251# Create original points
2x_vals = [0]
3y_vals = [0]
4
5# Number of new points to run...
6new_points = 10100
7
8# Calculate all the new points...
9for x in range(new_points):
10
11 # Get coefficient locator
12 locator = select_locator()
13
14 # Get the coefficients
15 a,b,c,d,e,f = get_coefficients(locator)
16
17 # Get most recent coordinates
18 current_x = x_vals[-1]
19 current_y = y_vals[-1]
20
21 # Pass coefficients and coordinates to calculate the new locations
22 new_x, new_y = get_new_points(a,b,c,d,current_x,current_y)
23
24 x_vals.append(new_x)
25 y_vals.append(new_y)
xxxxxxxxxx
71plt.figure(figsize=(10,10))
2
3plt.scatter(x_vals[:], y_vals[:], c="#005400")
4
5sns.despine(left=True, bottom=True)
6plt.yticks([])
7plt.xticks([])