Program to find expected value of maximum occurred frequency values of expression results in Python

When analyzing M expressions with results ranging from 1 to N, we need to find the expected value of the maximum frequency among all possible outcomes. This problem involves calculating probabilities using combinatorics and the inclusion-exclusion principle.

Problem Understanding

Given M expressions with results in range [1, N], we want to find the expected value of the maximum frequency. For each possible sequence of expression results, we count the frequency of the most common value.

For example, with M = 3, N = 3:

Sequence Maximum Frequency
111 3
112 2
113 2
121 2
122 2
123 1
131 2
132 1
133 2
211 2
212 2
213 1
221 2
222 3
223 2
231 1
232 2
233 2
311 2
312 1
313 2
321 1
322 2
323 2
331 2
332 2
333 3

Solution Approach

The solution uses the inclusion-exclusion principle to calculate the probability that the maximum frequency is exactly k. We implement a combinatorial function and apply dynamic programming for efficiency ?

combination = {}

def nCr(n, k_in):
    k = min(k_in, n - k_in)
    if n < k or k < 0:
        return 0
    elif (n, k) in combination:
        return combination[(n, k)]
    elif k == 0:
        return 1
    elif n == k:
        return 1
    else:
        a = 1
        for cnt in range(k):
            a *= (n - cnt)
            a //= (cnt + 1)
            combination[(n, cnt + 1)] = a
        return a

def solve(M, N):
    arr = []
    
    # Calculate probability for each frequency level
    for k in range(2, M + 2):
        a = 1
        s = 0
        for i in range(M // k + 2):
            if M < i * k:
                break
            s += a * nCr(N, i) * nCr(N - 1 + M - i * k, M - i * k)
            a *= -1
        arr.append(s)
    
    total = arr[-1]
    
    # Calculate differences for exact probabilities
    diff = [arr[0]] + [arr[cnt + 1] - arr[cnt] for cnt in range(M - 1)]
    
    # Calculate expected value
    output = sum(diff[cnt] * (cnt + 1) / total for cnt in range(M))
    return output

# Test the function
M = 3
N = 3
result = solve(M, N)
print(f"Expected value: {result:.6f}")

The output of the above code is ?

Expected value: 2.200000

How the Algorithm Works

The algorithm calculates probabilities using these key steps:

  1. Combinatorial Function: Implements nCr with memoization for efficient calculation of combinations
  2. Inclusion-Exclusion: For each frequency level k, calculates the probability using alternating sums
  3. Probability Distribution: Converts cumulative probabilities to exact probabilities for each frequency
  4. Expected Value: Computes the weighted sum of frequencies and their probabilities

Verification

For M=3, N=3, we have 27 total sequences. The expected value calculation ?

# Manual verification for M=3, N=3
sequences_with_freq = {
    1: 6,  # 6 sequences have max frequency 1
    2: 18, # 18 sequences have max frequency 2  
    3: 3   # 3 sequences have max frequency 3
}

total_sequences = 27
expected_manual = sum(freq * count / total_sequences 
                     for freq, count in sequences_with_freq.items())

print(f"Manual calculation: {expected_manual:.6f}")
print(f"Algorithm result: {solve(3, 3):.6f}")
Manual calculation: 2.200000
Algorithm result: 2.200000

Conclusion

This algorithm efficiently calculates the expected value of maximum frequency using combinatorics and inclusion-exclusion principle. The approach scales well for larger values of M and N compared to brute force enumeration.

Updated on: 2026-03-26T15:26:13+05:30

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements