This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the point of this script is to demonstrate the SchellingSegregation() function, | |
# which takes the output from SchellingProcess() and calculates the segregation index given a certain neighborhood size, by default 4x4 cells blocks in the larger matrix. | |
# we first source two git gists containing the functions we'll use: | |
# source Schelling Process functions: | |
# get Schelling Process functions: | |
source("http://raw.github.com/gist/1910183/785efd5809a05df73de23c4fa9d897e3789444ba/SchellingProcessFunctions.r") | |
# get Segregation Index functions for Schelling Process output: | |
source("http://raw.github.com/gist/1910173/248483abb658f73acb03e897d3374b5b709c3c21/SegregationIndex.r") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this code does a little experiment to see how the proportion living in a 500x500 game of life board changes | |
# over time by starting population density. | |
# we first define 2 functions (from Vadim Vinichenko) for the game of life. | |
# His functions are convenient because they scale well. | |
shiftMatrix <- function(mx, dr, dc) { | |
#Shift the matrix by dr (delta r) rows and dc columns | |
#by adding e.g. dr rows of zeros and removing dr rows from the other side | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MontyHall <- function(verbose=TRUE){ | |
choices <- as.character(1:3) | |
rightchoice <- sample(choices,size=1) | |
yourfirstchoice <- select.list(choices=choices,multiple=FALSE,title="First Guess") | |
remaining <- choices[!choices %in% yourfirstchoice] | |
eliminate <- sample(choices[!choices %in% c(rightchoice,yourfirstchoice)],size=1) | |
otherdoor <- choices[!choices %in% c(eliminate,yourfirstchoice)] | |
if (verbose){ | |
cat("You chose door",yourfirstchoice,"\n") | |
cat("Of the other two doors, I can tell you that it's definitely NOT door",eliminate,"\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Tim Riffe tim.riffe@gmail.com | |
############################################################################### | |
# ---------------------------------- | |
# a big fat game of life simulation | |
# ---------------------------------- | |
# -------------------------------------------------------------------------- | |
# Explanation of what this is all about: | |
# -------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### FIXED!! | |
# It turns out that the assumptions you make for border have a huge impact on the evolution of a | |
# Game of Life board. Our first implementations (Vadim and myself both) were to assume dead cells on the | |
# borders. Here I've included a new function where edge cells have neigbors on the opposite side of the board | |
# i.e. the board wraps around both top to bottom and right to left, which is a shape called a taurus. | |
# this function runs about twice as fast as the previosu best-performer from Vadim and gives VERY different | |
# results (just looking at one output, measurement, the proportion of cells alive at any given iteration). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FIXED!! | |
# 2 simulations run, both take a long time. This code will dish it out to 8 cores. | |
# I will post the 2 data objects produced as .Rdata files somewhere so you can just load them | |
# and follow the code to reproduce the figures, etc. | |
# rnd_threshold are starting densities: | |
rnd_threshold <- seq(.01,.99,by=.01) | |
ParThreshold <- function(thr,n_rows=500,n_cols=500,n_cycles=5){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this syntax includes 3 functions: | |
# GOLborderwrap(), which is a repeat from earlier GOL posts. Just give it a matrix and it'll iterate once | |
# for you | |
# Illustrate() uses GOLborderwrap() inside. Just give Illustrate() a matrix and it'll do 20 iterations | |
# by default. You can change that number using the 'iter' argument. Also you can change the speed using | |
# the 'frame.pause' argument. | |
# finally theres the GOLdraw() function, which is interactive. If you see a GOL pattern online or in a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Mazdaika | |
############################################################################### | |
## should be col*row, not col+row | |
# create matrix - initial life situation | |
crmtx <- function(col=5, row=5, st=0.4){ | |
x <- matrix(rbinom(col+row,1, st), ncol=col, nrow=row) | |
return(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: triffe | |
############################################################################### | |
# this code will try to implement an example percolation model, where density and matrix size are parameters | |
# the main function also produces neat statistics along the way for post-analysis | |
# dead borders. either rook or queen neighborhoods work. | |
# um, name means percolation, with dead borders | |
PercIteration <- function(M, neighborhood.type = "rook"){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Tim Riffe | |
# this code (almost entirely commented out because it would take so long to re-run [however, you have the | |
# option!]) shows how the below results were derived. This percolation code uses a stripped-down version of | |
# the previous percolation code here: https://gist.github.com/2044583 - it only produces 1 result: does the | |
# infiltration reach the other side of the board or not, using the rook definition. The code was reduced to | |
# be as concise as possible and then compiled, to run as fast as possible (though I'm sure there's an even | |
# faster way) | |
# the flow: we want to try out the same percolation model for each starting density from 0 to 1 in increments | |
# of .001 on a 100x100 board. Each density we repeat 300 times. Since the output is TRUE/FALSE (traversed or |