diff --git a/your_code/main.ipynb b/your_code/main.ipynb index 7810ccf..c15ea64 100644 --- a/your_code/main.ipynb +++ b/your_code/main.ipynb @@ -14,11 +14,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fail to reject the null hypothesis. The data follows a Poisson distribution.\n" + ] + } + ], "source": [ - "# your answer here" + "import numpy as np\n", + "from scipy.stats import poisson, chi2\n", + "\n", + "observed_frequencies = np.array([35, 99, 104, 110, 62, 25, 10, 3])\n", + "total_times = 448\n", + "mean_parameter = np.sum(np.arange(len(observed_frequencies)) * observed_frequencies) / total_times\n", + "expected_frequencies = poisson.pmf(np.arange(len(observed_frequencies)), mean_parameter) * total_times\n", + "chi_squared_statistic = np.sum((observed_frequencies - expected_frequencies)**2 / expected_frequencies)\n", + "degrees_of_freedom = len(observed_frequencies) - 1\n", + "critical_value = chi2.ppf(0.95, degrees_of_freedom)\n", + "\n", + "\n", + "if chi_squared_statistic > critical_value:\n", + " print(\"Reject the null hypothesis. The data does not follow a Poisson distribution.\")\n", + "else:\n", + " print(\"Fail to reject the null hypothesis. The data follows a Poisson distribution.\")" ] }, { @@ -60,11 +83,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reject the null hypothesis. The data does not fit a binomial distribution.\n" + ] + } + ], "source": [ - "# your answer here" + "observed_frequencies = np.array([138, 53, 9])\n", + "defective_item_counts = np.array([0, 1, 2])\n", + "\n", + "\n", + "total_samples = np.sum(observed_frequencies)\n", + "p = 0.05\n", + "expected_frequencies = np.array([total_samples * (1 - p)**2, total_samples * 2 * p * (1 - p), total_samples * p**2])\n", + "chi_squared_statistic = np.sum((observed_frequencies - expected_frequencies)**2 / expected_frequencies)\n", + "degrees_of_freedom = len(observed_frequencies) - 1\n", + "critical_value = chi2.ppf(0.95, degrees_of_freedom)\n", + "\n", + "\n", + "if chi_squared_statistic > critical_value:\n", + " print(\"Reject the null hypothesis. The data does not fit a binomial distribution.\")\n", + "else:\n", + " print(\"Fail to reject the null hypothesis. The data fits a binomial distribution.\")" ] }, { @@ -79,17 +125,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reject the null hypothesis. There is a significant association between patterns of physical activity and sugary drink consumption.\n" + ] + } + ], "source": [ - "#your answer here" + "from scipy.stats import chi2_contingency\n", + "\n", + "\n", + "observed_frequencies = np.array([[32, 14, 6],\n", + " [12, 22, 9]])\n", + "chi2, p, dof, expected = chi2_contingency(observed_frequencies)\n", + "alpha = 0.05\n", + "if p < alpha:\n", + " print(\"Reject the null hypothesis. There is a significant association between patterns of physical activity and sugary drink consumption.\")\n", + "else:\n", + " print(\"Fail to reject the null hypothesis. There is no significant association between patterns of physical activity and sugary drink consumption.\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -103,7 +167,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.9" } }, "nbformat": 4,