-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangereligion.py
executable file
·148 lines (113 loc) · 4.09 KB
/
changereligion.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python3
"""
Copyright (c) 2016
This file is part of free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this file. If not, see <http://www.gnu.org/licenses/>.
"""
import random
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot') # makes graphs pretty
# initialisation
N = 100 # population size
A = 65 # initial number of believers A
B = N-A # initial number of believers B
MAX_TIME = 100
t = 0 # initial time
Ta = 1.0 # initial attractiveness of option A
Tb = 2.0 # initial attractiveness of option B
alpha = 0.1 # strength of the transmission process
believersA = []
believersB = []
believersA.append(A)
believersB.append(B)
def payoff(believers, Tx,Ty):
""" payoff is the interest of 'conversion' of believers from one option (religion) to another.
It depends on the current proportion between believers in the population.
And its attractiveness to believers (defined in the 'attractiveness' function).
"""
proportionBelievers = (believers / N)
attraction = (Tx) / (Ty + Tx)
return proportionBelievers * attraction
def attractiveness(Ta, Tb):
""" attractiveness is a dynamically changing feature of each cultural option.
The function is composed of the current value for each option (Ta, Tb)
and a small stochastic change defined by the function K
####### different options for modelling attractiveness ########
# OPTION 1 - fixed attractiveness
"""
Ka = 0.1
Kb = 0
Ta = Ta+Ka
Tb = Tb+Kb
return Ta, Tb
def attractiveness2(Ta, Tb):
"""
# OPTION 2 - gaussian noise with strong tail (lognormal distribution)
"""
Ka, Kb = np.random.normal(0, 1, 2)
diff = Ka-Kb
Ta += diff
Tb -= diff
return Ta, Tb
def attractiveness3(Ta, Tb):
"""
# OPTION 3 - anti-conformist behavior
# sort of lotka-volterra where diff of attractiveness is negatively correlated with diff of populations
# we use gamma to add some stochasticity
"""
Ka = 0
Kb = 0
diffPop = np.random.gamma(believersA[t], 1) - np.random.gamma(believersB[t], 1)
# if diffPop is negative it means that we have more believers of A than B
# so we have to promote Kb
if diffPop<0:
Ka = -diffPop
# else we should promote Ka
else:
Kb = diffPop
# add 1 to avoid dividing by 0 if both are 0
Ta += Ka
Tb += Kb
return Ta, Tb
while t < MAX_TIME:
""" Main loop. Repeat until stop condition is met.
1. define the current attractiveness of each option
2. define proportion of population swithching from B to A and vice versa
3. calculate current numbers of practicioners of each option
4. output the numbers to two lists for plotting
"""
# define the current attractiveness of each option
Ta, Tb = attractiveness2(Ta, Tb)
# calculate the change between believers A and B in the current time step
variationBA = payoff(A, Ta, Tb)
variationAB = payoff(B, Tb, Ta)
difference = variationBA - variationAB
# B -> A
if difference> 0:
variation = difference*B
# A -> B
else:
variation = difference*A
# control the pace of variation with alpha
variation = alpha*variation
# update the population
A = A + variation
B = B - variation
# save the values to a list for plotting
believersA.append(A)
believersB.append(B)
# time = time + 1
t+=1
# plot the results
plt.plot(believersA)
plt.plot(believersB)
plt.show()