Where Will the Bikes Be?

Information Theory & Stochastic ProcessesMedium+20 XP+20

Where Will the Bikes Be?

A city bike-share scheme has nnn docking stations. Months of trip logs show how a single bike tends to move during one day:

  • P[i][j] is the probability that a bike which starts the day at station i ends that day at station j.
  • Every row of P sums to 111, because a bike always ends the day somewhere.
  • Each bike moves independently of the others, and where it goes tomorrow depends only on where it is tonight (the Markov property).

Tonight the fleet is fleet[i] bikes at station i. To plan the redistribution truck, the operator wants the expected number of bikes at each station after days more days.

Task: write fleet_forecast(P, fleet, days) returning a list with the expected number of bikes at each station after exactly days days, each rounded to 4 decimal places.

  • days is a whole number, 0 or more. After 0 days nothing has moved yet.
  • P is square and its size matches fleet. Expected counts can be fractional, and that's fine.

Once your function works, try one P with a large days and two very different starting fleets, and compare the two answers.

Markov Chain

Markov Chain Simulator

Observe convergence to the stationary state as the transition matrix powers update the state probability vector over time.

Transition Rates

P(State A → State B)0.30
P(State B → State A)0.40
TRANSITION MATRIX P
[
0.700.300.400.60
]
Rows sum to exactly 1.0
State Transition Diagram
0.300.400.700.60SUNNY ARAINY B
Probability State Distribution
Sunny A:100.00%
Rainy B:0.00%
Transition Steps:0
Stationary Target:[57.1%, 42.9%]
Regardless of which state you start from (Sunny or Rainy), repeated multiplication by the transition matrix converges to the same stationary distribution.

Examples

P = [[0.8, 0.2], [0.35, 0.65]]
fleet = [100, 0]
days = 2
returns [71.0, 29.0]
P = [[0.6, 0.3, 0.1], [0.2, 0.5, 0.3], [0.1, 0.3, 0.6]]
fleet = [40, 30, 30]
days = 3
returns [28.77, 37.44, 33.79]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run