Consecutive-state behavior

Longest Run

Compute distributions for the longest consecutive run of a selected state or of any identical state.

What it computes

P(Lₙ < L), P(Lₙ = L), and tail probabilities

Supported models

Unconditional categorical trials and conditional binary trials

Main inputs

n, number of states, run threshold L, probability vector or conditional count N

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 L_n, the longest run length in a sequence of trials.

The app supports two probability cases:

1. Unconditional case
   The trials are independent m-state categorical trials.
   A probability vector Pi is used:
       Pi = [P(X=1), P(X=2), ..., P(X=m)]
2. Conditional case
   This case is only for two-state trials.
   The app conditions on the total number N of occurrences of
   the selected state in the sequence.
   The conditional calculation uses:
       [p, pn1] = Fast_Cond_Longest_Run(n, L, N)
   where p = P(L_n < L | N).
Run Modes

The app also supports two run modes in the unconditional case:

1. Selected states Computes the longest run of one specified state only. Example: longest run of state 2.

2. All the states Computes the longest run of any identical consecutive state. This means the maximum run length among all states.

For the conditional case, the app uses selected-state logic only. Because Fast_Cond_Longest_Run is a binary conditional function, the conditional case is restricted to m = 2.

Inputs
m-state trials
    Enter the number of possible states, m.
    The states are labeled 1, 2, ..., m.
    In the unconditional case:
        m can be 2, 3, 4, ...
    In the conditional case:
        m must be 2.
        The app should force or require m-state trials = 2.
Length of the Sequence
    Enter n, the total sequence length.
Length of the Longest Run
    Enter k, the run length of interest.
    The app reports probabilities involving L_n at this k.
Enter the probability Pi
    Used only in the unconditional case.
    Opens a probability-entry window.
    Enter one probability for each state.
    The probabilities must be nonnegative and sum to 1.
    Default probabilities are equal:
        [1/m, 1/m, ..., 1/m]
    Example for m = 3:
        Pi = [0.3 0.5 0.2]
Number of selected-state occurrences N
    Used only in the conditional case.
    Enter N, the total number of occurrences of the selected
    state in the whole sequence.
    N must be an integer from 0 to n.
    Example:
        n = 20
        selected state = 1
        N = 8
    This means the app computes probabilities conditional on
    exactly eight 1s occurring in the sequence.
Selected-state input
    Used when Selected states is chosen.
    Also used in the conditional case.
    In the unconditional case:
        Enter one integer state index from 1 to m.
    In the conditional case:
        Enter 1 or 2 only.
    Example:
        2
    This means: compute the longest run of state 2 only.
How To Use The App: Unconditional Case
Step 1. Click Clear all items
    This starts from a clean screen.
Step 2. Select Unconditional
    Use this when trials are independent with probabilities Pi.
Step 3. Enter m-state trials
    Example:
        m = 3
Step 4. Enter Length of the Sequence
    Example:
        n = 30
Step 5. Enter Length of the Longest Run
    Example:
        k = 5
Step 6. Click Enter the probability Pi
    A probability-entry window opens.
    If m = 3, enter three probabilities.
    Example:
        P(State 1) = 0.3
        P(State 2) = 0.5
        P(State 3) = 0.2
    Then click Save.
Step 7. Select one run mode
    A. Select All the states
       Use this when the longest run can be from any state.
       No selected-state number is needed.
    B. Select Selected states
       Use this when the longest run should be computed for
       one specified state only.
       Enter the selected state number in the selected-state box.
Step 8. Click Calculate the Probability
    The app computes and displays the probability results.
How To Use The App: Conditional Case
Step 1. Click Clear all items
Step 2. Select Conditional
    This case is only for two-state trials.
    The app should force m-state trials to 2.
Step 3. Enter Length of the Sequence
    Example:
        n = 20
Step 4. Enter Length of the Longest Run
    Example:
        k = 4
Step 5. Enter selected state
    Enter 1 or 2.
    Example:
        selected state = 1
Step 6. Enter Number of selected-state occurrences N
    Example:
        N = 8
    This means there are exactly eight occurrences of the
    selected state in the full sequence.
Step 7. Click Calculate the Probability
    The app uses Fast_Cond_Longest_Run to compute conditional
    longest-run probabilities.
Important conditional-case notes:
    Pi is not used.
    Enter the probability Pi should be hidden or disabled.
    All the states is not used.
    The conditional case is for selected-state runs only.
    N can be 0, but it cannot be larger than n.
Understanding The Results

Let L_n be the longest run length.

The results panel reports:

    P(L_n = k)
        The probability that the longest run is exactly k.
    P(L_n <= k)
        The cumulative probability up to k.
    P(L_n >= k)
        The survival/tail probability at k.

Unconditional case:

    The app uses:
        [pmf, cdf, F] = fmci_longest_run_dist(n, Pi, k, mode, selState)
    For Selected states:
        [pmf, cdf, F] = fmci_longest_run_dist(n, Pi, k, ''selected'', selState)
    For All the states:
        [pmf, cdf, F] = fmci_longest_run_dist(n, Pi, k, ''all'', [])

Conditional case:

    The app uses:
        probLTk = Fast_Cond_Longest_Run(n, k, N)
        probLEk = Fast_Cond_Longest_Run(n, k + 1, N)
    Since Fast_Cond_Longest_Run(n, L, N) returns P(L_n < L | N):
        P(L_n < k | N)  = Fast_Cond_Longest_Run(n, k, N)
        P(L_n <= k | N) = Fast_Cond_Longest_Run(n, k + 1, N)
        P(L_n = k | N)  = P(L_n <= k | N) - P(L_n < k | N)
        P(L_n >= k | N) = 1 - P(L_n < k | N)
Example 1: Unconditional, Two-State, All The States
Question: In 20 binary trials, what is the distribution of
the longest run of either state?
Inputs:
    Probability case             = Unconditional
    m-state trials               = 2
    Length of the Sequence        = 20
    Length of the Longest Run     = 4
    Pi                            = [0.5 0.5]
    Run mode                      = All the states
Interpretation:
    The app considers runs of state 1 and runs of state 2.
    L_n is the longest identical consecutive run in the
    whole sequence.
Output meaning:
    P(L_n = 4), P(L_n <= 4), and P(L_n >= 4).
Example 2: Unconditional, Three-State, Selected State
Question: In 30 trials with three possible states, what is
the distribution of the longest run of state 2 only?
Inputs:
    Probability case             = Unconditional
    m-state trials               = 3
    Length of the Sequence        = 30
    Length of the Longest Run     = 5
    Pi                            = [0.3 0.5 0.2]
    Run mode                      = Selected states
    Selected state                = 2
Interpretation:
    Only consecutive 2s count as a run.
    States 1 and 3 break the run.
Output meaning:
    P(L_n = 5), P(L_n <= 5), and P(L_n >= 5) for the
    longest run of state 2.
Example 3: Unconditional, Four-State, Unequal Probabilities
Question: In 50 trials with four states, what is the
distribution of the longest run of any identical state?
Inputs:
    Probability case             = Unconditional
    m-state trials               = 4
    Length of the Sequence        = 50
    Length of the Longest Run     = 6
    Pi                            = [0.1 0.2 0.3 0.4]
    Run mode                      = All the states
Interpretation:
    Runs of 1s, 2s, 3s, and 4s are all considered.
    L_n is the largest of those run lengths.
Example 4: Conditional, Two-State, Selected State
Question: In 20 two-state trials, conditional on exactly 8
occurrences of state 1, what is the longest-run probability
for state 1?
Inputs:
    Probability case             = Conditional
    m-state trials               = 2
    Length of the Sequence        = 20
    Length of the Longest Run     = 4
    Selected state                = 1
    Number of occurrences N       = 8
Function calls used by the app:
    P(L_n < 4 | N=8)  = Fast_Cond_Longest_Run(20, 4, 8)
    P(L_n <= 4 | N=8) = Fast_Cond_Longest_Run(20, 5, 8)
Interpretation:
    Among all binary sequences of length 20 with exactly eight
    selected-state occurrences, the app computes the longest run
    distribution for the selected state.
Common Mistakes To Avoid
1. Pi must have exactly m entries in the unconditional case.
    If m = 4, Pi must have four probabilities.
2. Pi must sum to 1 in the unconditional case.
    Examples:
        Valid:   [0.25 0.25 0.25 0.25]
        Invalid: [0.25 0.25 0.25]
        Invalid: [0.5 0.5 0.5 0.5]
3. The selected state must be between 1 and m.
    If m = 3, valid selected states are 1, 2, and 3.
    In the conditional case, valid selected states are only 1 and 2.
4. The run length k must be positive and no larger than n.
    If n = 20, use k = 1, 2, ..., 20.
5. If you change m after entering Pi, click Enter the
   probability Pi again.
    The probability window should reset to equal default
    probabilities when the number of states changes.
6. In the conditional case, N must satisfy 0 <= N <= n.
    N is the total number of selected-state occurrences.
7. In the conditional case, do not enter Pi.
    Pi is not used because the calculation conditions on N.
8. In the conditional case, do not use All the states.
    The conditional function is for selected-state longest runs
    in two-state trials.
Eigvalue/Vector Button

The EigValue/vector button is optional.

In the unconditional case, it is used to inspect the largest eigenvalue and the right eigenvector of the FMCI transition matrix used for the current input case.

In the conditional case, there is no single fixed transition matrix in the same unconditional sense, because the transition probabilities depend on the remaining number of selected-state occurrences. Therefore, the app should display a clear message in the eigenvalue/vector panel instead of overlapping eigenvalue text.

The app writes eigenvalue output into one scrollable text area in the eigenvalue/vector panel to avoid overlapping text.

Clearing The App
Click Clear all items to remove:
    input fields
    saved probability vector Pi
    selected-state input
    conditional N input
    results panel output
    eigenvalue/vector panel output
Closing The App

In the web app, switch tabs or close the browser page to leave the Longest Run app.