# AnalyticBridge

A Data Science Central Community

The 90-seconds video showing a 'random walk' (a particular case of a Markov process) evolving over 400,000 steps, was produced using two pieces of code. Last frame of the video:  darker areas are those visited first

1. Production of the data set

The data set consists of 2,000 video frames. Each frame is a set of 200 new (x, y) locations (2,000 times 200 = 400,000). The output data set rw.txt has 3 fields: frame number (referred to as iter in the second program), x, and y coordinates.

Here's the code (Perl version, easy to translate to Python, PHP, Java or C++):

\$x=0;
\$y=0;
\$max_x=-1;
\$max_y=-1;
\$min_x=999999999;
\$min_y=999999999;

\$m=200;
\$n=200;
\$niter=2000;
\$end=400000;

# \$end must be > \$niter * \$m

open(OUT,"> extremes.txt");
for (\$k=0; \$k<\$end; \$k++) {
\$ax[\$k]=\$x;
\$ay[\$k]=\$y;
if (\$x> \$max_x) { \$max_x=\$x; }
if (\$y> \$max_y) { \$max_y=\$y; }
if (\$x< \$min_x) { \$min_x=\$x; }
if (\$y< \$min_y) { \$min_y=\$y; }
print OUT "\$max_x\t\$max_y\t\$min_x\t\$min_y\n";
\$rnd1=-0.5+rand();
\$rnd2=-0.5+rand();
\$x+=\$rnd1;
\$y+=\$rnd2;
}
close(OUT);

open(OUT,">rw.txt");
print OUT "iter\tx\ty\n";
for (\$iter=0; \$iter<\$niter; \$iter++) { # generate niter frames for video
for (\$k=0; \$k<\$n; \$k++) { # display n points on each frame
\$idx=\$m*\$iter+\$k; # add m new points in each subsequent frame, delete m old points
print OUT "\$iter\t\$ax[\$idx]\t\$ay[\$idx]\n";
}
}
close(OUT);

print "\$min_x | \$max_x | \$min_y | \$max_y\n";

Note that in addition to producing the data set rw.txt for step #2, it also produces an auxiliary file extremes.txt, to perform some ad-hoc analyses to answer some of the questions in the associated challenge of the week.

2. Production of the video, using R

The R code below reads the data set rw.txt produced in step #1, and displays the 2,000 frames on your screen.

iter<-vv\$iter;

minx=min(vv\$x);
maxx=max(vv\$x);
miny=min(vv\$y);
maxy=max(vv\$y);

for (n in 1:2000) {

x<-vv\$x[iter == n];
y<-vv\$y[iter == n];
p<-vv\$x[iter == n-1];
q<-vv\$y[iter == n-1];

if (n==1) {
plot(x,y,xlim=c(minx,maxx),ylim=c(miny,maxy),pch=40, cex=0,col=rgb(1,1,0),xlab="",ylab="",axes=TRUE);

}

s=1/sqrt(1+n);
points(p,q,col=rgb(1-s,1-s,1-s),pch=20,cex=0.01);
points(x,y,col=rgb(1,0,0),pch=20,cex=0.01);

# Sys.sleep(0.2);
}

For details about how the video was captured from the R GUI using an open-source screencast tool, click here.  At each iteration, the 200 points in the current frame are displayed in red (using col=rgb(1,0,0) in the R code), to help you visualize the progress on the screen.