def _ps(t_s, t_e, S, pe, F, G):
    """Marginal likelihood of S. Arguments:
      t_s --- start of interval
      t_e --- end of interval
      S   --- sequence of events (t_i, y_i)
      pe  --- prior per-event intrusion probability 
      F   --- interarrival distribution
      G   --- mark distribution
    Returns the marginal likelihood of S and a[], as a tuple.
    """
    N = len(S)

    # Section 5.3, Lemma 2

    a = [None] * N
    for k in range(N):
        tk, yk = S[k]
        a[k] = pe ** k * F.if2(tk - t_s) / F.meanpdf
        for j in range(k):
            tj = S[j][0]
            a[k] += a[j] * pe ** (k - j - 1) * F.pdf(tk - tj) / F.meanpdf
        a[k] *= (1 - pe) * G.pdf(yk) / G.meanpdf
    A = pe ** N * F.ixf2(t_e - t_s) / F.meanpdf
    for j in range(N):
        tj = S[j][0]
        A += a[j] * pe ** (N - j - 1) * F.if2(t_e - tj) / F.meanpdf

    return A, a