```python
import pandas as pd
import numpy as np
import random
from random import expovariate as nexttime
import matplotlib.pyplot as plt
import sys
import cv2
import glob
sys.path.append('../../Python')
from time import time as t
import seaborn as sns
import os
from Senior_Project_Function import *
import math 
import sympy
import datetime as dt
from sympy import *
```

# 1. Simple Queue Model Basics

Function: **Simplesimulate**  
Description: **Simplesimulate** simulates the bike share system from a model variated from Queueing Theory model. For this function, it simulates the bike share system with 1 station only.  
Input: (ArrivalRate, LeaveRate, BikesInUseInitial, BikesAtStationInitial, Capacaity, n)  
Output: Time, BikesInUseList, BikesAtStationList, WaitListPick, WaitListReturn  
See Senior_Project_Function.py for detailed information

1. Example Usage: Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = **Simplesimulate**(2,2,10,20,20,2000)


```python
path = 'Pictures\InfinitePatience'
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = Simplesimulate(2,2,10,20,20,2000)
```


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,BikesInUseList,label="# of bikes in use")
plt.plot(Time,BikesAtStationList,label="# of bikes at station")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of Bikes',fontsize=20)
plt.title(r'$\lambda = 2, \mu = 2, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\lambda2mu2Bikes.png')
# plt.savefig(p)
```


```python

fig=plt.figure(figsize=(15, 8))
plt.plot(Time,WaitListPick,label="# of People wait to pick")
plt.plot(Time,WaitListReturn,label="# of People wait to return")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of People',fontsize=20)
plt.title(r'$\lambda = 2, \mu = 2, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\lambda2mu2Peoplewaiting.png')
# plt.savefig(p)
```


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = Simplesimulate(2.5,2,10,20,20,2000)
```


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,BikesInUseList,label="# of bikes in use")
plt.plot(Time,BikesAtStationList,label="# of bikes at station")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of Bikes',fontsize=20)
plt.title(r'$\lambda = 2.5, \mu = 2, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\lambda2dot5mu2Bikes.png')
# plt.savefig(p)
```


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,WaitListPick,label="# of People wait to pick")
plt.plot(Time,WaitListReturn,label="# of People wait to return")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of People',fontsize=20)
plt.title(r'$\lambda = 2.5, \mu = 2, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\lambda2dot5mu2Peoplewaiting.png')
# plt.savefig(p)
```

# 2 Birth and Death Simulation Basics

Function: **BirthDeathsimulate**  
Description: **BirthDeathsimulate** simulates the bike share system from a model variated from birth and death model. For this function, it simulates the bike share system with 1 station only.  
Input: (ArrivalRate, LeaveRate, BikesInUseInitial, BikesAtStationInitial, Capacaity, n)  
Output: Time, BikesInUseList, BikesAtStationList, WaitListPick, WaitListReturn  
See Senior_Project_Function.py for detailed information

1. Example Usage: Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = **BirthDeathsimulate**(1,1/20,10,20,20,2000)  


```python
path = 'Pictures\Patience_with_Max_waiting_to_pickupsize_10'
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/20,10,20,20,2000)
```

2. Plot the list of number of bikes in use and list of number of bikes at station versus time steps in the same graph and save the picture if necessary


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,BikesInUseList,label="# of bikes in use")
plt.plot(Time,BikesAtStationList,label="# of bikes at station")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of Bikes',fontsize=20)
plt.title(r'$\lambda = 1, \mu = 0.05, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\lambda1mu0.05.png')
#plt.savefig(p)
```

3. Plot the list of number of people wait in line to pick up bikes and list of number of people wait in line to return bikes versus time steps in the same graph and save the picture if necessary


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,WaitListPick,label="# of People wait to pick")
plt.plot(Time,WaitListReturn,label="# of People wait to return")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of People',fontsize=20)
plt.title(r'$\lambda = 1, \mu = 0.05, T = 30, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\Peoplewaiting.png')
#plt.savefig(p)
```

# 3 Birth and Death Simulation Stationary Distribution

1. Use **BirthDeathsimulate** to simulate the system and compare it with theoretical results

Function: **calculatex_0**  
Description: Given lambda and mue values, use sympy to solve x_0.
Input: (lambdav,mue,b = 30,Cwait = 10)  
Output: x_0
See Senior_Project_Function.py for detailed information

Example Usage: z = calculatex_0(1,1/35,30,10)


```python
z = calculatex_0(1,1/35,30,10)
Dict = {}
sumV,Dict = calwaittimestationary(30,1/35,10,z,1)
Dict
```




    {0: 0.150356387055402,
     1: 0.0385680185223662,
     2: 0.0449960216094272,
     3: 0.0524953585443317,
     4: 0.0612445849683870,
     5: 0.0714520157964516,
     6: 0.0833606850958601,
     7: 0.0972541326118368,
     8: 0.113463154713810,
     9: 0.132373680499445,
     10: 0.154435960582685}



Function: **time_distribution_cal**  
Description: Given the state list and time list, **time_distribution_cal** function outputs the time distribution of each state
Input: (List,Time)  
Output: Output  
See Senior_Project_Function.py for detailed information

Example Usage: test = **time_distribution_cal**(WaitListPick,Time)  
test = **normalize**(test) 


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/35,10,20,20,500)
test = time_distribution_cal(WaitListPick,Time)
test = normalize(test)
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = Dict.values()
bars2 = test.values()
 
# Set position of bar on X axis
r1 = list(Dict.keys())
r2 = [x + barWidth for x in r1]
 
# Make the plot
plt.bar(r1, bars1, color='Red', width=barWidth, edgecolor='white', label='Theoretical')
plt.bar(r2, bars2, color='Black', width=barWidth, edgecolor='white', label='Simulation')
 
# Add xticks on the middle of the group bars
plt.xlabel('Distribution of # of Waiting', fontweight='bold')
plt.ylabel('Percentage of Total', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['0','1', '2', '3', '4', '5','6','7','8','9','10'])

# Create legend & Show graphic
plt.legend()
p = str(path + '\Waitingt500lambda1mu1over35.png')
plt.savefig(p)
```


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/35,10,20,20,3000)
test = time_distribution_cal(WaitListPick,Time)
test = normalize(test)
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = Dict.values()
bars2 = test.values()
 
# Set position of bar on X axis
r1 = list(Dict.keys())
r2 = [x + barWidth for x in r1]
 
# Make the plot
plt.bar(r1, bars1, color='Red', width=barWidth, edgecolor='white', label='Theoretical')
plt.bar(r2, bars2, color='Black', width=barWidth, edgecolor='white', label='Simulation')
 
# Add xticks on the middle of the group bars
plt.xlabel('Distribution of # of Waiting', fontweight='bold')
plt.ylabel('Percentage of Total', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['0','1', '2', '3', '4', '5','6','7','8','9','10'])
 
# Create legend & Show graphic
plt.legend()
p = str(path + '\Waitingt3000lambda1mu1over35.png')
# plt.savefig(p)
```


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/35,10,20,20,8000)
test = time_distribution_cal(WaitListPick,Time)
test = normalize(test)
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = Dict.values()
bars2 = test.values()
 
# Set position of bar on X axis
r1 = list(Dict.keys())
r2 = [x + barWidth for x in r1]
 
# Make the plot
plt.bar(r1, bars1, color='Red', width=barWidth, edgecolor='white', label='Theoretical')
plt.bar(r2, bars2, color='Black', width=barWidth, edgecolor='white', label='Simulation')
 
# Add xticks on the middle of the group bars
plt.xlabel('Distribution of # of Waiting', fontweight='bold')
plt.ylabel('Percentage of Total', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['0','1', '2', '3', '4', '5','6','7','8','9','10'])
 
# Create legend & Show graphic
plt.legend()
p = str(path + '\Waitingt8000lambda1mu1over35.png')
#plt.savefig(p)
```


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/35,10,20,20,20000)
test = time_distribution_cal(WaitListPick,Time)
test = normalize(test)
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = Dict.values()
bars2 = test.values()
 
# Set position of bar on X axis
r1 = list(Dict.keys())
r2 = [x + barWidth for x in r1]
 
# Make the plot
plt.bar(r1, bars1, color='Red', width=barWidth, edgecolor='white', label='Theoretical')
plt.bar(r2, bars2, color='Black', width=barWidth, edgecolor='white', label='Simulation')
 
# Add xticks on the middle of the group bars
plt.xlabel('Distribution of # of Waiting', fontweight='bold')
plt.ylabel('Percentage of Total', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['0','1', '2', '3', '4', '5','6','7','8','9','10'])
 
# Create legend & Show graphic
plt.legend()
p = str(path + '\Waitingt20000lambda1mu1over35.png')
#plt.savefig(p)
```


```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(1,1/35,10,20,20,100000)
test = time_distribution_cal(WaitListPick,Time)
test = normalize(test)
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = Dict.values()
bars2 = test.values()
 
# Set position of bar on X axis
r1 = list(Dict.keys())
r2 = [x + barWidth for x in r1]
 
# Make the plot
plt.bar(r1, bars1, color='Red', width=barWidth, edgecolor='white', label='Theoretical')
plt.bar(r2, bars2, color='Black', width=barWidth, edgecolor='white', label='Simulation')
 
# Add xticks on the middle of the group bars
plt.xlabel('Distribution of # of Waiting', fontweight='bold')
plt.ylabel('Percentage of Total', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['0','1', '2', '3', '4', '5','6','7','8','9','10'])
 
# Create legend & Show graphic
plt.legend()
p = str(path + '\Waitingt100000lambda1mu1over35.png')
# plt.savefig(p)
```

2. Use stationary distribution to find combination of lambda and mue that minimizes waiting time.

Function: **calculatewaittimelist**  
Description: Given the lambda value, iterate through different mu values and record waiting time of each combination. (fixed lambda value)
Input: (lambdav)  
Output: temp  
See Senior_Project_Function.py for detailed information

Example Usage: **calculatewaittimelist**(0.5)

Find best $\mu$ values for situations with certain $\lambda$ values by hard coding.


```python
DataWait = pd.DataFrame(columns= ["mu1","mu2","mu3","mu4","WaitingTimeTotalmu1","WaitingTimeTotalmu2","WaitingTimeTotalmu3","WaitingTimeTotalmu4"])
temp1 = calculatewaittimelist(0.5)
temp2 = calculatewaittimelist(1)
temp3 = calculatewaittimelist(1.5)
temp4 = calculatewaittimelist(2)

for i in range(5,36):
    DataWait = DataWait.append({"mu1":1/i,"mu2":1/i,"mu3":1/i,"mu4":1/i,
                                "WaitingTimeTotalmu1":temp1[i - 5],
                                "WaitingTimeTotalmu2":temp2[i - 5],
                                "WaitingTimeTotalmu3":temp3[i - 5],
                                "WaitingTimeTotalmu4":temp4[i - 5]}, ignore_index=True)
    
fig, ax = plt.subplots()
ax.scatter(x=DataWait.iloc[:,0],y=DataWait.iloc[:,4],c='DarkBlue',label = "lambda = 0.5")
ax.scatter(x=DataWait.iloc[:,1],y=DataWait.iloc[:,5],c='Green',label = "lambda = 1")
ax.scatter(x=DataWait.iloc[:,2],y=DataWait.iloc[:,6],c='Red',label = "lambda = 1.5")
ax.scatter(x=DataWait.iloc[:,3],y=DataWait.iloc[:,7],c='Pink',label = "lambda = 2")
plt.title(r'Expected Total Waiting List with Different $ \lambda$')
plt.xlabel(r'$\mu$')
plt.ylabel('Expected Total Waiting List')
ax.legend()
path = 'Pictures'
p = str(path + "\lambda(0.5+1+1.5+2)WaitingTimeTotal.png")
plt.savefig(p)

```

#  3 Data

1. Explore data itself


```python
data = pd.read_csv("201608_trip_data.csv")
data
path = 'Pictures'
```


```python
# checking data types
data.dtypes
```




    Trip ID             int64
    Duration            int64
    Start Date         object
    Start Station      object
    Start Terminal      int64
    End Date           object
    End Station        object
    End Terminal        int64
    Bike #              int64
    Subscriber Type    object
    Zip Code           object
    dtype: object




```python
# Convert object data type to datetime data type
data["Start Date"] = pd.to_datetime(data["Start Date"])
data["End Date"] = pd.to_datetime(data["End Date"])
```


```python
data.dtypes
```




    Trip ID                     int64
    Duration                    int64
    Start Date         datetime64[ns]
    Start Station              object
    Start Terminal              int64
    End Date           datetime64[ns]
    End Station                object
    End Terminal                int64
    Bike #                      int64
    Subscriber Type            object
    Zip Code                   object
    dtype: object




```python
# Extract date part only from date
data['Start Date Only'] = data['Start Date'].dt.date
data['End Date Only'] = data['End Date'].dt.date
```


```python
data
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Trip ID</th>
      <th>Duration</th>
      <th>Start Date</th>
      <th>Start Station</th>
      <th>Start Terminal</th>
      <th>End Date</th>
      <th>End Station</th>
      <th>End Terminal</th>
      <th>Bike #</th>
      <th>Subscriber Type</th>
      <th>Zip Code</th>
      <th>Start Date Only</th>
      <th>End Date Only</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>913465</td>
      <td>746</td>
      <td>2015-09-01 00:10:00</td>
      <td>San Francisco Caltrain 2 (330 Townsend)</td>
      <td>69</td>
      <td>2015-09-01 00:23:00</td>
      <td>San Francisco City Hall</td>
      <td>58</td>
      <td>238</td>
      <td>Subscriber</td>
      <td>94107</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>1</td>
      <td>913466</td>
      <td>969</td>
      <td>2015-09-01 00:15:00</td>
      <td>Clay at Battery</td>
      <td>41</td>
      <td>2015-09-01 00:31:00</td>
      <td>Washington at Kearny</td>
      <td>46</td>
      <td>16</td>
      <td>Subscriber</td>
      <td>94133</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>2</td>
      <td>913467</td>
      <td>233</td>
      <td>2015-09-01 00:15:00</td>
      <td>Davis at Jackson</td>
      <td>42</td>
      <td>2015-09-01 00:19:00</td>
      <td>Commercial at Montgomery</td>
      <td>45</td>
      <td>534</td>
      <td>Subscriber</td>
      <td>94111</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>3</td>
      <td>913468</td>
      <td>213</td>
      <td>2015-09-01 01:29:00</td>
      <td>Clay at Battery</td>
      <td>41</td>
      <td>2015-09-01 01:32:00</td>
      <td>Steuart at Market</td>
      <td>74</td>
      <td>312</td>
      <td>Subscriber</td>
      <td>94107</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>4</td>
      <td>913469</td>
      <td>574</td>
      <td>2015-09-01 01:33:00</td>
      <td>Steuart at Market</td>
      <td>74</td>
      <td>2015-09-01 01:42:00</td>
      <td>San Francisco Caltrain 2 (330 Townsend)</td>
      <td>69</td>
      <td>279</td>
      <td>Subscriber</td>
      <td>94107</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>313684</td>
      <td>1338404</td>
      <td>511</td>
      <td>2016-08-31 23:05:00</td>
      <td>Davis at Jackson</td>
      <td>42</td>
      <td>2016-08-31 23:14:00</td>
      <td>Post at Kearny</td>
      <td>47</td>
      <td>370</td>
      <td>Subscriber</td>
      <td>94109</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313685</td>
      <td>1338405</td>
      <td>701</td>
      <td>2016-08-31 23:06:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2016-08-31 23:17:00</td>
      <td>Spear at Folsom</td>
      <td>49</td>
      <td>175</td>
      <td>Subscriber</td>
      <td>94133</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313686</td>
      <td>1338406</td>
      <td>457</td>
      <td>2016-08-31 23:06:00</td>
      <td>Davis at Jackson</td>
      <td>42</td>
      <td>2016-08-31 23:13:00</td>
      <td>Spear at Folsom</td>
      <td>49</td>
      <td>504</td>
      <td>Subscriber</td>
      <td>94111</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313687</td>
      <td>1338407</td>
      <td>260</td>
      <td>2016-08-31 23:09:00</td>
      <td>Embarcadero at Bryant</td>
      <td>54</td>
      <td>2016-08-31 23:14:00</td>
      <td>Steuart at Market</td>
      <td>74</td>
      <td>226</td>
      <td>Subscriber</td>
      <td>94114</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313688</td>
      <td>1338408</td>
      <td>378</td>
      <td>2016-08-31 23:32:00</td>
      <td>Washington at Kearny</td>
      <td>46</td>
      <td>2016-08-31 23:38:00</td>
      <td>Embarcadero at Sansome</td>
      <td>60</td>
      <td>667</td>
      <td>Subscriber</td>
      <td>94111</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
  </tbody>
</table>
<p>313689 rows × 13 columns</p>
</div>



2. Explore the trip duration distribution of the whole system, converting unit from seconds to minutes


```python
test =  data[data["Duration"] <= 4000]
test["Duration"] = test["Duration"]/60
test["Duration"].plot.hist(bins=1000, alpha=0.5)
plt.xlabel("Durantion (minutes)")
path = 'Pictures'
p = str(path + "/DurationDistribution.png")
# plt.savefig(p)
```

3. Explore arrival rate and trip duration distribution of certain station


```python
# Checking how many stations are in the system
test['Start Station'].value_counts()
```




    San Francisco Caltrain (Townsend at 4th)    23465
    San Francisco Caltrain 2 (330 Townsend)     22286
    Harry Bridges Plaza (Ferry Building)        15469
    2nd at Townsend                             13967
    Steuart at Market                           13482
                                                ...  
    Sequoia Hospital                               13
    Mezes Park                                      7
    Redwood City Medical Center                     5
    Franklin at Maple                               1
    5th St at E. San Salvador St                    1
    Name: Start Station, Length: 80, dtype: int64




```python
# Extract information on one station
teststation1arrival = test[test["Start Station"] == "San Francisco Caltrain (Townsend at 4th)"]
teststation1return = test[test["End Station"] == "San Francisco Caltrain (Townsend at 4th)"]
```


```python
# Checking if the distributions are similar
teststation1arrival["Duration"].plot.hist(bins=1000, alpha=0.5,label = "Arrival",density = True)
teststation1return["Duration"].plot.hist(bins=1000, alpha=0.5,label = "Return",density = True)
plt.xlabel("Durantion (minutes)")
plt.legend(loc='upper right')
p = str(path + '\DataDistributionCheck.png')
# plt.savefig(p)
```


```python
# Extract bike trips that are related to one station
teststation1 = test[(test["Start Station"] == "San Francisco Caltrain (Townsend at 4th)") | (test["End Station"] == "San Francisco Caltrain (Townsend at 4th)")]
```


```python
teststation1
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Trip ID</th>
      <th>Duration</th>
      <th>Start Date</th>
      <th>Start Station</th>
      <th>Start Terminal</th>
      <th>End Date</th>
      <th>End Station</th>
      <th>End Terminal</th>
      <th>Bike #</th>
      <th>Subscriber Type</th>
      <th>Zip Code</th>
      <th>Start Date Only</th>
      <th>End Date Only</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>11</td>
      <td>913476</td>
      <td>12.950000</td>
      <td>2015-09-01 05:02:00</td>
      <td>Davis at Jackson</td>
      <td>42</td>
      <td>2015-09-01 05:15:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>86</td>
      <td>Subscriber</td>
      <td>94111</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>14</td>
      <td>913479</td>
      <td>7.416667</td>
      <td>2015-09-01 05:43:00</td>
      <td>Civic Center BART (7th at Market)</td>
      <td>72</td>
      <td>2015-09-01 05:50:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>315</td>
      <td>Subscriber</td>
      <td>94103</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>16</td>
      <td>913481</td>
      <td>7.650000</td>
      <td>2015-09-01 06:01:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2015-09-01 06:09:00</td>
      <td>2nd at Folsom</td>
      <td>62</td>
      <td>579</td>
      <td>Subscriber</td>
      <td>94002</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>31</td>
      <td>913496</td>
      <td>14.516667</td>
      <td>2015-09-01 06:36:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2015-09-01 06:50:00</td>
      <td>Market at 10th</td>
      <td>67</td>
      <td>531</td>
      <td>Subscriber</td>
      <td>94306</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>33</td>
      <td>913500</td>
      <td>12.250000</td>
      <td>2015-09-01 06:44:00</td>
      <td>Broadway St at Battery St</td>
      <td>82</td>
      <td>2015-09-01 06:57:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>282</td>
      <td>Subscriber</td>
      <td>94133</td>
      <td>2015-09-01</td>
      <td>2015-09-01</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>313645</td>
      <td>1338350</td>
      <td>9.950000</td>
      <td>2016-08-31 20:17:00</td>
      <td>Steuart at Market</td>
      <td>74</td>
      <td>2016-08-31 20:27:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>377</td>
      <td>Subscriber</td>
      <td>94158</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313672</td>
      <td>1338392</td>
      <td>10.616667</td>
      <td>2016-08-31 22:05:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2016-08-31 22:16:00</td>
      <td>South Van Ness at Market</td>
      <td>66</td>
      <td>524</td>
      <td>Subscriber</td>
      <td>94117</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313674</td>
      <td>1338394</td>
      <td>5.750000</td>
      <td>2016-08-31 22:10:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2016-08-31 22:16:00</td>
      <td>2nd at Townsend</td>
      <td>61</td>
      <td>415</td>
      <td>Subscriber</td>
      <td>94107</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313677</td>
      <td>1338397</td>
      <td>16.300000</td>
      <td>2016-08-31 22:28:00</td>
      <td>Harry Bridges Plaza (Ferry Building)</td>
      <td>50</td>
      <td>2016-08-31 22:45:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>630</td>
      <td>Subscriber</td>
      <td>94158</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
    <tr>
      <td>313685</td>
      <td>1338405</td>
      <td>11.683333</td>
      <td>2016-08-31 23:06:00</td>
      <td>San Francisco Caltrain (Townsend at 4th)</td>
      <td>70</td>
      <td>2016-08-31 23:17:00</td>
      <td>Spear at Folsom</td>
      <td>49</td>
      <td>175</td>
      <td>Subscriber</td>
      <td>94133</td>
      <td>2016-08-31</td>
      <td>2016-08-31</td>
    </tr>
  </tbody>
</table>
<p>52039 rows × 13 columns</p>
</div>




```python
# Trip Duration
teststation1["Duration"].mean()
```




    11.082856127135376




```python
# return rate
1/11.082856127135376
```




    0.09022944884681756




```python
# Arrival Rate
teststation1["Trip ID"].count()/(teststation1["Start Date Only"].nunique()*8*60)
```




    0.29702625570776253




```python
teststation1["Bike #"].value_counts()
```




    586    242
    497    202
    134    194
    484    192
    356    188
          ... 
    57       2
    27       2
    84       1
    82       1
    657      1
    Name: Bike #, Length: 444, dtype: int64




```python
Time,BikesInUseList,BikesAtStationList,WaitListPick,WaitListReturn = BirthDeathsimulate(0.29702625570776253,1/11.082856127135376,424,20,20,2000)
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,BikesInUseList,label="# of bikes in use")
plt.plot(Time,BikesAtStationList,label="# of bikes at station")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of Bikes',fontsize=20)
plt.title(r'$\lambda = 0.3, \mu = 0.09, T = 444, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\Data.png')
plt.savefig(p)
```


```python
fig=plt.figure(figsize=(15, 8))
plt.plot(Time,WaitListPick,label="# of People wait to pick")
plt.plot(Time,WaitListReturn,label="# of People wait to return")
plt.legend(loc="upper left",fontsize = 11)
plt.xlabel('Time step',fontsize = 20)
plt.ylabel('Number of People',fontsize=20)
plt.title(r'$\lambda = 0.3, \mu = 0.09, T = 444, c = 10$, 2000 steps',fontsize = 20)
p = str(path + '\DataPeoplewaiting.png')
plt.savefig(p)
```


```python

```


```python

```

02717c7b86d25ef266fa8a672a985b5fc2a07ad3
ipynb
Jupyter Notebook
Simulation.ipynb
MingLyu-byte/Senior-Project
745a15cce893861bdb972df038faf3ea9e918d31
MIT
Simulation.ipynb
MingLyu-byte/Senior-Project
745a15cce893861bdb972df038faf3ea9e918d31
MIT
Simulation.ipynb
MingLyu-byte/Senior-Project
745a15cce893861bdb972df038faf3ea9e918d31
MIT
Qwen/Qwen-72B
1. YES
2. YES