## 6.13
## initial values
stockA = 100
stockB = 100
addA = 0.05
removeA = 0.05
addA_dependingB = 0.01
addB = 0.05
removeB = 0.05
addB_dependingA = 0.01
for round in range(1, 51):
stockA = stockA + stockA * addA + stockB * addA_dependingB
stockA = stockA - stockA * removeA
stockB = stockB + stockB * addB + stockB * addB_dependingA
stockB = stockB - stockB * removeB
print( stockA, stockB )
141.733831528246 141.733831528246
## 6.14
## initial values
stockA = 100
stockB = 100
addA = 0.05
removeA = 0.05
addA_dependingB = 0.01
addB = 0.05
removeB = 0.05
addB_dependingA = 0.01
listA = []
listB = []
for round in range(1, 51):
listA.append( stockA )
listB.append( stockB )
stockA = stockA + stockA * addA + stockB * addA_dependingB
stockA = stockA - stockA * removeA
stockB = stockB + stockB * addB + stockB * addB_dependingA
stockB = stockB - stockB * removeB
print( listA )
print( listB )
[100, 100.7, 101.4049, 102.1147343, 102.8295374401, 103.54934420218069, 104.27418961159594, 105.00410893887711, 105.73913770144924, 106.47931166535938, 107.22466684701688, 107.975239514946, 108.73106619155062, 109.49218365489148, 110.25862894047572, 111.03043934305904, 111.80765241846046, 112.59030598538968, 113.37843812728741, 114.17208719417843, 114.97129180453767, 115.77609084716943, 116.58652348309963, 117.40262914748133, 118.22444755151372, 119.0520186843743, 119.88538281516492, 120.72458049487108, 121.56965255833516, 122.42064012624351, 123.2775846071272, 124.14052769937707, 125.00951139327269, 125.8845779730256, 126.76577001883676, 127.65313040896862, 128.5467023218314, 129.4465292380842, 130.35265494275077, 131.26512352735003, 132.1839793920415, 133.10926724778577, 134.0410321185203, 134.97931934334994, 135.9241745787534, 136.87564380080465, 137.83377330741027, 138.79860972056213, 139.77019998860607, 140.7485913885263] [100, 100.7, 101.4049, 102.1147343, 102.8295374401, 103.54934420218069, 104.27418961159594, 105.00410893887711, 105.73913770144924, 106.47931166535938, 107.22466684701688, 107.975239514946, 108.73106619155062, 109.49218365489148, 110.25862894047572, 111.03043934305904, 111.80765241846046, 112.59030598538968, 113.37843812728741, 114.17208719417843, 114.97129180453767, 115.77609084716943, 116.58652348309963, 117.40262914748133, 118.22444755151372, 119.0520186843743, 119.88538281516492, 120.72458049487108, 121.56965255833516, 122.42064012624351, 123.2775846071272, 124.14052769937707, 125.00951139327269, 125.8845779730256, 126.76577001883676, 127.65313040896862, 128.5467023218314, 129.4465292380842, 130.35265494275077, 131.26512352735003, 132.1839793920415, 133.10926724778577, 134.0410321185203, 134.97931934334994, 135.9241745787534, 136.87564380080465, 137.83377330741027, 138.79860972056213, 139.77019998860607, 140.7485913885263]
## 6.15a
agent_red_neighbours = [0, 5, 7, 4]
agent_blue_neighbours = [2, 7, 4, 3]
agent_own_colour = ['blue', 'red', 'blue', 'red']
for colour in agent_own_colour:
print( colour )
blue red blue red
## 6.15b
agent_red_neighbours = [0, 5, 7, 4]
agent_blue_neighbours = [2, 7, 4, 3]
agent_own_colour = ['blue', 'red', 'blue', 'red']
for index in range( 0, len( agent_own_colour ) ):
print( agent_own_colour[index], agent_red_neighbours[index], agent_blue_neighbours[index] )
blue 0 2 red 5 7 blue 7 4 red 4 3
## 6.15c
agent_red_neighbours = [0, 5, 7, 4]
agent_blue_neighbours = [2, 7, 4, 3]
agent_own_colour = ['blue', 'red', 'blue', 'red']
for index in range( 0, len( agent_own_colour ) ):
neigbours_total = agent_red_neighbours[index] + agent_blue_neighbours[index]
if( agent_own_colour[index] == "red" ):
if agent_blue_neighbours[index] > neigbours_total / 2:
print("agent", index, "moves")
if( agent_own_colour[index] == "blue" ):
if agent_red_neighbours[index] > neigbours_total / 2:
print("agent", index, "moves")
## remember that index starts from 0, so agent 1 is actually the second agent etc
agent 1 moves agent 2 moves
## 6.16
data = []
for row in open("microsimulation.csv"):
row = row.strip().split(',')
entry = {}
entry['id'] = float( row[0] )
entry['sex'] = row[1]
entry['age'] = float( row[2] )
entry['number_of_children'] = float( row[3] )
entry['income'] = float( row[4] )
entry['tax'] = float( row[5] )
data.append( entry )
## 6.17
## age bins
agebin = {}
## this a dummy approach for this
agebin['<25'] = 0
agebin['25-49'] = 0
agebin['50-74'] = 0
agebin['>74'] = 0
for entry in data:
age = entry['age']
if age < 25:
agebin['<25'] = agebin['<25'] + 1
if age >= 25 and age < 50:
agebin['25-49'] = agebin['25-49'] + 1
if age >= 50 and age < 75:
agebin['50-74'] = agebin['50-74'] + 1
if age > 74:
agebin['>74'] = agebin['>74'] + 1
for bin_name, count in agebin.items():
print( bin_name, '*' * count )
<25 * 25-49 *** 50-74 >74 *
## 6.18
## simple rule to age people
for year in range(50):
for entry in data:
entry['age'] = entry['age'] + 1
## reusing code from prior 6.17
## age bins
agebin = {}
## this a dummy approach for this
agebin['<25'] = 0
agebin['25-49'] = 0
agebin['50-74'] = 0
agebin['>74'] = 0
for entry in data:
age = entry['age']
if age < 25:
agebin['<25'] = agebin['<25'] + 1
if age >= 25 and age < 50:
agebin['25-49'] = agebin['25-49'] + 1
if age >= 50 and age < 75:
agebin['50-74'] = agebin['50-74'] + 1
if age > 74:
agebin['>74'] = agebin['>74'] + 1
for bin_name, count in agebin.items():
print( bin_name, '*' * count )
<25 25-49 50-74 >74 *****