Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / SchellingNeighborhoods.R
Created February 25, 2012 19:32
Model Thinking- Schelling Process- Segregation Indices and Neighborhood size
# 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")
@timriffe
timriffe / GameOfLifeStartingDensities.r
Created February 26, 2012 13:06
Model Thinking- Game of Life- varying starting densities
# 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
@timriffe
timriffe / MontyHall.R
Created February 27, 2012 17:24
ModelThinking- Monty Hall
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")
@timriffe
timriffe / GOLdensity5000bigsim.r
Created February 28, 2012 10:39
Model Thinking- Aggregation- Game of Life- Massive simulation over density, 500 iterations, parellel code
# Author: Tim Riffe tim.riffe@gmail.com
###############################################################################
# ----------------------------------
# a big fat game of life simulation
# ----------------------------------
# --------------------------------------------------------------------------
# Explanation of what this is all about:
# --------------------------------------
@timriffe
timriffe / GOLwrapping.r
Created February 28, 2012 17:16
Model Thinking- Aggregation- Game of Life- new cycle function with wrapping instead of dead borders
### 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).
@timriffe
timriffe / GOL_startingdensbyborderassumption500x500_100iterations.r
Created March 1, 2012 14:31
Model Thinking- Aggregation- Game of Life- Comparing dead and wrapped borders by starting density
# 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){
@timriffe
timriffe / GOLextras.r
Created March 1, 2012 21:28
Model Thinking- Aggregation- Game of Life- Complementary Functions and Formations.
# 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
@timriffe
timriffe / GOL_MazdaikaComments.R
Created March 13, 2012 15:56
Game of Life comments for Mazdaika
# 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)
@timriffe
timriffe / Percolation.R
Created March 15, 2012 14:49
Model Thinking- Tipping Points- Percolation
# 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"){
@timriffe
timriffe / PercolationSim.R
Created March 21, 2012 10:26
Model Thinking- Tipping Points- Percolation- Simulation to find tipping point
# 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