Skip to content

Commit fd1911f

Browse files
authored
Merge pull request #8 from daimpi/wip-computation-performance
Wip computation performance
2 parents 6fd56a7 + cbfa7e0 commit fd1911f

File tree

3 files changed

+138
-99
lines changed

3 files changed

+138
-99
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ B/c the normal distribution is a continuous distribution the outcome is rounded
1818

1919
## Variables
2020

21-
Default-values have been set to mirror Zollman's (2010) model. The slider ranges are mostly set to mirror the ranges considered by Rosenstock et al. (2016). The exceptions are:
21+
Default-values have been set to mirror Zollman's (2010) model. The slider ranges are mostly set to mirror the ranges considered by Rosenstock et al. (2016) ( pulls correspond to `n` in Rosenstock et al.(2016)). The exceptions are:
2222

2323
* The signal ranges have a larger interval
24-
* The pulls range doesn't start at 1 but at 100 because of the normal approximation potentially not being accurate enough for low numbers of pulls. ( pulls correspond to `n` in Rosenstock et al.(2016))
24+
2525

2626
### Globals
2727

SocNetABM.nlogo

Lines changed: 55 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
turtles-own [a b theory-jump times-jumped cur-best-th current-theory-info
2-
mytheory successes subj-th-i-signal crit-interact-lock confidence]
2+
mytheory successes subj-th-i-signal crit-interact-lock confidence
3+
avg-neighbor-signal]
34

45
globals [th-i-signal indiff-count crit-interactions-th1 crit-interactions-th2
5-
confidence-cutoff converged-ticks last-converged-th max-confidence
6+
confidence-cutoff converged-ticks last-converged-th
67
max-ticks converge-reporters converge-reporters-values
7-
run-start-scientists-save rndseed]
8+
run-start-scientists-save rndseed g-confidence g-depressed-confidence]
89

910
__includes ["protocol.nls"]
1011

@@ -27,12 +28,11 @@ to setup [rs]
2728
let a2 init-ab
2829
let b2 init-ab
2930
set a list a1 a2
30-
; b contains the denominator of the mean of the beta distribution (i.e.
31-
; alpha + beta): the first (second) entry is the denominator for
31+
; b contains the denominator of the mean of the beta distribution (i.e.
32+
; alpha + beta): the first (second) entry is the denominator for
3233
; theory 1 (2).
3334
set b (list (a1 + b1) (a2 + b2))
34-
set current-theory-info [0 0]
35-
; calculate the prior (i.e. mean of the beta distribution) from the random
35+
; calculate the prior (i.e. mean of the beta distribution) from the random
3636
; alphas / betas.
3737
calc-posterior
3838
compute-strategies
@@ -92,8 +92,7 @@ end
9292

9393
; initializes the hidden variables (= not set in the interface)
9494
to init-hidden-variables
95-
set confidence-cutoff 10 ^ 4
96-
set max-confidence 10 ^ 5
95+
set confidence-cutoff 1 - (1 / 10 ^ 4)
9796
set max-ticks 10000
9897
end
9998

@@ -186,6 +185,15 @@ end
186185

187186

188187

188+
; reports a draw from the binomial distribution with n pulls and probability p
189+
to-report binomial [n p]
190+
report length filter [ ?1 -> ?1 < p ] n-values n [random-float 1]
191+
end
192+
193+
194+
195+
196+
189197
; Researchers pull from their respective slot machine:
190198
; the binomial distribution is approximated by the normal distribution with
191199
; the same mean and variance. This approximation is highly accurate for all
@@ -197,6 +205,7 @@ end
197205
to pull
198206
let mysignal item mytheory subj-th-i-signal
199207
set successes [0 0]
208+
ifelse pulls > 100 [
200209
let successes-normal round random-normal
201210
(pulls * mysignal) sqrt (pulls * mysignal * (1 - mysignal) )
202211
ifelse successes-normal > 0 and successes-normal <= pulls [
@@ -206,6 +215,10 @@ to pull
206215
set successes replace-item mytheory successes pulls
207216
]
208217
]
218+
][
219+
let successes-binomial binomial pulls mysignal
220+
set successes replace-item mytheory successes successes-binomial
221+
]
209222
end
210223

211224

@@ -223,7 +236,7 @@ end
223236

224237

225238

226-
; the sharing of information between researchers optionally including
239+
; the sharing of information between researchers optionally including
227240
; critical-interaction
228241
to share
229242
let cur-turtle self
@@ -234,7 +247,7 @@ to share
234247
ask link-neighbors [
235248
ifelse mytheory = cur-turtle-th or not critical-interaction [
236249
set successvec (map + successvec successes)
237-
set pullcounter replace-item mytheory pullcounter
250+
set pullcounter replace-item mytheory pullcounter
238251
(item mytheory pullcounter + pulls)
239252
][
240253
let other-successes successes
@@ -243,7 +256,7 @@ to share
243256
if other-success-ratio > item mytheory [current-theory-info] of
244257
cur-turtle [
245258
ask cur-turtle [
246-
evaluate-critically
259+
evaluate-critically
247260
]
248261
]
249262
ask cur-turtle [
@@ -262,7 +275,7 @@ end
262275

263276

264277
; If researchers communicate with researchers from another theory they might
265-
; interact critically
278+
; interact critically
266279
to evaluate-critically
267280
let actual-prob-suc 0
268281
ifelse mytheory = 0 [
@@ -283,7 +296,7 @@ end
283296

284297

285298

286-
; calculates from a given a (= alpha) and b (= alpha + beta) the belief of a
299+
; calculates from a given a (= alpha) and b (= alpha + beta) the belief of a
287300
; researcher i.e. the mean of the beta distribution
288301
to calc-posterior
289302
set current-theory-info (map / a b)
@@ -335,74 +348,6 @@ to set-researcher-colors
335348
set color turquoise
336349
]
337350
end
338-
339-
340-
341-
342-
343-
; Calculates how confident the researcher is in the fact that her current best
344-
; theory is actually the best theory (i.e. how unlikely it is that she will
345-
; change her mind). This calculation only makes sense in case researchers have
346-
; converged.
347-
to calc-confidence
348-
let worst-signal [item mytheory subj-th-i-signal] of min-one-of turtles [
349-
item mytheory subj-th-i-signal]
350-
ask turtles [
351-
let belief-to-beat item ((mytheory + 1) mod 2) current-theory-info
352-
* strategy-threshold
353-
let cur-theory mytheory
354-
let avg-neighbor-signal mean [item cur-theory subj-th-i-signal] of
355-
(turtle-set link-neighbors self)
356-
; if the scientist would be given sufficient time for her belief to
357-
; converge to the average signal of her and her link-neighbors, would
358-
; this be enough for her to abandon her current theory? If so, she's not
359-
; confident enough.
360-
if avg-neighbor-signal <= belief-to-beat [
361-
set confidence 0
362-
stop
363-
]
364-
; the following calculations are based on probability maximization of the
365-
; normal-distribution. This is separately documented at [placeholder].
366-
let alpha item mytheory a
367-
let varepsilon avg-neighbor-signal - belief-to-beat
368-
let delta item mytheory current-theory-info - belief-to-beat
369-
if (2 * alpha - 1) * delta <= belief-to-beat [
370-
set confidence 0
371-
stop
372-
]
373-
let exit-probability 0.5 + 0.5 * erf (
374-
((0 - 2 * alpha + 1) * delta + belief-to-beat)
375-
/ (sqrt((0 - 2 * alpha * delta + delta + belief-to-beat)
376-
* (belief-to-beat + varepsilon) * (0 - 1 + belief-to-beat + varepsilon)
377-
/ (varepsilon * (belief-to-beat + delta)))
378-
* (belief-to-beat + delta)))
379-
ifelse exit-probability > 0 [
380-
set confidence 1 / exit-probability
381-
if confidence > max-confidence [
382-
set confidence max-confidence
383-
]
384-
][
385-
set confidence max-confidence
386-
]
387-
]
388-
end
389-
390-
391-
392-
393-
394-
; reports a numerical approximation for the error-function function on its
395-
; negative domain, therefore the argument (x) must be smaller than 0. For
396-
; sources see infotab.
397-
to-report erf [x]
398-
let t (1 - .5 * x)
399-
report exp ( 0 - x ^ 2 - 1.26551223 + 1.00002368 / t
400-
+ .37409196 / t ^ 2 + 0.09678418 / t ^ 3
401-
- .18628806 / t ^ 4 + .27886807 / t ^ 5
402-
- 1.13520398 / t ^ 6 + 1.48851587 / t ^ 7
403-
- .82215223 / t ^ 8 + .17087277 / t ^ 9)
404-
/ t - 1
405-
end
406351
@#$#@#$#@
407352
GRAPHICS-WINDOW
408353
210
@@ -468,10 +413,10 @@ SLIDER
468413
288
469414
pulls
470415
pulls
471-
100
416+
1
472417
6000
473418
1000.0
474-
100
419+
1
475420
1
476421
NIL
477422
HORIZONTAL
@@ -678,10 +623,10 @@ B/c the normal distribution is a continuous distribution the outcome is rounded
678623

679624
## Variables
680625

681-
Default-values have been set to mirror Zollman's (2010) model. The slider ranges are mostly set to mirror the ranges considered by Rosenstock et al. (2016). The exceptions are:
626+
Default-values have been set to mirror Zollman's (2010) model. The slider ranges are mostly set to mirror the ranges considered by Rosenstock et al. (2016) ( pulls correspond to `n` in Rosenstock et al.(2016)). The exceptions are:
682627

683628
* The signal ranges have a larger interval
684-
* The pulls range doesn't start at 1 but at 100 because of the normal approximation potentially not being accurate enough for low numbers of pulls. ( pulls correspond to `n` in Rosenstock et al.(2016))
629+
685630

686631
### Globals
687632

@@ -709,9 +654,10 @@ The sum of critical interactions scientists on theory 1(2) encountered.
709654
#### confidence-cutoff
710655

711656
* type: integer
712-
* example: 10000
657+
* example: 0.9999
658+
659+
The global-confidence `g-confidence` must be higher than this value for the run to be terminated.
713660

714-
A hidden variable: the confidence that the least confident scientist has to reach before the run is terminated.
715661

716662
#### converged-ticks
717663

@@ -727,13 +673,6 @@ The number of ticks which have passed since the researchers converged for the la
727673

728674
The theory the researchers converged on the last time they converged: 0 = th1, 1 = th2
729675

730-
#### max-confidence
731-
732-
* type: integer
733-
* example: 100000
734-
735-
A hidden variable: the maximal confidence a researcher can reach.
736-
737676
#### max-ticks
738677

739678
* type: integer
@@ -769,6 +708,20 @@ The number of scientists on [th1 th2] at the beginning of the run.
769708

770709
Stores the random-seed of the current run.
771710

711+
#### g-confidence
712+
713+
* format: float
714+
* example: 0.9993
715+
716+
Global-confidence: the probability that not a single researcher will switch theories i.e. the probability that this convergence is final. Range: [0,1]
717+
718+
#### g-depressed-confidence
719+
720+
* format: boolean
721+
* example: false
722+
723+
If there is a researcher for whom, if given sufficient time for her belief to converge to the average signal of her and her link-neighbors, this would this be enough to abandon her current theory, her confidence will always be zero and therefore `g-confidence` will also be zero. In this case `g-depressed-confidence` will be set to true in order to avoid redundant confidence calculations.
724+
772725

773726
### Turtles-own
774727

@@ -848,6 +801,13 @@ For how many more rounds the researcher is blocked from pursuing strategies (i.e
848801

849802
How confident the researcher is in the fact that her current best theory is actually the best theory (i.e. how unlikely it is that she will change her mind). Only calculated once all researchers have converged to one theory.
850803

804+
#### avg-neighbor-signal
805+
806+
* type: float
807+
* example: 0.499
808+
809+
Only set once all researchers converged. This is the average signal the researcher and her link-neighbors currently observe for the theory they converged on.
810+
851811

852812
## CREDITS AND REFERENCES
853813

protocol.nls

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,85 @@ end
6262

6363

6464

65+
; Calculates how confident the researcher is in the fact that her current best
66+
; theory is actually the best theory (i.e. how unlikely it is that she will
67+
; change her mind). This calculation only makes sense in case researchers have
68+
; converged.
69+
to calc-confidence
70+
if converged-ticks = 0 [
71+
set g-depressed-confidence false
72+
]
73+
if g-depressed-confidence [stop]
74+
let stop? false
75+
ask turtles [
76+
if stop? [stop]
77+
let belief-to-beat item ((mytheory + 1) mod 2) current-theory-info
78+
* strategy-threshold
79+
let cur-theory mytheory
80+
if converged-ticks = 0 [
81+
set avg-neighbor-signal mean [item cur-theory subj-th-i-signal] of
82+
(turtle-set link-neighbors self)
83+
]
84+
; if the scientist would be given sufficient time for her belief to
85+
; converge to the average signal of her and her link-neighbors, would
86+
; this be enough for her to abandon her current theory? If so, she's not
87+
; confident enough.
88+
if avg-neighbor-signal <= belief-to-beat [
89+
set confidence 0
90+
set stop? true
91+
set g-depressed-confidence true
92+
stop
93+
]
94+
; the following calculations are based on probability maximization of the
95+
; normal-distribution. This is separately documented at [placeholder].
96+
let alpha item mytheory a
97+
let varepsilon avg-neighbor-signal - belief-to-beat
98+
let delta item mytheory current-theory-info - belief-to-beat
99+
if (2 * alpha - 1) * delta <= belief-to-beat [
100+
set confidence 0
101+
set stop? true
102+
stop
103+
]
104+
let exit-probability 0.5 + 0.5 * erf (
105+
((0 - 2 * alpha + 1) * delta + belief-to-beat)
106+
/ (sqrt((0 - 2 * alpha * delta + delta + belief-to-beat)
107+
* (belief-to-beat + varepsilon) * (0 - 1 + belief-to-beat + varepsilon)
108+
/ (varepsilon * (belief-to-beat + delta)))
109+
* (belief-to-beat + delta)))
110+
ifelse exit-probability > 0 [
111+
set confidence 1 - exit-probability
112+
][
113+
set confidence 1
114+
]
115+
]
116+
ifelse stop? [
117+
set g-confidence 0
118+
][
119+
set g-confidence reduce * [confidence] of turtles
120+
]
121+
end
122+
123+
124+
125+
126+
127+
; reports a numerical approximation for the error-function function on its
128+
; negative domain, therefore the argument (x) must be smaller than 0. For
129+
; sources see infotab.
130+
to-report erf [x]
131+
let t (1 - .5 * x)
132+
report exp ( 0 - x ^ 2 - 1.26551223 + 1.00002368 / t
133+
+ .37409196 / t ^ 2 + 0.09678418 / t ^ 3
134+
- .18628806 / t ^ 4 + .27886807 / t ^ 5
135+
- 1.13520398 / t ^ 6 + 1.48851587 / t ^ 7
136+
- .82215223 / t ^ 8 + .17087277 / t ^ 9)
137+
/ t - 1
138+
end
139+
140+
141+
142+
143+
65144
; reports 1 if scientists converged on the best theory (th1) by the end of the
66145
; run, 0 otherwise
67146
to-report successful-run
@@ -227,10 +306,10 @@ end
227306
to-report average-confidence [rec?]
228307
let identifier "avgconfidence"
229308
ifelse rec? [
230-
if not converged [
309+
if ticks >= max-ticks and not converged [
231310
report (list identifier 0)
232311
]
233-
let avg-confidence mean [confidence] of turtles
312+
let avg-confidence g-confidence
234313
report (list identifier avg-confidence)
235314
][
236315
let avg-confidence item 1 first filter [curitem ->

0 commit comments

Comments
 (0)