Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit da0cdc2

Browse files
[Term Entry] Chi-Square Distribution (#6272)
* Created the entry * added Data Distributions * minor fixes * Update chi-square-distribution.md ---------
1 parent 084aa8c commit da0cdc2

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
Title: 'Chi-Square Distribution'
3+
Description: 'The chi-square distribution is a continuous probability distribution used primarily in hypothesis testing and confidence interval estimation.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'AI'
7+
Tags:
8+
- 'Data Distributions'
9+
- 'Chi-Square'
10+
- 'Statistics'
11+
CatalogContent:
12+
- 'learn-data-science'
13+
- 'paths/data-science'
14+
---
15+
16+
The **chi-square distribution** is derived from the sum of squared standard normal variables. It plays a crucial role in statistical tests, such as the chi-square test for independence and goodness of fit. The shape of the distribution varies with its degrees of freedom; for lower degrees of freedom, it is skewed, while higher degrees of freedom result in a more symmetric shape.
17+
18+
## Example
19+
20+
The example below demonstrates how to generate random samples from a chi-square distribution and visualize them with a [histogram](https://www.codecademy.com/learn/statistics-histograms). In this demonstration, [SciPy](https://www.codecademy.com/resources/docs/scipy) is used to generate the samples and [Matplotlib](https://www.codecademy.com/resources/docs/matplotlib) is used for plotting:
21+
22+
```py
23+
import numpy as np
24+
import matplotlib.pyplot as plt
25+
from scipy.stats import chi2
26+
27+
# Set the degrees of freedom
28+
df = 4
29+
30+
# Generate 1,000 random samples from the chi-square distribution
31+
data = chi2.rvs(df, size=1000)
32+
33+
# Plot the histogram
34+
plt.hist(data, bins=30, density=True, alpha=0.6, color='skyblue', edgecolor='black')
35+
plt.title(f"Chi-Square Distribution (df={df})")
36+
plt.xlabel("Value")
37+
plt.ylabel("Density")
38+
plt.show()
39+
```
40+
41+
The above code generates a histogram illustrating the chi-square distribution:
42+
43+
![The output for the above example](https://raw.githubusercontent.com/Codecademy/docs/main/media/chi-square-distribution.png)

documentation/tags.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Calendar
5555
Catch
5656
Characters
5757
Charts
58+
Chi-Square
5859
Chatbots
5960
Cryptocurrency
6061
Classes
@@ -89,6 +90,7 @@ D3
8990
Deployment
9091
Dart
9192
Data
93+
Data Distributions
9294
Data Parallelism
9395
Data Structures
9496
Data Types

media/chi-square-distribution.png

179 KB
Loading

0 commit comments

Comments
 (0)