help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
scipy-scientific
/
notebooks
RSK World
scipy-scientific
Scientific Computing with SciPy
notebooks
  • 01_optimization.ipynb19.5 KB
  • 02_integration.ipynb7.4 KB
  • 03_interpolation.ipynb8.8 KB
  • 04_statistics.ipynb8.8 KB
  • 05_signal_processing.ipynb16.6 KB
04_statistics.ipynb
notebooks/04_statistics.ipynb
Raw Download
Find: Go to:
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SciPy Statistical Functions\n",
    "\n",
    "<!--\n",
    "Author: RSK World\n",
    "Website: https://rskworld.in\n",
    "Email: help@rskworld.in\n",
    "Phone: +91 93305 39277\n",
    "-->\n",
    "\n",
    "This notebook demonstrates statistical functions with SciPy.\n",
    "\n",
    "## Topics Covered:\n",
    "1. Probability Distributions\n",
    "2. Descriptive Statistics\n",
    "3. Hypothesis Testing\n",
    "4. Confidence Intervals\n",
    "5. Correlation and Regression\n",
    "6. Non-parametric Tests\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Author: RSK World - https://rskworld.in\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "from scipy.stats import norm, t, chi2\n",
    "\n",
    "print(\"SciPy Statistical Functions Examples\")\n",
    "print(\"Author: RSK World - https://rskworld.in\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Probability Distributions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Normal distribution\n",
    "mu, sigma = 0, 1\n",
    "x = np.linspace(-4, 4, 100)\n",
    "y_norm = norm.pdf(x, mu, sigma)\n",
    "\n",
    "# t-distribution\n",
    "df = 5\n",
    "y_t = t.pdf(x, df)\n",
    "\n",
    "# Chi-square distribution\n",
    "x_chi2 = np.linspace(0, 10, 100)\n",
    "df_chi2 = 3\n",
    "y_chi2 = chi2.pdf(x_chi2, df_chi2)\n",
    "\n",
    "print(f\"Normal distribution: μ={mu}, σ={sigma}\")\n",
    "print(f\"t-distribution: df={df}\")\n",
    "print(f\"Chi-square distribution: df={df_chi2}\")\n",
    "\n",
    "# Visualize\n",
    "fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n",
    "\n",
    "axes[0].plot(x, y_norm, 'b-', linewidth=2, label=f'Normal(μ={mu}, σ={sigma})')\n",
    "axes[0].fill_between(x, 0, y_norm, alpha=0.3)\n",
    "axes[0].set_xlabel('x', fontsize=12)\n",
    "axes[0].set_ylabel('PDF', fontsize=12)\n",
    "axes[0].set_title('Normal Distribution', fontsize=12, fontweight='bold')\n",
    "axes[0].legend()\n",
    "axes[0].grid(True, alpha=0.3)\n",
    "\n",
    "axes[1].plot(x, y_t, 'r-', linewidth=2, label=f't-distribution (df={df})')\n",
    "axes[1].plot(x, y_norm, 'b--', linewidth=1.5, alpha=0.7, label='Normal')\n",
    "axes[1].fill_between(x, 0, y_t, alpha=0.3, color='red')\n",
    "axes[1].set_xlabel('x', fontsize=12)\n",
    "axes[1].set_ylabel('PDF', fontsize=12)\n",
    "axes[1].set_title('t-Distribution', fontsize=12, fontweight='bold')\n",
    "axes[1].legend()\n",
    "axes[1].grid(True, alpha=0.3)\n",
    "\n",
    "axes[2].plot(x_chi2, y_chi2, 'g-', linewidth=2, label=f'Chi² (df={df_chi2})')\n",
    "axes[2].fill_between(x_chi2, 0, y_chi2, alpha=0.3)\n",
    "axes[2].set_xlabel('x', fontsize=12)\n",
    "axes[2].set_ylabel('PDF', fontsize=12)\n",
    "axes[2].set_title('Chi-Square Distribution', fontsize=12, fontweight='bold')\n",
    "axes[2].legend()\n",
    "axes[2].grid(True, alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Descriptive Statistics\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate sample data\n",
    "np.random.seed(42)\n",
    "data = np.random.normal(100, 15, 1000)\n",
    "\n",
    "# Calculate statistics\n",
    "mean = np.mean(data)\n",
    "median = np.median(data)\n",
    "std = np.std(data)\n",
    "var = np.var(data)\n",
    "skew = stats.skew(data)\n",
    "kurtosis = stats.kurtosis(data)\n",
    "\n",
    "print(f\"Sample size: {len(data)}\")\n",
    "print(f\"Mean: {mean:.4f}\")\n",
    "print(f\"Median: {median:.4f}\")\n",
    "print(f\"Standard deviation: {std:.4f}\")\n",
    "print(f\"Variance: {var:.4f}\")\n",
    "print(f\"Skewness: {skew:.4f}\")\n",
    "print(f\"Kurtosis: {kurtosis:.4f}\")\n",
    "\n",
    "# Visualize\n",
    "fig, axes = plt.subplots(1, 2, figsize=(14, 5))\n",
    "\n",
    "# Histogram\n",
    "axes[0].hist(data, bins=30, density=True, alpha=0.7, color='blue', edgecolor='black')\n",
    "x_fit = np.linspace(data.min(), data.max(), 100)\n",
    "axes[0].plot(x_fit, norm.pdf(x_fit, mean, std), 'r-', linewidth=2, label='Normal fit')\n",
    "axes[0].axvline(mean, color='red', linestyle='--', linewidth=2, label=f'Mean={mean:.2f}')\n",
    "axes[0].axvline(median, color='green', linestyle='--', linewidth=2, label=f'Median={median:.2f}')\n",
    "axes[0].set_xlabel('Value', fontsize=12)\n",
    "axes[0].set_ylabel('Density', fontsize=12)\n",
    "axes[0].set_title('Data Distribution', fontsize=12, fontweight='bold')\n",
    "axes[0].legend()\n",
    "axes[0].grid(True, alpha=0.3)\n",
    "\n",
    "# Box plot\n",
    "axes[1].boxplot(data, vert=True, patch_artist=True)\n",
    "axes[1].set_ylabel('Value', fontsize=12)\n",
    "axes[1].set_title('Box Plot', fontsize=12, fontweight='bold')\n",
    "axes[1].grid(True, alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate two samples\n",
    "np.random.seed(42)\n",
    "sample1 = np.random.normal(100, 15, 100)\n",
    "sample2 = np.random.normal(105, 15, 100)\n",
    "\n",
    "# One-sample t-test (test if mean = 100)\n",
    "t_stat1, p_value1 = stats.ttest_1samp(sample1, 100)\n",
    "\n",
    "# Two-sample t-test (test if means are equal)\n",
    "t_stat2, p_value2 = stats.ttest_ind(sample1, sample2)\n",
    "\n",
    "print(f\"One-sample t-test (H0: μ = 100):\")\n",
    "print(f\"  t-statistic: {t_stat1:.4f}, p-value: {p_value1:.4f}\")\n",
    "print(f\"  Result: {'Reject H0' if p_value1 < 0.05 else 'Fail to reject H0'}\")\n",
    "\n",
    "print(f\"\\nTwo-sample t-test (H0: μ1 = μ2):\")\n",
    "print(f\"  t-statistic: {t_stat2:.4f}, p-value: {p_value2:.4f}\")\n",
    "print(f\"  Result: {'Reject H0' if p_value2 < 0.05 else 'Fail to reject H0'}\")\n",
    "\n",
    "# Visualize\n",
    "plt.figure(figsize=(12, 5))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.hist(sample1, bins=20, alpha=0.7, label=f'Sample 1 (μ={np.mean(sample1):.2f})', color='blue')\n",
    "plt.hist(sample2, bins=20, alpha=0.7, label=f'Sample 2 (μ={np.mean(sample2):.2f})', color='red')\n",
    "plt.xlabel('Value', fontsize=12)\n",
    "plt.ylabel('Frequency', fontsize=12)\n",
    "plt.title('Sample Distributions', fontsize=12, fontweight='bold')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.boxplot([sample1, sample2], labels=['Sample 1', 'Sample 2'])\n",
    "plt.ylabel('Value', fontsize=12)\n",
    "plt.title('Box Plot Comparison', fontsize=12, fontweight='bold')\n",
    "plt.grid(True, alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Correlation and Regression\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate correlated data\n",
    "np.random.seed(42)\n",
    "x = np.linspace(0, 10, 50)\n",
    "y_true = 2 * x + 3\n",
    "y = y_true + np.random.normal(0, 2, len(x))\n",
    "\n",
    "# Calculate correlation\n",
    "correlation, p_value = stats.pearsonr(x, y)\n",
    "\n",
    "# Linear regression\n",
    "slope, intercept, r_value, p_value_reg, std_err = stats.linregress(x, y)\n",
    "\n",
    "# Predictions\n",
    "y_pred = slope * x + intercept\n",
    "\n",
    "print(f\"Correlation coefficient: {correlation:.4f}\")\n",
    "print(f\"P-value (correlation): {p_value:.4f}\")\n",
    "print(f\"\\nLinear regression:\")\n",
    "print(f\"  Slope: {slope:.4f}\")\n",
    "print(f\"  Intercept: {intercept:.4f}\")\n",
    "print(f\"  R²: {r_value**2:.4f}\")\n",
    "print(f\"  P-value: {p_value_reg:.4f}\")\n",
    "print(f\"  Standard error: {std_err:.4f}\")\n",
    "\n",
    "# Visualize\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(x, y, alpha=0.6, color='blue', label='Data points')\n",
    "plt.plot(x, y_true, 'k--', linewidth=2, alpha=0.7, label='True relationship')\n",
    "plt.plot(x, y_pred, 'r-', linewidth=2, label=f'Regression line (R²={r_value**2:.3f})')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('y', fontsize=12)\n",
    "plt.title('Linear Regression', fontsize=14, fontweight='bold')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
276 lines•8.8 KB
json

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer