File accompanying lecture 5-2.

Christopher Hanusa

The basic simulation

Table[If[RandomReal[]≤0.075,1,0],{i,180}]
Total[%]

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];]
nwait

Collecting statistics

Number of patients waiting

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
nwait

trials=Table[nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
nwait,{j,1000}];
N[Mean[trials]]
Histogram[trials]

How long after noon will the doctor stay?

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
Max[0,endTime+nwait*15-180]

trials=Table[nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
Max[0,endTime+nwait*15-180],{j,1000}];
N[Mean[trials]]
Histogram[trials]

Time-dependent data

When is the doctor busy?

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];
isBusy[i]=busy;];
busyList=Table[isBusy[i],{i,0,179}];
Total[busyList]
ListLinePlot[busyList,PlotStyle->Thick]

numWait[i]=nwait;

How full is the waiting room?

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];
numWait[i]=nwait;];
waitList=Table[numWait[i],{i,0,179}];
m=N[Mean[waitList]]
p1=ListLinePlot[waitList,PlotStyle->{Thick}];
p2=Plot[m,{x,0,179},PlotStyle->{Thick,Red}];
Show[p1,p2]

All together

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];newPatient=If[RandomReal[]≤0.075,1,0];If[newPatient==1,nwait++];If[busy==0&&nwait>0,nwait=nwait-1;busy=1;endTime=i+15];
patient[i]=newPatient;
isBusy[i]=busy;
numWait[i]=nwait;]

Table[{i,patient[i],isBusy[i],numWait[i]},{i,0,59}] // TableForm

d1=Table[{i,patient[i]},{i,0,179}];
d2=Table[{i,isBusy[i]},{i,0,179}];
d3=Table[{i,numWait[i]},{i,0,179}];
ListLinePlot[{d3,d2,d1},PlotStyle->{{Yellow,Thick},{Blue,Thick},{Red,Thick}}]

Notes graphics

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];newPatient=If[RandomReal[]≤0.075,1,0];If[newPatient==1,nwait++];If[busy==0&&nwait>0,nwait=nwait-1;busy=1;endTime=i+15];
patient[i]=newPatient;
isbusy[i]=busy;
waiting[i]=nwait;]

Table[{i,patient[i],isbusy[i],waiting[i]},{i,0,59}] // TableForm

BusyPlot=ListLinePlot[Table[isbusy[i],{i,0,179}]]

BusyPlot=ListLinePlot[Table[isbusy[i],{i,0,179}],PlotRange->{0,1.3},PlotStyle->Thick]
Export["Dropbox/245/figures/doctorbusyplot.eps",%]

waitList=Table[waiting[i],{i,0,179}];
m=Mean[waitList]
p2=Plot[m,{x,0,179},PlotStyle->{Red,Thick}]
Waiting=ListLinePlot[waitList,PlotStyle->Thick]
Export["Dropbox/245/figures/doctorwaitplot.eps",Show[Waiting,p2]]

Waiting=ListLinePlot[Table[waiting[i],{i,0,179}],PlotStyle->Thick]

ListPlot[Table[patient[i],{i,0,179}]]

Arrivals=Graphics[{Red,Thick,Table[If[patient[i]==1,Line[{{i,0},{i,5}}],{}],{i,0,179}]}]

nwait

Show[BusyPlot,Arrivals]
Export["Dropbox/245/figures/doctorbusy.eps",%]

Show[Waiting,Arrivals]
Export["Dropbox/245/figures/doctorwaiting.eps",%]

trials=Table[
nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];newPatient=If[RandomReal[]≤0.075,1,0];If[newPatient==1,nwait++];If[busy==0&&nwait>0,nwait=nwait-1;busy=1;endTime=i+15];];
nwait,{j,1000}];

N[Mean[trials]]

Histogram[trials]
Export["Dropbox/245/figures/doctorhistogram.eps",%]

newtrials=Table[
nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];newPatient=If[RandomReal[]≤0.075,1,0];If[newPatient==1,nwait++];If[busy==0&&nwait>0,nwait=nwait-1;busy=1;endTime=i+15];];
Max[0,endTime+nwait*15-180],{j,1000}];

N[Mean[newtrials]]

Histogram[newtrials]
Export["Dropbox/245/figures/doctortimehistogram.eps",%]