In: Statistics and Probability
In this project we will consider a particle sitting at the origin inside of a square whose sides intersect at the points (-10, -10), (-10, 10), (10, -10), and (10, 10). Each second, the particle moves in a random direction a random distance. That distance has an X as well as a Y component, each of which are standard normally distributed.
1.1 [8 points] Use R-Studio to model the following scenario: the particle moves every second as described above, use your code to model that motion until the particle steps out of bound “FOR THE FIRST TIME” then stop counting the steps.
1.2 [5 points] Repeat this simulation 1,000 times and store the number of steps in a matrix, then calculate the mean number of steps needed for the particle to step out of bounds, the median and the standard deviation.
1.3 [4 points] Plot a box plot and a histogram of the number of steps of all replications.
1.4 [3 points] Using the last simulation run, trace the motion of that particle using a line plot. What where the coordinates of the particle when it stepped out of bound.
1.1]
#Initial co-ordinates
x=0
y=0
z<-c(0,0) #Origin(al) co-ordinates of particle stored in
vector
#Simulate x and y co-orindates until the particle is out of
bounds
while(x<10 & x>-10 & y<10 & y>-10)
{
x<-x + rnorm(1)
y<-y + rnorm(1)
l<-c(x,y)
z<-c(z,c(l))
}
print(z) #This is the vector which has the path of co-ordinates the
particle takes
1.2]
#Simulating x and y co-ordinates 1000 times
a=0;
while(a<1001)
{
a<-a+1
x=0
y=0
z<-c(0,0) #Origin(al) co-ordinates of particle stored in
vector
while(x<10 & x>-10 & y<10 & y>-10)
{
x<-x + rnorm(1)
y<-y + rnorm(1)
l<-c(x,y)
z<-c(z,c(l))
}
q<-c(q,length(z))# storing no.of steps after which particle was
out of bounds
}
print(mean(q))# printing mean and standard deviation of vector q
which has the no.of steps the particles takes to go out of bounds
in each of the 1000 simulation
print(sd(q))
1.3] boxplot(q,
main = "Mean no.of steps for particle to go our of bounds",
xlab = "Steps",
ylab = "Particle",
col = "orange",
border = "brown",
horizontal = TRUE,
notch = TRUE
)
hist(q)
1.4] plot(X,Y)
lines(X,Y,type="l") #syntax for plotting path of
particle
Path of Particle