## Thursday, October 25, 2012

### Using Simulations to Maximize Stochastic Models

# Today, I was running late for class and decided to drive and park in two hour parking even though I had a three hour class.  I hoped the chance of being ticketted was low.  Whether it was or not, I ended up being ticketted for a wopping 35 dollars.  This got me thinking about the decisions that cities use to decide on how many officers should patrol their streets.  In some cities, I believe, parking laws and corresponding fees are structured in such a way as to cause minimal congestion while still providing parking for those who are looking for a short term soluation.  East Lansing, however I believe has a different incentive.  I believe they are generally uninterested in the welfare of the mostly students who come from other cities to access the University in East Lansing.  East Lansing, using parking as a revenue generating system that extracts rents from mostly non-East Lansing residents.

# If we are the city of East Lansing and are considering how to maximize rents in the form of traffic tickets we may use a simulation to identify the most likely strategy for maximizing over stochastic outcomes.

# The cost of employing one parking officer checking all of the vehicles takes 1 hour at a cost of \$15.

cost_officer = 15

# Each minute there is a 20% chance of someone arriving.  There are 10 hours in which parking is enforced.  Thus there could be at most 60*10=600 people per day.

# Each entry is the arrival time a potential person
arrive = 1:600

# Person
showed_up = rbinom(600,1,.2)

# The duration each person stays is drawn from a poisson distribution.

duration = rpois(600, 30)*4*showed_up

depart = (arrive+duration)*showed_up

# What we want to know is, what is the optimal number of times to send out parking officers.

# In order to answer that we have to think about what the officers are doing.  First, they need to mark vehicles that are currently parked.  If there are vehicles that were marked more than 2 hours previously, then they write a ticket.  It is possible that vehicles will not pay even though they viloate the 2 hour limit because they either were not marked early enough or left before a follow up could be arranged.

# The choice variable is number of patrols which each cost 15 dollars.

numb_patrols = 50

# The patrols are spaced evenly over the 10 hours.

patrol_time = seq(0,600,length.out=numb_patrols+1)[-1]
# I add 1 to the numb_patrols because the first patrol is sent out not at the beginning of the parking day since by construction there is nobody parking before the parking hours.

# Now let's figure out who gets chalked and who gets ticketted.
chalked = 0*showed_up

# We want vehicles to be chalked by the first officer that they encounter but not to be chalked more than once.
for (v in patrol_time) {
chalked[(arrive < v)&(chalked==0)] = v
}

ticketed = 0*showed_up
# Now let's see who gets ticketted
for (v in patrol_time) {
print(((v-chalked)>120) & (chalked!=0) & (depart>v) )
ticketed[(depart>v) & ((v-chalked)>120) & (chalked!=0)] =1
}

cbind(duration, ticketed)

# In order to calculate net benefits.  Compare the cost of highering patrols to that of the ticket value.

sum(ticketed)*35 - numb_patrols*15
# My approximation of the revenue is 1385 for one day of ticketing.  Let's see if we can't get a profitability curve as a function of the number of patrols.  To do this I will copy and compress the code above into a function.

##########################################
park_fun <- function(num_officer) {
returnvalues=matrix(NA, nrow=length(num_officer), ncol=2)
for(i in 1:length(num_officer)) {
numb_patrols = num_officer[i]
cost_officer = 15
arrive = 1:600
showed_up = rbinom(600,1,.2)
duration = rpois(600, 30)*4*showed_up
depart = (arrive+duration)*showed_up
patrol_time = seq(0,600,length.out=numb_patrols+1)[-1]
chalked = 0*showed_up
for (v in patrol_time) {
chalked[(arrivev)&(chalked==0)] = v
}

ticketed = 0*showed_up
for (v in patrol_time) {
ticketed[(depart>v) & ((v-chalked)>120) & (chalked!=0)] =1
}
returnvalues[i,]<- cbind(sum(ticketed)*35, numb_patrols*15)
}
returnvalues
}
park_fun(1:10)

# Now let's map a profit function

park_results <- park_fun(1:100)

plot(1:100, park_results[,1], type="l", ylab="\$", xlab="Number of Patrols", main="Jagged line is ticket revenue, Straight line is patrol cost", , lwd=2)
lines(1:100, park_results[,2],  col="purple", lwd=2)

# The city would like to run this simulation 100 or 1000 times to get expected distributions of retuns for each choice of number of patrols.  Then, you simply take the expected revenue and subtract it from expected cost (assuming the city is risk nuetral in parking violations).  Eyeballing the graph we can get an idea.  A number of patrols around 50 would be ideal to maximize profits.  Another key feature of the graph is the dimishing returns to more patrols after the first hump.  This is because it does little good to send out more cars to ticket if there are already a lot of cars ticketing other cars.