Pattern occurrence timing

Waiting Time

Compute the exact probability that the r-th occurrence of one or more patterns has not occurred by time n.

What it computes

P(Wᵣ > n) = P(Xₙ < r)

Supported models

Independent or Markov-dependent; overlapping or non-overlapping counting

Main inputs

States, patterns, r, n, probabilities, counting option

Single-window web-app note

The original desktop instructions sometimes refer to buttons that open separate input windows. In this single-window web version, enter those same values directly in the text boxes or tables on the current tab. The mathematical meaning of the inputs and outputs is unchanged.

Purpose

This app computes probabilities for the waiting time until patterns occur in a sequence of m-state trials.

Let W_r be the waiting time until the r-th occurrence of any entered pattern. The app calculates:

    P(W_r > n)
        Probability that the r-th occurrence has NOT happened
        by time n.
    P(W_r <= n)
        Probability that the r-th occurrence HAS happened
        by time n.

Equivalently, if X_n is the number of pattern occurrences in the first n trials, then:

    P(W_r > n)  =  P(X_n < r)
    P(W_r <= n) =  P(X_n >= r)
Main Inputs
m-state trials
    Enter the number of possible states, m.
    States are labeled: 1, 2, ..., m.
Number of Patterns
    Enter how many patterns you want to study.
    Then click Enter The Patterns and enter that many patterns.
Enter The Patterns
    Opens the pattern-entry window.
    Each pattern should use state labels 1,...,m.
    Put spaces between symbols.
    Correct examples for m = 2:
        1 2
        2 2
        1 1 2
    Correct examples for m = 4:
        1 3 4
        2 2
    Incorrect examples:
        0 1       because state 0 is not allowed
        1 5       if m = 4
        A B       because pattern entries must be numeric
Length of Sequence
    Enter n, the time point or sequence length.
Waiting order r
    Enter which occurrence you are waiting for.
    r = 1 means the first occurrence.
    r = 2 means the second occurrence.
    r = 3 means the third occurrence.
Over / Non_Over Counting
Over
    Overlapping pattern occurrences are allowed.
    Example: pattern = 1 1, sequence = 1 1 1
        The pattern occurs at positions 1-2 and 2-3.
        These two occurrences overlap.
non_Over
    Non-overlapping counting is used.
    Example: pattern = 1 1, sequence = 1 1 1
        The occurrence at positions 1-2 is counted.
        The overlapping occurrence at positions 2-3 is not
        counted as a separate non-overlapping occurrence.
Dependency Type
Independent
    Each trial is independent with the same state probability
    vector.
    Use the button:
        Enter State Probabilities
    The probability vector is:
        Pi = [P(State 1), P(State 2), ..., P(State m)]
    Default values are equal probabilities:
        [1/m, 1/m, ..., 1/m]
Markov Dependent
    The sequence follows an m-state Markov chain.
    Use these buttons:
        Enter the Initial P
        Enter the Transition P
    Initial P is:
        ini = [P(X_1=1), P(X_1=2), ..., P(X_1=m)]
    Transition P is an m-by-m matrix:
        P(i,j) = P(X_t = j | X_{t-1} = i)
    Every row of the transition matrix must sum to 1.
    Default initial probabilities are equal:
        [1/m, 1/m, ..., 1/m]
    Default transition probabilities are also equal in every
    row: [1/m, 1/m, ..., 1/m]
How To Use The App: Independent Case

Example 1: First waiting time for one pattern

Question: In 20 independent binary trials, what is the
probability that pattern 1 2 has not occurred by time 20?
Use these inputs:
    m-state trials           = 2
    Number of Patterns       = 1
    Pattern                  = 1 2
    Length of Sequence       = 20
    Waiting order r          = 1
    Counting                 = Over
    Dependency               = Independent
    State probabilities      = [0.5 0.5]
Steps:
    1. Click Clear all items.
    2. Enter m-state trials = 2.
    3. Enter Number of Patterns = 1.
    4. Click Enter The Patterns.
    5. Enter Pattern 1 as: 1 2.
    6. Click Save.
    7. Enter Length of Sequence = 20.
    8. Enter Waiting order r = 1.
    9. Select Over.
   10. Select Independent.
   11. Click Enter State Probabilities.
   12. Enter P(State 1)=0.5 and P(State 2)=0.5.
   13. Click Save.
   14. Click Calculate the Probability.
The Results panel displays:
    P(W_1 > 20)
    P(W_1 <= 20)

Example 2: Second occurrence of a pattern

Question: In 30 independent binary trials, what is the
probability that pattern 1 1 has occurred at least twice by
time 30?
Use these inputs:
    m-state trials           = 2
    Number of Patterns       = 1
    Pattern                  = 1 1
    Length of Sequence       = 30
    Waiting order r          = 2
    Counting                 = Over
    Dependency               = Independent
    State probabilities      = [0.4 0.6]
Interpretation:
    P(W_2 <= 30) is the probability that the second occurrence
    of pattern 1 1 has happened by time 30.

Example 3: Multiple patterns

Question: In a three-state sequence, what is the probability
that at least one of two patterns has occurred by time 25?
Use these inputs:
    m-state trials           = 3
    Number of Patterns       = 2
    Pattern 1                = 1 2
    Pattern 2                = 3 3
    Length of Sequence       = 25
    Waiting order r          = 1
    Counting                 = Over
    Dependency               = Independent
    State probabilities      = [0.2 0.5 0.3]

The app treats the waiting time as the time until any entered pattern occurs.

How To Use The App: Markov Dependent Case

Example 4: Markov-dependent binary sequence

Question: In a Markov-dependent binary sequence, what is the
probability that pattern 1 1 has not occurred by time 20?
Use these inputs:
    m-state trials           = 2
    Number of Patterns       = 1
    Pattern                  = 1 1
    Length of Sequence       = 20
    Waiting order r          = 1
    Counting                 = Over
    Dependency               = Markov Dependent
Initial probability:
    ini = [0.6 0.4]
Transition matrix:
    P = [0.7 0.3
         0.2 0.8]
Meaning:
    P(X_1=1) = 0.6
    P(X_1=2) = 0.4
    P(1 -> 1) = 0.7
    P(1 -> 2) = 0.3
    P(2 -> 1) = 0.2
    P(2 -> 2) = 0.8
Steps:
    1. Click Clear all items.
    2. Enter m-state trials = 2.
    3. Enter Number of Patterns = 1.
    4. Click Enter The Patterns and enter: 1 1.
    5. Enter Length of Sequence = 20.
    6. Enter Waiting order r = 1.
    7. Select Markov Dependent.
    8. Click Enter the Initial P and enter [0.6 0.4].
    9. Click Enter the Transition P and enter the matrix above.
   10. Click Calculate the Probability.

Example 5: Markov-dependent three-state sequence

Use these inputs:
    m-state trials           = 3
    Number of Patterns       = 2
    Pattern 1                = 1 2 1
    Pattern 2                = 3 3
    Length of Sequence       = 40
    Waiting order r          = 1
    Counting                 = non_Over
    Dependency               = Markov Dependent
Initial probability:
    ini = [0.2 0.5 0.3]
Transition matrix:
    P = [0.6 0.3 0.1
         0.2 0.5 0.3
         0.1 0.4 0.5]
Results Panel

The Results panel reports:

    P(W_r > n)
        Probability that the r-th occurrence has not occurred
        by time n.
    P(W_r <= n)
        Probability that the r-th occurrence has occurred by
        time n.
    mode and count
        Shows whether the calculation used Independent or
        Markov Dependent probabilities, and Over or non_Over
        counting.
View The Pattern(S)

Click View the Pattern(s) to display the patterns currently saved in the app.

Mean Button

The Mean button is optional. If enabled in your version, it computes a truncated mean using the survival probabilities:

    1 + sum_{t=1}^n P(W_r > t)

This is the expected value of min(W_r, n+1).

Eigvalue/Vector Button

The L_eigValue/vector button is optional.

It displays the dominant eigenvalue and a right eigenvector of the transient FMCI matrix used in the waiting-time calculation.

The output should appear in one clean text area inside the eigenvalue/vector panel to avoid overlapping text.

Clear All Items

Click Clear all items before starting a new calculation.

This should clear input fields, saved patterns, saved probabilities, saved transition matrix, results, and the eigenvalue/vector output.

The Waiting order r field is usually reset to 1.

Common Mistakes To Avoid
  1. Pattern symbols must be between 1 and m.
  2. Number of Patterns must match the number of patterns entered.
  3. Probability vectors must sum to 1.
  4. Transition matrix rows must sum to 1.
  5. For Independent, use Enter State Probabilities.
  6. For Markov Dependent, use both Initial P and Transition P.
  7. If you change m-state trials, enter probabilities again.
  8. If the result seems unexpected, click View the Pattern(s).
Function Used By The App

The app calls:

    [probGTn, probGTnCurve, PGF] = Waiting_time(...)

The main returned probability is:

    probGTn = P(W_r > n)

and the app also displays:

    1 - probGTn = P(W_r <= n)