# The Chi-squared Test
>[!Question] Is the variation in your data just due to chance? Or does it have to do with one of the variables?
>[!Abstract] The Chi-squared Test
> Assess the **goodness of fit**
> Calculated as follows:
> $\chi^2 = \sum_i \frac{(O_i - E_i)^2}{E_i}$
> Where $O_i$ is the observed value of interest and $E_i$ is the expected value.
>
>**Used to check if two categorical variables are independent**
## Computation
### Chi-squared Test
```python
from scipy.stats import chisquare
chisquare(f_obs, ddof=1, f_exp=None, axis=1)>)
```
### Testing for Independence
```python
from scipy.stats import chi2_contingency
tab_data = [[203, 150, 190, 305], [195, 170, 250, 400]]
chi2_contingency(tab_data)
```
## Use when
- Testing the goodness of fit
- To check if two categorical variables are independent