492 Study Report: Engin Deniz Alpman June 2, 2017
492 Study Report: Engin Deniz Alpman June 2, 2017
June 2, 2017
1
1 Elements of programming
In this part of the book, I learn and do practice about loop and arrays in java.Also I improve my
knowledge about conditionals. At the end of the chapter there was a case-study called Random Web
Surfer. This model is used for investigating the users movement across the internet. To investigate
the model, I learned about Markov chains and matrices. Markov chain can be used in lots of field.
For example it helps us to compute the probability of being in specific state after certain steps
when initial state is known, or probability of being specific state as an array when initial state is
not known.Elements in the output array will give us the different probability depending on the
starting state. Also Markov chain can be used to make simulations and that is exactly what I
did. I have made simulation about a board game called Chutes and ladders.I create simplified
version of the game not because implementation is hard,infact implementation is the same, but it
is repetitive. So I create 3x3 version of the game which is originally 10x10.The simulation that I
created computes the relative probability of winning when a player starts as a first player and as a
second player. The code I write for it in java language can be seen at below.
import java.lang.*;
import java.util.*;
int N = 8;
for(int k=0;k<50;k++){
int state1 = 0;
int state2=0;
int steps = 0;
int counter=0;
1
while (state1!=7 && state2!=7) {
// System.out.println(state);
steps++;
counter+=1;
double r1 = Math.random();
double r2 = Math.random();
double sum1 = 0.0;
double sum2=0.0;
if(counter==1){
for (int j = 0; j < N; j++) {
sum1 += transition[state1][j];
break;
}
}
}
else{
break;
}
}
break;
}
}
}
2
if(state1==7){
else{
So we can say that first player has an advantage over the second player.
3
2 Functions
In this part of the book, I learned about static methods and recursion. Static methods and variables
can be used for creating more stable model and self-containing models like singletons. Also recursion
is useful for approaching problem with divide and conquer method. In recursion, problem is divided
into similar subproblems and then combination of those problem gives us the desired result. At the
end of the chapter, as was in the first chapter, there was a case study called Percolation. There
is an union find algorithm in the book for implementation of percolation algorithm. But rather
than copying the code, I decided to learn union find algorithm. After that I implement percolation
using my union find algorithm. Percolation is a mathematically unsolved problem but we can easily
compute it with computers. It can be used in numerous field like the damage evaluation of forest
fire, disease spread,electrical resistance in a mixture of two media,oil spread etc. I put the union
find algorithms and percolations code below.
#union find
import java.util.*;
public class Uf {
int p = _parent[i];
if (i == p) {
return i;
}
return _parent[i]=find(p);
4
public Uf(int max) {
5
#Percolation
import java.lang.*;
if(Math.random()<=p){
a[i][j] = true;
}
else{
a[i][j] = false;
}
return a;
}
}
6
3 Object-Oriented-Programming
In this part of the book, I learned how to create and use classes. This is the core part of the object
oriented programming. By using classes we can divide problem to simpler subproblems that are
different from one another and solve them using different classes.This gives us very powerful tool
for solving problems. Because instead of solving one huge problem, now we can solve it by step by
step. In the case study of chapter 3, I studied object under gravitational force. To do that I used
different classes. For example body and a ball have different classes. Body gives us the object that
is affected by gravitational field, and the ball gives us the visual geometric image of the object we
are investigating. Using classes, we can assign different attributes to different objects while there
share similar attributes.
#Ball
public Ball() {
rx = 0.0;
ry = 0.0;
vx = StdRandom.uniform(-0.015, 0.015);
vy = StdRandom.uniform(-0.015, 0.015);
radius = StdRandom.uniform(0.025, 0.075);
}
7
public static void main(String[] args) {
StdDraw.setXscale(-1.0, +1.0);
StdDraw.setYscale(-1.0, +1.0);
StdDraw.enableDoubleBuffering();
while (true) {
StdDraw.clear(StdDraw.GRAY);
StdDraw.setPenColor(StdDraw.BLACK);
b1.move();
b2.move();
b1.draw();
b2.draw();
StdDraw.show();
StdDraw.pause(20);
}
}
}
8
#Body
private Vector r;
private Vector v;
private final double mass;
9
4 Data Structures
In the fourth part of the book, I learned about data types like Stack and Queues ; Symbol tables.
Learning data types is an importing step in learning programming because each problem has its
own properties and using correct data structure to approach problem is crucial in both efficiency
and implementation. In the case study of this chapter, I used what I learned about data structure
from this chapter to implement small world algorithm. I used those knowledge to make connections
between data points. That was not wanted but I want to implement doubly linked list,stack and
binary tree algorithms because I think it is crucial to know those algorithms and how they work.
#Node
public class Node{
String data;
int count=1;
Node left;
Node right;
10
if(current==null){
parent.right = newNode;
return;
}
}
else{
current.count++;
return;
}
}
}
public void display(Node root){
if(root!=null){
display(root.left);
System.out.println(" " + root.data+":"+ root.count);
display(root.right);
}}}
11
}
12
public void deleteFromEnd(){
if(size==0){
System.out.println("\nList is Empty");
}else if(size==1){
deleteFromStart();
}else{
//store the 2nd last node
int x = tail.data;
Node prevTail = tail.previous;
p.next=n;
n.previous=p;
size-- ;
return;
}
del_node = del_node.next;
13
}
}
//get Size
public int getSize(){
return size;
}
14
test.insertAtPos(2,2);
test.print();
test.deleteFromStart();
test.print();
System.out.println("Element at index 3: "+test.elementAt(3));
test.addAtStart(5);
test.print();
test.deleteFromEnd();
test.print();
System.out.println("Size of the Linked List: " + test.getSize());
test.print_reverse();
}
}
#Stack
public class Stack {
public Stack(int s) {
maxSize = s;
char_array = new char[maxSize];
top = -1;
}
public void push(char j) {
char_array[++top] = j;
}
public char pop() {
return char_array[top--];
}
public char peek() {
return char_array[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
Stack theStack = new Stack(10);
theStack.push(n);
theStack.push(a);
theStack.push(b);
theStack.push(e);
theStack.push(r);
15
while (!theStack.isEmpty()) {
char value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
#Graph
public class Graph {
private int E;
public Graph() {
st = new ST<String, SET<String>>();
}
16
public void addEdge(String v, String w) {
if (!hasVertex(v)) addVertex(v);
if (!hasVertex(w)) addVertex(w);
if (!hasEdge(v, w)) E++;
st.get(v).add(w);
st.get(w).add(v);
}
17
}
StdOut.println(graph);
dI
dt = SI I
dR
dt = I
18
So we can model this by implementing this equations on python like this:
import scipy.integrate as spi
import numpy as np
import pylab as pl
beta=1.4247
gamma=0.14286
TS=1.0
ND=70.0
S0=1-1e-6
I0=1e-6
INPUT = (S0, I0, 0.0)
def eq(INP,t):
Y=np.zeros((3))
V = INP
Y[0] = - beta * V[0] * V[1]
Y[1] = beta * V[0] * V[1] - gamma * V[1]
Y[2] = gamma * V[1]
return Y
19
Figure 1: SIR model without births and deaths
dI
dt = SI I I
dR
dt = I R
20
So we can model this by implementing this equations on python like this:
import scipy.integrate as spi
import numpy as np
import pylab as pl
mu=1/(70*365.0)
beta=520/365.0
gamma=1/7.0
TS=1.0
ND=60*365
S0=0.1
I0=1e-4
R0=1-S0-I0
INPUT = (S0, I0, R0)
def eq(INP,t):
Y=np.zeros((3))
V = INP
Y[0] = mu - beta * V[0] * V[1] - mu * V[0]
Y[1] = beta * V[0] * V[1] - gamma * V[1] - mu * V[1]
Y[2] = gamma * V[1] - mu * V[2]
return Y
pl.subplot(311)
pl.plot(RES[:,0], -g, label=Susceptibles)
pl.title(SIR)
pl.xlabel(Time)
pl.ylabel(Susceptibles)
pl.subplot(312)
pl.plot(RES[:,1], -r, label=Infectious)
pl.xlabel(Time)
pl.ylabel(Infectious)
pl.subplot(313)
pl.plot(RES[:,2], -k, label=Recovereds)
pl.xlabel(Time)
pl.ylabel(Recovereds)
pl.show()
21
Figure 2: General SIR model
dI
dt = SI I
22
So we can model this by implementing this equations on python like this:
import scipy.integrate as spi
import numpy as np
import pylab as pl
beta=1.4247
gamma=0.14286
I0=1e-6
ND=70
TS=1.0
INPUT = (1.0-I0, I0)
def eq(INP,t):
Y=np.zeros((2))
V = INP
Y[0] = - beta * V[0] * V[1] + gamma * V[1]
Y[1] = beta * V[0] * V[1] - gamma * V[1]
return Y
pl.subplot(211)
pl.plot(RES[:,0], -g, label=Susceptibles)
pl.title(SIS model)
pl.xlabel(Time)
pl.ylabel(Susceptibles)
pl.subplot(212)
pl.plot(RES[:,1], -r, label=Infectious)
pl.xlabel(Time)
pl.ylabel(Infectious)
pl.show()
23
Figure 3: SIS model
dE
dt = SI ( + )E
dI
dt = E ( + )I
dR
dt = I E
24
So we can model this by implementing this equations on python like this:
import scipy.integrate as spi
import numpy as np
import pylab as pl
mu=1/(70*365.0)
beta=520/365.0
sigma=1/14.0
gamma=1/7.0
ND=60*365.0
TS=1.0
S0=0.1
E0=1e-4
I0=1e-4
INPUT = (S0, E0, I0)
def eq(INP,t):
Y=np.zeros((3))
V = INP
Y[0] = mu - beta * V[0] * V[2] - mu * V[0]
Y[1] = beta * V[0] * V[2] - sigma * V[1] - mu * V[1]
Y[2] = sigma * V[1] - gamma * V[2] - mu * V[2]
return Y
Rec=1. - (RES[:,0]+RES[:,1]+RES[:,2])
pl.subplot(311)
pl.plot(RES[:,0], -g, label=Susceptibles)
pl.title(SEIR Model)
pl.xlabel(Time)
pl.ylabel(Susceptibles)
pl.subplot(312)
pl.plot(RES[:,1], -m, label=Exposed)
pl.plot(RES[:,2], -r, label=Infectious)
pl.legend(loc=0)
pl.xlabel(Time)
pl.ylabel(Infected)
pl.subplot(313)
pl.plot(Rec, -k, label=Recovereds)
pl.xlabel(Time)
pl.ylabel(Recovereds)
pl.show()
25
Figure 4: SEIR model
dI
dt = (I + BC)S I I
dC
dt = q C C
dR
dt = (1 q)I + C R
26
So we can model this by implementing this equations on python like this:
import scipy.integrate as spi
import numpy as np
import pylab as pl
beta=0.2
epsilon=0.1
gamma=0.01
Gamma=0.001
mu=1/(50*365.0)
q=0.4
S0=0.1
I0=1e-4
C0=1e-3
ND=60*365
TS=1.0
INPUT = (S0, I0, C0)
def eq(INP,t):
Y=np.zeros((3))
V = INP
Y[0] = mu - beta * V[0] * (V[1] + epsilon * V[2]) - mu * V[0]
Y[1] = beta * V[0] * (V[1] + epsilon * V[2]) - gamma * V[1] - mu * V[1]
Y[2] = q * gamma * V[1] - Gamma * V[2] - mu * V[2]
return Y
Rec=1. - (RES[:,0]+RES[:,1]+RES[:,2])
pl.subplot(311)
pl.plot(RES[:,0], -g, label=Susceptibles)
pl.title(SIR model with carrier)
pl.xlabel(Time)
pl.ylabel(Susceptibles)
pl.subplot(312)
pl.plot(RES[:,1], -r, label=Infectious)
pl.xlabel(Time)
pl.ylabel(Infected)
pl.subplot(313)
pl.plot(RES[:,1], -m, label=Carriers)
pl.xlabel(Time)
pl.ylabel(Carriers)
pl.show()
27
Figure 5: SIR model with Carrier
References
28