turtles-own [opinion eps opinion-list extremist?] ;; eps is the bound of confidence, opinion-list is to hold the list of the last max-pxcor opinions, extremist? checks if the turtle is in an opinion interval of extremism globals [ current_min_eps current_max_eps current_alpha current_beta] ;; variables to display the parameters of the beta distribution from which the current eps-values are drawn ;; the slider-variables min_eps, max_eps, alpha, beta are changed to current_... when new_confidence_bounds is called ;; BUTTON PROCEDURES to setup clear-all ask patches [set pcolor white] create-turtles number_of_agents ask turtles [ set opinion new-opinion set opinion-list (list opinion) setxy 0 (opinion * max-pycor) ] new_confidence_bounds ;; see procedure reset-ticks end to go ask turtles [ set opinion-list lput opinion opinion-list ] ;; put opinion after opinion-list to use it as "old" value for simulatanous update in HK ifelse (communication_regime = "DW (select one)") and (original = true) [repeat count turtles [ask one-of turtles [ update-opinion ]]] ;; in original DW we chose N random pairs each tick [ask turtles [ update-opinion ]] ;; in all other versions there is an update for each agent every tick ;; for "update-opinion" see the procedure ask turtles [ set opinion-list replace-item ( length opinion-list - 1) opinion-list opinion ] ;; update the opinion-list ask turtles [ if (length opinion-list = max-pxcor + 1) [ set opinion-list butfirst opinion-list ] ] ;; cut oldest values for "rolling" opinion list ask turtles [ entry-exit ] ;; see the procedure draw-trajectories ;; see the procedure tick end to new_confidence_bounds set current_min_eps min_eps set current_max_eps max_eps set current_alpha alpha set current_beta beta ask turtles [ let x random-gamma alpha 1 set eps ( x / ( x + random-gamma beta 1) ) ;; set eps a random number from distribution Beta(alpha,beta) (between 0 and 1) set eps min_eps + (eps * (max_eps - min_eps)) ;; scale and shift eps to lie between min_eps and max_eps set color colorcode eps 0.5 ;; see reporter colorcode ] update-plots end ;; INTERNAL PROCEDURES to update-opinion ;; detect if in extremism zone if (extremism_type = "two side") [set extremist? (0.5 - abs(opinion - 0.5) < extremism_range)] if (extremism_type = "one side") [set extremist? (opinion < extremism_range)] ;; change of opinion if (communication_regime = "DW (select one)") [ ;; adjust opinion with random partner let partner one-of turtles if (abs (opinion - [opinion] of partner) < eps ) and (not extremist?) [ set opinion (opinion + [opinion] of partner) / 2 if (original = true) [ ;; also partner updates opinion, this increases number of average activities of agents per time step from 1 to 2 ask partner [ if (extremism_type = "two side") [set extremist? (0.5 - abs(opinion - 0.5) < extremism_range)] if (extremism_type = "one side") [set extremist? (opinion < extremism_range)] if (abs (opinion - [opinion] of myself) < eps ) and (not extremist?) [set opinion (opinion + [opinion] of myself) / 2] ] ] ] ] if (communication_regime = "HK (select all)") [ ;; adjust opinion to mean of all in agents closer than eps if not extremist? [ ifelse (original = true) [ set opinion aggregate ( filter [abs( ? - last opinion-list) < eps] [last opinion-list] of turtles ) ] [ set opinion aggregate ( filter [abs( ? - opinion) < eps] [opinion] of turtles ) ] ;; for aggregate see the reporter ] ] end to entry-exit ;; randomly reset opinion with probability entry_exit_rate if (random-float 1 < entry_exit_rate) [set opinion new-opinion] end to draw-trajectories ;; let turtles move with their opinion trajectories from left to right across the world drawing trajectories or coloring patches clear-drawing ask turtles [ pen-up setxy 0 (item 0 opinion-list * max-pycor) ] ifelse (visualization = "Colored histogram over time") [ ask turtles [ pen-up ] ] [ask turtles [ pen-down ] ] let t-counter 1 while [ t-counter < (length ( [opinion-list] of turtle 1 )) ] [ ask turtles [setxy t-counter (item t-counter opinion-list * max-pycor)] ifelse (visualization = "Colored histogram over time") [ ask patches with [pxcor = t-counter ] [ set pcolor colorcode ((count turtles-here) / number_of_agents) 0.2 ] ] ;; see reporter colorcode [ ask patches [ set pcolor white ] ] set t-counter t-counter + 1 ] end ;; REPORTERS to-report new-opinion report random-float 1 end to-report aggregate [opinions] if (aggregation_in_HK = "mean") [report mean opinions] if (aggregation_in_HK = "median") [report median opinions] end to-report colorcode [x max_x] ;; report a color as "x=0 --> violet", "x=max_x --> red" on the color axis violet,blue,cyan,green,yellow,orange,red report hsb (190 - 190 * (x / max_x)) 255 255 end ;; Copyright 2012 Jan Lorenz ;; See Info tab for full copyright and license @#$#@#$#@ GRAPHICS-WINDOW 276 10 698 300 -1 -1 3.41 1 10 1 1 1 0 0 0 1 0 120 0 75 1 1 1 ticks 30.0 BUTTON 71 28 163 61 NIL setup NIL 1 T OBSERVER NIL NIL NIL NIL 1 BUTTON 168 28 267 61 NIL go T 1 T OBSERVER NIL NIL NIL NIL 1 SLIDER 15 91 266 124 number_of_agents number_of_agents 0 1000 200 1 1 NIL HORIZONTAL SWITCH 170 149 265 182 original original 1 1 -1000 CHOOSER 15 149 167 194 communication_regime communication_regime "HK (select all)" "DW (select one)" 0 CHOOSER 15 196 167 241 aggregation_in_HK aggregation_in_HK "mean" "median" 0 PLOT 14 377 203 513 Histogram eps eps NIL 0.0 1.0 0.0 30.0 false false "" "set-plot-y-range 0 round(number_of_agents / 8)" PENS "default" 0.02 1 -16777216 true "" "histogram [eps] of turtles" SLIDER 422 324 591 357 min_eps min_eps 0 1 0 0.01 1 NIL HORIZONTAL SLIDER 422 360 590 393 max_eps max_eps 0 1 0.5 0.01 1 NIL HORIZONTAL SLIDER 422 396 590 429 alpha alpha 0.01 6 2 0.01 1 NIL HORIZONTAL BUTTON 227 332 409 365 NIL new_confidence_bounds NIL 1 T OBSERVER NIL NIL NIL NIL 1 TEXTBOX 13 310 412 328 3. Setting Confidence Bounds: eps ~ Beta(alpha,beta,min,max) 12 0.0 1 MONITOR 16 28 66 73 N count turtles 17 1 11 MONITOR 14 328 64 373 min current_min_eps 17 1 11 MONITOR 67 328 117 373 max current_max_eps 17 1 11 MONITOR 120 328 170 373 alpha current_alpha 17 1 11 MONITOR 173 328 223 373 beta current_beta 17 1 11 PLOT 208 377 410 513 pdf beta-distribution eps NIL 0.0 1.0 0.0 0.0 true false "" "clear-plot" PENS "default" 1.0 0 -5825686 true "" "if (max_eps > min_eps) [\nplotxy min_eps 0\nforeach ( n-values 99 [ (? + 1) / 100 * (max_eps - min_eps) + min_eps] ) [plotxy ? ( ((? - min_eps) ^ (alpha - 1) * (max_eps - ?) ^ (beta - 1)) / ( max_eps - min_eps ) ^ (alpha + beta - 1) )]\nplotxy max_eps 0\n]" TEXTBOX 16 10 166 28 Setup and Go 12 0.0 1 SLIDER 610 423 767 456 extremism_range extremism_range 0 0.5 0 0.01 1 NIL HORIZONTAL CHOOSER 702 10 909 55 visualization visualization "Colored histogram over time" "Agents' trajectories" 0 CHOOSER 770 423 875 468 extremism_type extremism_type "one side" "two side" 1 PLOT 702 58 909 178 Histogram Current Opinion NIL 0.0 1.0 0.0 0.0 true false "" "set-plot-y-range 0 round(number_of_agents / 8)" PENS "default" 0.02 1 -13345367 true "" "histogram [opinion] of turtles" TEXTBOX 15 131 259 149 1. Communication and Opinion Update 12 0.0 1 MONITOR 435 467 503 512 eps mean (alpha * max_eps + beta * min_eps) / (alpha + beta) 3 1 11 MONITOR 505 467 571 512 eps mode ifelse-value ((alpha > 1) and (beta > 1)) [((alpha - 1) * max_eps + (beta - 1) * min_eps ) / (alpha + beta - 2)] [\"\"] 3 1 11 PLOT 702 180 909 300 Opinion NIL NIL 0.0 10.0 0.0 1.0 true true "" "" PENS "mean" 1.0 0 -16777216 true "" "plot mean [opinion] of turtles" "median" 1.0 0 -2674135 true "" "plot median [opinion] of turtles" SLIDER 423 432 590 465 beta beta 0.01 6 4 0.01 1 NIL HORIZONTAL TEXTBOX 71 63 263 90 Hint: number_of_agents-Slider (without running \"Setup\") changes coloraxis. 9 0.0 1 TEXTBOX 186 186 277 213 \"Original\" has marginal effect. 9 0.0 1 TEXTBOX 170 216 239 239 Only effective in HK! 9 0.0 1 TEXTBOX 424 311 591 329 Parameters for beta-distribution 9 0.0 1 TEXTBOX 614 407 764 425 4. Introduce extremists 12 0.0 1 TEXTBOX 610 459 760 483 Every agent in extremism zone never changes opinion. 9 0.0 1 TEXTBOX 610 485 875 509 Extremism zones: [0,extremism_range] \nand [1-extremism_range,1] under \"two sided\" 9 0.0 1 TEXTBOX 751 389 780 407 violet 9 115.0 1 TEXTBOX 753 379 778 397 blue 9 105.0 1 TEXTBOX 751 368 780 386 cyan 9 85.0 1 TEXTBOX 749 357 778 375 green 9 55.0 1 TEXTBOX 748 348 781 366 yellow 9 45.0 1 TEXTBOX 746 338 782 356 orange 9 25.0 1 TEXTBOX 752 328 769 346 red 9 15.0 1 TEXTBOX 654 316 749 334 colored histogram 9 0.0 1 TEXTBOX 616 329 739 347 >20% number_of_agents 9 0.0 1 TEXTBOX 696 389 743 407 0 agents 9 0.0 1 TEXTBOX 662 352 742 376 Fraction of agents in patch 9 0.0 1 TEXTBOX 791 328 848 347 eps >= 0.5 9 0.0 1 TEXTBOX 791 389 829 408 eps=0 9 0.0 1 TEXTBOX 771 315 864 333 eps in trajectories 9 0.0 1 TEXTBOX 712 302 831 332 Info: Color axis 12 0.0 1 TEXTBOX 791 358 839 376 eps=0.25 9 0.0 1 SLIDER 15 263 187 296 entry_exit_rate entry_exit_rate 0 1 0.02 0.01 1 NIL HORIZONTAL TEXTBOX 15 245 260 263 2. Probability of random opinion reset 12 0.0 1 @#$#@#$#@ # Continuous Opinion Dynamics under Bounded Confidence ## WHAT IS IT? A model of **continuous opinion dynamics under bounded confidence**, which includes * its two main variants of communication regimes (as in [Deffuant et al 2000](http://dx.doi.org/10.1142/S0219525900000078) with μ=0.5 and as in [Hegselmann and Krause 2002](http://jasss.soc.surrey.ac.uk/5/3/2.html)) * alternative aggregation of opinions by the median instead of the mean * heterogeneous bounds of confidence coming from a four parameter [beta distribution](http://en.wikipedia.org/wiki/Beta_distribution) * noise via random reset of opinions (as in [Pineda et al 2009](http://dx.doi.org/10.1088/1742-5468/2009/08/P08001)) * one-sided and two-sided extremism (similar to [Deffuant et al 2002](http://jasss.soc.surrey.ac.uk/5/4/1.html)) Visualizations: * a rolling colored histogram of opinion density over time * rolling trajectories of opinions over time colored by the bound of confidence * a bar plot histogram of current opinions * trajectories of the mean and the median opinion over time. ## HOW IT WORKS ### In a nutshell Agents adjust their opinion gradually towards the opinions of others when the distance in opinion is within their bound of confidence. Sometimes agents change their opinion to a new one at random. When agents hold extremal opinions they might get extremists which never adjust opinions. ### Variables Each of N agent has its **opinion** between 0.0 and 1.0 as a dynamic variable and its **bound of confidence** (eps) as a static variable. Other static variables are global and regard the type of communication and aggregation, the probability of random reset of opinion and the type and size of extremism zones. ### Setup Each agent is assigned its initial opinion as a random number between 0.0 and 1.0 from the uniform distribution. Each agent is assigned its bound of confidence as a random number form a [beta-distribution](http://en.wikipedia.org/wiki/Beta_distribution) between min_eps and max_eps. ### Dynamics Each tick agents are asked to adjust their opinions with respect to the opinions of others and their bound of confidence (eps). **1. Communication and aggregation:** * "DW (select one)": Each tick each agent is asked to select a random partner and changes its opinion to the average of the two opinions but only when the opinion of the partner is closer than eps to the own opinion. When original=On, N randomly selected agents are chosen each tick (so some agents possibly more than once) and both agents (the selected one and the randomly selected partner) both adjust opinions. (This is the version of Deffuant et al 2000, differences to original=Off are mostly minor.) * "HK (select all)": Each tick each agent is asked to change its opinion to the aggregated opinion of all opinions which are closer than eps to its opinion. The aggregate opinion can be the mean or the median. When original=On is checked all agents do the change simultaneously. (This is the version of Hegselmann and Krause 2002, differences are minor.) With original=Off an agent's change of opinion can immediately takes place and the new opinion might already effect the adjustments of the next agent even in the same tick. **2. Probability of random opinion reset** After the update of opinion each agent is asked to select a new opinion (in the same way as its initialization) but only with probability given by the variable entry_exit_rate. **3. Heterogeneous bounds of confidence** Notice that under heterogeneous bounds of confidence it might be that one agent has confidence in another but not vice versa! **4. Extremists** Extremism zones are intervals in the opinion space from 0.0 to 1.0 which are determined by the parameter extremism_range. The extremism intervals are from 0.0 to extremism_range for the selection "one side" and additionally from 1.0-extremism_range to 1.0 for the selection "two side". Agents which are located in the extermism zone never change their opinion. But still other non-extremist agents might adjust their opinion with respect to the opinions of extremists. Extremists still undergo change of opinion due to random reset of opinions. ## HOW TO USE IT Click "setup" to inititialize agents with opinions random and uniformly distributed between 0.0 and 1.0. Agents are located at the left border of the world with their opinions spreading over the vertical axis. Further, on confidence bounds are initialized for each agent as random draws from a beta distribution under the current choice of the four parameters. Click "go" to start the simulation. Agents move with ticks from left to right, displaying their opinion with the position on the vertical axis. This goes over into a "rolling" graphic in the world, where the last 120 ticks are shown (respectively the last max-pxcor ticks). Visualization can be chosen as trajectories of agents or as color-coded histograms. In colored histograms each patch's color is associated to the number of agents at this patch. A change of the variables communication regime, original, aggregation_in_HK, entry_exit_rate, extremism_range, and extremism_type is immediately effective in a running simulation. A change of the variable number_of_agents immediately changes the color axis in the "Colored histogram over time"-visualization and the vertical axis in the histogram of current opinions. A change of the four parameters of the distribution of eps (min_eps, max_esp, alpha, beta) immediately effect only the plot of the pdf of the distribution, but not the eps in the running simulation (click "new_confidence_bounds" for this). Click "new_confidence_bounds" to make new random draws for eps for each agent. Draws come from the beta distribution with the current parameters. (The same procedure is called when clicking "setup".) ## THINGS TO NOTICE Agents move towards the right-hand side of the world with one step each tick. This goes over into a "rolling" plot. Notice how agents form **clusters** in the opinion space. See how these clusters **evolve**, **drift** and **unite** in the "**Colored histograms over time**"-visualization. Look at the role of agents with different bounds of confidence in the "**Agents' trajectories**"-visualization. Look at the current distribution of opinions in the **bar plot histogram** on the right hand side and compare it to the colored histogram (the most recent colored vertical line in the world at the right hand side). Look how the **mean and the median opinion** evolve over time. The mean represents the center of mass of the distribution (cf. the current histogram). The median represents an unbeatable opinion under pairwise majority decisions. (This holds when agents have single-peaked preferences with peaks at their opinion, cf. [median voter theorem](http://en.wikipedia.org/wiki/Median_voter_theorem)). Look how the histogram of bounds of confidence matches the probability density function of the beta distribution when you click "new_confidence_bounds". ## THINGS TO TRY Try the **original models of homogeneous DW and HK communication without noise**: Set entry_exit_rate to zero, min_eps and max_eps to the same value, and extremism_range to zero. See how a stable configuration of clusters evolves. Check how the **number**, **sizes** and **locations** of clusters depend on the bound of confidence. Try to determine critical bounds of confidence where the evolving cluster pattern changes significantly. Use the setup button to rerun with a new initial configuration. Try to understand the impact of **noise** (entry_exit_rate) under homogeneous bounds of confidence: Set up a homogeneous eps (min_eps=max_eps), run the simulation and play with the entry_exit_rate slider (reasonable values are low, e.g. between 0.01 and 0.1). There are bounds of confidence, where the cluster pattern oscillates between two numbers of clusters. Under different entry_exit_rates the two patterns become differently likely. Play with **hetergeneous bounds of confidence** (min_eps