def linear_decay(x0, alpha, T, t):
    """Compute the linear decay rate of quantity x at time t.

    x(t) = x0 - (1-alpha) * x0 * t / T   if t <= T
    x(t) = alpha * x0                    if t > T

    Args:
      x0: Initial value
      alpha: Linear decay coefficient (alpha > 0)
      T: Time at which to stop decaying
      t: Current time
    """
    if t <= T:
      return x0 - (1 - alpha) * x0 * t / T
    else:
      return alpha * x0