A Swift Introduction To Probability Theory With Python | by Emmett Boudreau | Towards Data Science

Probability theory is a sect of mathematics that focuses on the interpretations of the likelihood of certain outcomes. The mathematical branch uses axioms to formally measure probability in probable space. Probable space is a name given to sample space that is used to determine likelihood. Given the definition, it is easy to see why probability theory takes a strong hold in experiments and predicting the outcomes of those experiments. Using data and probable space, we can make informed decisions using data based on prior results. Just as well, we can determine analytical correlations that can provide valuable information about the world we live in.
Needless to say, this makes this branch of mathematics pretty powerful. Not only can we use these mathematics to make informed decisions about the world around us and how to improve it, but we can also use it to predict the impact of those decisions. This in turn makes us even more informed about the potential outcome of the decisions made from these analytics. Now that we understand the power of probability theory, let's dive into probable space, distributions, and how we can use them in the Python programming language.
A probability distribution is a function that gives the likelihood of different possible outcomes for an experiment. There are a lot of different statistical distributions that are used in probability theory, but the most popular two are probably the T distribution and the Z distribution, also known as the normal distribution. There are two different types of distributions that are used to predict two different types of features.
The first type of feature to consider is discrete features. These are features that represent some sort of category or possibility that is non-numerical. For example, we roll a dice and we have a potential of the dice roll being classified as one, two, three, four, five, or six. The other type of distribution is a continuous probability distribution. These are used to gauge sample space for continuous values. Continuous values are just features that have a representation in numerical space, for example, a measurement of different weights across a population of observed humans. Another thing worth mentioning about distributions is that they have two functions that are used for creating statistical inference, Cumulative Distribution Functions (CDFs) and Probability Density Functions (PDFs.) While I do not have an article that goes into depth about CDFs, I do have one on PDFs that touches a moderate bit on what a CDF is. As such, I will only be reviewing that information briefly here, so if you would like to read a bit more on these two functions, you may here:
towardsdatascience.com
The basis for all continuous sample space in probability theory is the normal distribution. The normal distribution has a simple PDF that utilizes three important values from a continuous feature:
μ — Sample mean.
σ — Sample standard deviation.
n — Number of observations in the sample.
Using these simple attributes of the feature, this PDF will show the number of standard deviations away from the mean a given value is. The formula can be written in Python as follows:
x = [5, 10, 15]N = len(x)               σ = std(x)μ = mean(x)normally_distributed = [i = (i-μ) / σ for i in x]
However, you can also use the NumPy or SciPy implementation:
import numpy as npimport scipy.stats as scsnormal = np.normal(x)normal = scs.norm.pdf(x)
The normal distribution is perfectly capable of being used for testing, however most scientists opt to use another probability distribution. Why is this? The most important part of a distribution that goes towards statistical significance is the tails of a distribution. The tails of a distribution are usually represented in length with an alpha value, and represent sample space where very few samples reside. An alpha value is a percentage value that is created by subtracting the level of confidence we need in our test by one. The most popular alpha value used to prove hypothesis is .05, which means that typically science is done with 95-percent confidence. Personally, I prefer to shoot for a 97-percent confidence level in order to really solidify the validity of a hypothesis before accepting it. Now that we understand tails, we can consider why the normal distribution is not often used for testing.
The normal distribution has rather small tails with a steep cutoff, leaving very little room inside of the tails for samples to actually fall. In other words, it is much less likely that normally distributed data is going to fall within the area of our alpha because the tails are smaller. This is because the normal distribution is a regular probability distribution, whereas the T distribution is an equivalent possibility distribution. Let’s take a look at the T distribution’s PDF function from SciPy.stats. In order to use the T distribution, we will first need to get our degrees of freedom. This is usually represented with v, and can be calculated by subtracting the length of the samples by one.
v = len(x) - 1t_data = scs.t.pdf(x, v)
Probability theory has become one of the most important branches of mathematics for applied sciences. This is because using this math we can both analyze data returned from experiments, as well as actually perform experiments with collected data in order to gain insights. As humans with access to a lot of technology, it can be pretty easy to think that we have discovered everything there is to know. However, this is simply not the case, and using a statistical testing regimen you can prove your ideas with mathematical confidence! That is what makes these skills so powerful! Thank you very much for reading my article, I appreciate it.