.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/LinearModels/LinearRegressionConfidenceIntervals.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_LinearModels_LinearRegressionConfidenceIntervals.py: Linear Regression with confidence intervals ============================================== This script performs linear regression on a synthetic dataset using the LinearRegression class from the DLL library. It also computes and visualizes the 95% confidence interval for the regression line. .. GENERATED FROM PYTHON SOURCE LINES 9-46 .. image-sg:: /auto_examples/LinearModels/images/sphx_glr_LinearRegressionConfidenceIntervals_001.png :alt: Linear Regression with 95% Confidence Interval :srcset: /auto_examples/LinearModels/images/sphx_glr_LinearRegressionConfidenceIntervals_001.png :class: sphx-glr-single-img .. code-block:: Python import torch import matplotlib.pyplot as plt from scipy.stats import t from DLL.MachineLearning.SupervisedLearning.LinearModels import LinearRegression x = torch.linspace(0, 10, 100) y = 2.5 * x + 3 + 5 * torch.randn(len(x)) x = x.unsqueeze(1) model = LinearRegression() model.fit(x, y) y_pred = model.predict(x) x = x.squeeze() n = len(x) dof = n - 2 s_err = torch.sqrt(torch.sum(model.residuals ** 2) / dof) t_crit = t.ppf(0.975, dof) x_mean = torch.mean(x) sum_sq_x = torch.sum((x - x_mean)**2) conf_margin = t_crit * s_err * torch.sqrt(1/n + (x - x_mean)**2 / sum_sq_x) y_upper = y_pred + conf_margin y_lower = y_pred - conf_margin plt.figure(figsize=(8, 8)) plt.scatter(x.numpy(), y.numpy(), s=10, label="Data", alpha=0.6) plt.plot(x.numpy(), y_pred.numpy(), color="red", label="Regression Line") plt.fill_between(x.numpy(), y_lower.numpy(), y_upper.numpy(), color="red", alpha=0.2, label="95% CI") plt.xlabel("X") plt.ylabel("Y") plt.title("Linear Regression with 95% Confidence Interval") plt.legend() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.086 seconds) .. _sphx_glr_download_auto_examples_LinearModels_LinearRegressionConfidenceIntervals.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: LinearRegressionConfidenceIntervals.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: LinearRegressionConfidenceIntervals.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: LinearRegressionConfidenceIntervals.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_