Created
May 18, 2021 13:42
-
-
Save ngbala6/0f77d5026cc65887a1b1351ae5da643d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Measure of Variability - Variance\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Import Packages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import statistics" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Sample Data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "data = {4, 6, 9, 3, 7}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Population Variance" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "##### Variance Calculation using Formula\n", | |
| "\n", | |
| "<img src=\"https://cdn-images-1.medium.com/max/800/1*N_yz5MPvRDBbzRZOTepnzQ.png\" width=\"45000\" height=\"50000\">\n", | |
| "\n", | |
| "By Applying this formula in the code, we will get Population Variance\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "4.5600000000000005\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def variance(data):\n", | |
| " # Number of observations\n", | |
| " n = len(data)\n", | |
| " # Mean of the data\n", | |
| " mean = sum(data) / n\n", | |
| " # Square deviations\n", | |
| " deviations = [(x - mean) ** 2 for x in data]\n", | |
| " # Variance\n", | |
| " variance = sum(deviations) / (n)\n", | |
| " return variance\n", | |
| "\n", | |
| "print(variance(data))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "##### Population Variance Calculation using Python Packages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "4.5600000000000005" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "statistics.pvariance(data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Sample Variance" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Sample Variance Calculation using Formula\n", | |
| "\n", | |
| "<img src=\"https://cdn-images-1.medium.com/max/800/1*J8_kemaD_2Wb2EOY7YR-1Q.png\" width=\"45000\" height=\"50000\">\n", | |
| "\n", | |
| "By Applying this formula in the code, we will get Sample Variance" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "5.7\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def variance(data):\n", | |
| " # Number of observations\n", | |
| " n = len(data)\n", | |
| " # Mean of the data\n", | |
| " mean = sum(data) / n\n", | |
| " # Square deviations\n", | |
| " deviations = [(x - mean) ** 2 for x in data]\n", | |
| " # Variance\n", | |
| " variance = sum(deviations) / (n - 1)\n", | |
| " return variance\n", | |
| "\n", | |
| "print(variance(data))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Sample Variance Calculation using Statistics Python Package" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "5.7" | |
| ] | |
| }, | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "statistics.variance(data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.7.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment