TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Chi-Square Test

Probability and Statistics
Medium

Given an r×cr\times cr×c contingency table of observed counts OOO, compute each expected count under independence:

Eij=RiCjNE_{ij} = \frac{R_iC_j}{N}Eij​=NRi​Cj​​

Then compute the chi-square statistic:

χ2=∑i=1r∑j=1c(Oij−Eij)2Eij\chi^2 = \sum_{i=1}^{r}\sum_{j=1}^{c}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}χ2=i=1∑r​j=1∑c​Eij​(Oij​−Eij​)2​

Here, RiR_iRi​ is row iii's total, CjC_jCj​ is column jjj's total, and NNN is the grand total. Return chi2 as a Python float and expected as a NumPy array in a dictionary.

Loading visualization...

Examples

Input: C = [[10, 20], [20, 10]]

Output: {"chi2": 6.666667, "expected": [[15.0, 15.0], [15.0, 15.0]]}

Explanation: Equal row and column totals produce expected counts of 15 in every cell.

Input: C = [[20, 30], [40, 60]]

Output: {"chi2": 0.0, "expected": [[20.0, 30.0], [40.0, 60.0]]}

Input: C = [[25, 25], [25, 25]]

Output: {"chi2": 0.0, "expected": [[25.0, 25.0], [25.0, 25.0]]}

Hint 1

Use np.outer(row_totals, column_totals) / total for expected counts.

Hint 2

Sum (C - expected) ** 2 / expected across the complete table.

Requirements

  • Compute row totals, column totals, and the grand total
  • Build the expected table with an outer product
  • Return exactly chi2 and expected in a dictionary
  • expected must be a NumPy array

Constraints

  • C is a nonempty two-dimensional table of positive counts
  • Every expected frequency is positive
  • Use NumPy only
Try Similar Problems
T Test One SampleBootstrap MeanNaive Bayes BernoulliBernoulli PmfExpected Value Discrete

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.
PrevNext

Chi-Square Test

Probability and Statistics
Medium

Given an r×cr\times cr×c contingency table of observed counts OOO, compute each expected count under independence:

Eij=RiCjNE_{ij} = \frac{R_iC_j}{N}Eij​=NRi​Cj​​

Then compute the chi-square statistic:

χ2=∑i=1r∑j=1c(Oij−Eij)2Eij\chi^2 = \sum_{i=1}^{r}\sum_{j=1}^{c}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}χ2=i=1∑r​j=1∑c​Eij​(Oij​−Eij​)2​

Here, RiR_iRi​ is row iii's total, CjC_jCj​ is column jjj's total, and NNN is the grand total. Return chi2 as a Python float and expected as a NumPy array in a dictionary.

Loading visualization...

Examples

Input: C = [[10, 20], [20, 10]]

Output: {"chi2": 6.666667, "expected": [[15.0, 15.0], [15.0, 15.0]]}

Explanation: Equal row and column totals produce expected counts of 15 in every cell.

Input: C = [[20, 30], [40, 60]]

Output: {"chi2": 0.0, "expected": [[20.0, 30.0], [40.0, 60.0]]}

Input: C = [[25, 25], [25, 25]]

Output: {"chi2": 0.0, "expected": [[25.0, 25.0], [25.0, 25.0]]}

Hint 1

Use np.outer(row_totals, column_totals) / total for expected counts.

Hint 2

Sum (C - expected) ** 2 / expected across the complete table.

Requirements

  • Compute row totals, column totals, and the grand total
  • Build the expected table with an outer product
  • Return exactly chi2 and expected in a dictionary
  • expected must be a NumPy array

Constraints

  • C is a nonempty two-dimensional table of positive counts
  • Every expected frequency is positive
  • Use NumPy only
Try Similar Problems
T Test One SampleBootstrap MeanNaive Bayes BernoulliBernoulli PmfExpected Value Discrete

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.