Show Non-Central Chi-squared Distribution in Statistics using Python

The non-central chi-squared distribution is a probability distribution used in statistical power analysis and hypothesis testing. Unlike the standard chi-squared distribution, it includes a non-centrality parameter that shifts the distribution, making it useful for modeling scenarios with non-zero effects.

Understanding the Non-Central Chi-squared Distribution

The non-central chi-squared distribution generalizes the standard chi-squared distribution by adding a non-centrality parameter. It has two key parameters:

  • df degrees of freedom (controls the shape)

  • nc non-centrality parameter (controls the location shift)

This distribution appears in signal processing, wireless communication, and statistical power analysis where you need to model the presence of a signal or effect.

Implementing Non-Central Chi-squared Distribution

We'll use Python's scipy.stats.ncx2 function to create and visualize the distribution ?

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import ncx2

# Set parameters
df = 5    # degrees of freedom  
nc = 2    # non-centrality parameter

# Create distribution object
dist = ncx2(df, nc)

# Generate x-values for plotting
x = np.linspace(0, 20, 100)

# Calculate PDF and CDF
pdf = dist.pdf(x)
cdf = dist.cdf(x)

print("Distribution parameters:")
print(f"Degrees of freedom: {df}")
print(f"Non-centrality parameter: {nc}")
print(f"Mean: {dist.mean():.2f}")
print(f"Variance: {dist.var():.2f}")
Distribution parameters:
Degrees of freedom: 5
Non-centrality parameter: 2
Mean: 7.00
Variance: 18.00

Visualizing PDF and CDF

Let's plot both the probability density function and cumulative distribution function ?

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import ncx2

# Set parameters
df = 5
nc = 2
dist = ncx2(df, nc)
x = np.linspace(0, 20, 100)

# Calculate functions
pdf = dist.pdf(x)
cdf = dist.cdf(x)

# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Plot PDF
ax1.plot(x, pdf, 'b-', linewidth=2, label=f'PDF (df={df}, nc={nc})')
ax1.set_title('Non-Central Chi-Squared Distribution (PDF)')
ax1.set_xlabel('x')
ax1.set_ylabel('Probability Density')
ax1.grid(True, alpha=0.3)
ax1.legend()

# Plot CDF
ax2.plot(x, cdf, 'r-', linewidth=2, label=f'CDF (df={df}, nc={nc})')
ax2.set_title('Non-Central Chi-Squared Distribution (CDF)')
ax2.set_xlabel('x')
ax2.set_ylabel('Cumulative Probability')
ax2.grid(True, alpha=0.3)
ax2.legend()

plt.tight_layout()
plt.show()

Comparing Different Parameters

Let's compare distributions with different non-centrality parameters to see the effect ?

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import ncx2

# Generate x-values
x = np.linspace(0, 25, 200)

# Different non-centrality parameters
nc_values = [0, 2, 5, 10]
df = 5

plt.figure(figsize=(10, 6))

for nc in nc_values:
    dist = ncx2(df, nc)
    pdf = dist.pdf(x)
    plt.plot(x, pdf, linewidth=2, label=f'nc={nc}')

plt.title('Non-Central Chi-Squared Distribution: Effect of Non-centrality Parameter')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

# Show statistics
print("Distribution Statistics:")
print("nc\tMean\tVariance")
print("-" * 20)
for nc in nc_values:
    dist = ncx2(df, nc)
    print(f"{nc}\t{dist.mean():.1f}\t{dist.var():.1f}")
Distribution Statistics:
nc	Mean	Variance
--------------------
0	5.0	10.0
2	7.0	18.0
5	10.0	30.0
10	15.0	50.0

Key Properties

Property Formula Description
Mean df + nc Expected value
Variance 2(df + 2×nc) Spread of distribution
Mode df + nc - 2 Most likely value (if > 0)

Conclusion

The non-central chi-squared distribution extends the standard chi-squared by adding a non-centrality parameter that shifts the distribution. Use scipy.stats.ncx2 to create and analyze this distribution in Python. Higher non-centrality values shift the distribution to the right and increase both mean and variance.

Updated on: 2026-03-27T15:43:31+05:30

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements