Created
May 18, 2021 13:57
-
-
Save ngbala6/bb45612894f8ab54c5eda00dc2856255 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 - Standard Deviation\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Import Packages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import statistics\n", | |
| "import math" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Sample Data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "data = {4, 6, 9, 3, 7}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Population Standard Deviation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "##### Standard Deviation Calculation using Formula\n", | |
| "\n", | |
| "<img src=\"https://cdn-images-1.medium.com/max/800/1*ZIklcOp7x2ntL33eg0530Q.png\" width=\"45000\" height=\"50000\">\n", | |
| "\n", | |
| "By Applying this formula in the code, we will get Population Standard Deviation\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2.1354156504062622\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def popstddev(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", | |
| " # stddev\n", | |
| " stddev = math.sqrt(sum(deviations) / (n))\n", | |
| " return stddev\n", | |
| "\n", | |
| "print(popstddev(data))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "##### Population Standard Deviation Calculation using Python Packages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.1354156504062622" | |
| ] | |
| }, | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "statistics.pstdev(data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Sample Standard Deviation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Sample Standard Deviation Calculation using Formula\n", | |
| "\n", | |
| "<img src=\"https://cdn-images-1.medium.com/max/800/1*7hs4AnYrvJc9rmqNLEcWQA.png\" width=\"45000\" height=\"50000\">\n", | |
| "\n", | |
| "By Applying this formula in the code, we will get Sample Standard Deviation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2.3874672772626644\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def samplestddev(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", | |
| " # stddev\n", | |
| " stddev = math.sqrt(sum(deviations) / (n - 1))\n", | |
| " return stddev\n", | |
| "\n", | |
| "print(samplestddev(data))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Sample Standard Deviation Calculation using Statistics Python Package" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.3874672772626644" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "statistics.stdev(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