The Wayback Machine - https://web.archive.org/web/20241127021137/https://www.geeksforgeeks.org/sampling-distribution-using-python/
Open In App

Sampling distribution Using Python

Last Updated : 30 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

There are different types of distributions that we study in statistics like normal/gaussian distribution, exponential distribution, binomial distribution, and many others. We will study one such distribution today which is Sampling Distribution.

Let’s say we have some data then if we sample some finite number of data points from it and then calculate some statistical measure of it and let’s do this some n number of times. Then if we draw the distribution curve of those sample statistics then the distribution obtained is known as Sampling Distribution.

Sampling distribution Using Python

There is also a special case of the sampling distribution which is known as the Central Limit Theorem which says that if we take some samples from a distribution of data(no matter how it is distributed) then if we draw a distribution curve of the mean of those samples then it will be a normal distribution.

Let’s understand it by using an example:

Let’s take numbers from 1 to 10 and use them as our primary data.

Python
import numpy as np
num = np.arange(10)
num

Output:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Now, let’s sample two points from the data and take the average of these two. Also, let’s maintain a dictionary with the sample means and the number of times they appear.

Python
sample_freq = {}

for i in range(4):
    for j in range(4):
          # Selecting each pair possible with 
        # repetition
      
        mean_of_two = (num[i] + num[j]) / 2
        
        if (mean_of_two in sample_freq):
              # Updating the value for a mean value
            # if it already exists
            sample_freq[mean_of_two] += 1
            
        else:
              # Adding a new key to the dictionary
            # if it is not their
            sample_freq[mean_of_two] = 1

sample_freq

Output:

{1.0: 1, 1.5: 2, 2.0: 3, 2.5: 4, 3.0: 3, 3.5: 2, 4.0: 1}

Now, let’s plot the sample statistics to visualize its distribution.

Python
import matplotlib.pyplot as plt
plt.scatter(sample_freq.keys(), sample_freq.values())
plt.show()

Output:

Distribution of the sample statistic

Distribution of the sample statistic

From the above graph, we can observe that the distribution of the sample statistic is symmetric and if we will take infinite such points which are totally random then we’ll be able to observe that the distribution formed will be a normal/gaussian distribution.

There are some error measurements that are related to the sampling distributions:

Standard Error

Let’s say we have a sampling distribution that has been calculated using some sample statistics then the SE of that statistics is calculated by dividing the standard deviation of those statistics by the square root of the sample size.

[Tex]SE = \frac{\sigma}{\sqrt n}[/Tex]

[Tex]\sigma \rightarrow \;\text{Standard Deviation of the sample statistic.}[/Tex]

[Tex]n \rightarrow \;\text{Number of the samples taken}[/Tex]

Python
means = []

# getting all the mean values also
# taking account of number of times they occur
for key in sample_freq.keys():
    for _ in range(sample_freq[key]):
        means.append(key)

# Applying standard error formula
se = np.std(means)/np.sqrt(len(means))
print(f'Standard Error of the samples is {se}.')

Output:

Standard Error of the samples is 0.19764235376052372.


Previous Article
Next Article

Similar Reads

Negaitve Sampling Using word2vec
Word2Vec, developed by Tomas Mikolov and colleagues at Google, has revolutionized natural language processing by transforming words into meaningful vector representations. Among the key innovations that made Word2Vec both efficient and effective is the technique of negative sampling. This article delves into what negative sampling is, why it’s cruc
7 min read
Introduction to Thompson Sampling | Reinforcement Learning
Reinforcement Learning is a branch of Machine Learning, also called Online Learning. It is used to decide what action to take at t+1 based on data up to time t. This concept is used in Artificial Intelligence applications such as walking. A popular example of reinforcement learning is a chess engine. Here, the agent decides upon a series of moves d
4 min read
DLSS - Deep Learning Super Sampling
Super Sampling is also known as Super Sampling Anti Aliasing(SSAA) is a spatial anti-aliasing method i.e. a method to remove aliasing (jagged and pixelated edges also known as "jaggies") from a video, rendered images or another software that produces computer graphics. Aliasing is not often dealt-with in higher resolutions but if the user does not
4 min read
Non-Probability sampling
In the realm of studies and facts collection, sampling techniques play a pivotal position in acquiring representative data without the want to survey an entire population. While probability sampling strategies like simple random sampling and stratified sampling are famous for his or her statistical rigor, non-possibility sampling techniques also ha
10 min read
Cluster Sampling
Sampling is a technique mostly used in data analysis and research. It is a technique in which we select a small part of the entire population to find out insights and draw conclusions about the whole population. Sampling can be done in many ways, and one of the common types of sampling is Clustered Sampling. In this article, we will see cluster sam
12 min read
Probability sampling
When conducting research and analyzing data in order to draw broad conclusions, the quality of inferences is crucial. Only the efficient statistical method known as probability sampling can achieve this. Probability sampling assures that every person or component of a population has an equal chance to be included in a sample that is randomly select
11 min read
Simple Random Sampling
It is frequently impractical or even impossible to gather data from the full population in the subject of statistics. Under such circumstances, researchers resort to sampling techniques, simple random sampling being one of the most basic. Using simple random sampling, a subset of people or things is chosen from a wider population, giving every pers
15 min read
What is Gibbs Sampling?
In statistics and machine learning, Gibbs Sampling is a potent Markov Chain Monte Carlo (MCMC) technique that is frequently utilized for sampling from intricate, high-dimensional probability distributions. The foundational ideas, mathematical formulas, and algorithm of Gibbs Sampling are examined in this article. Gibbs SamplingGibbs sampling is a t
11 min read
Stratified Random sampling - An Overview
Stratified Random Sampling is a technique used in Machine Learning and Data Science to select random samples from a large population for training and test datasets. When the population is not large enough, random sampling can introduce bias and sampling errors. Stratified Random Sampling ensures that the samples adequately represent the entire popu
15 min read
How to Implement Stratified Sampling with Scikit-Learn
In this article, we will learn about How to Implement Stratified Sampling with Scikit-Learn. What is Stratified sampling?Stratified sampling is a sampling technique in which the population is subdivided into groups based on specific characteristics relevant to the problem before sampling. The samples are drawn from this group with ample sizes propo
7 min read
Cluster Sampling in R
Cluster sampling, a widely used technique in statistics and data analysis, offers a practical solution for sampling from large populations. By dividing the population into clusters and selecting a subset of these clusters for analysis, researchers can efficiently collect representative samples while reducing logistical challenges. Here we delve int
7 min read
Stratified Sampling in Machine Learning
Machine learning can be a challenge when data isn't balanced. Stratified sampling is a technique that ensures all the important groups within your data are fairly represented. In this tutorial, we will understand what is stratified sampling and how it is crucial that it leads to superior machine learning models. What is Stratified Sampling?Stratifi
5 min read
What is Data Sampling - Types, Importance, Best Practices
Data sampling is a fundamental statistical method used in various fields to extract meaningful insights from large datasets. By analyzing a subset of data, researchers can draw conclusions about the entire population with accuracy and efficiency. This article will explore the concept of data sampling, its importance, techniques, process, advantages
9 min read
Image Sampling vs Quantization
In digital image processing, two fundamental concepts are image sampling and quantization. These processes are crucial for converting an analog image into a digital form that can be stored, manipulated, and displayed by computers. Despite being closely related, sampling and quantization serve distinct purposes and involve different techniques. This
4 min read
Different Types of Data Sampling Methods and Techniques
Data sampling is a statistical method that involves selecting a part of a population of data to create representative samples. The fundamental aim is to draw conclusions about the entire population without having to engage with every individual data point, thus saving time, resources, and effort while still achieving accurate results. In this guide
14 min read
Sampling Distributions in Data Science
Sampling distributions are like the building blocks of statistics. Exploring sampling distributions gives us valuable insights into the data's meaning and the confidence level in our findings. In this, article we will explore more about sampling distributions. Table of Content What is Sampling distributions?Importance Sampling Distribution in Data
9 min read
Inverse Gamma Distribution in Python
Inverse Gamma distribution is a continuous probability distribution with two parameters on the positive real line. It is the reciprocate distribution of a variable distributed according to the gamma distribution. It is very useful in Bayesian statistics as the marginal distribution for the unknown variance of a normal distribution. It is used for c
2 min read
Python - Student’s t Distribution in Statistics
We know the mathematics behind t-distribution. However, we can also use Python to implement t-distribution on a dataset. Python provides a unique package scipy for various statical techniques and methods. We will use this package for t-distribution implementation. prerequisite: t-distribution What is t-Distribution The t-distribution, also known as
5 min read
Map Data to a Normal Distribution in Scikit Learn
A Normal Distribution, also known as a Gaussian distribution, is a continuous probability distribution that is symmetrical around its mean. It is defined by its norm, which is the center of the distribution, and its standard deviation, which is a measure of the spread of the distribution. The normal distribution is often used to model continuous an
5 min read
NLP | Storing Conditional Frequency Distribution in Redis
The nltk.probability.ConditionalFreqDist class is a container for FreqDist instances, with one FreqDist per condition. It is used to count frequencies that are dependent on another condition, such as another word or a class label. It is being used here to create an API-compatible class on top of Redis using the RedisHashFreqDist . In the code given
2 min read
NLP | Storing Frequency Distribution in Redis
The nltk.probability.FreqDist class is used in many classes throughout NLTK for storing and managing frequency distributions. It's quite useful, but it's all in-memory, and doesn't provide a way to persist the data. A single FreqDist is also not accessible to multiple processes. All that can be changed by building a FreqDist on top of Redis. What i
3 min read
Seaborn | Distribution Plots
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. This article deals with the distribution plots in seaborn which is used for examining univariate and bivariate distributions. In this article we will be discussing 4 types of distributio
3 min read
Converting Power Law Distribution to a Linear graph
Whenever we are working on ML projects, we have to deal with the high dimensionality of the dataset. There is a very particular term to refer to here i.e. Curse of dimensionality. Although there are many ways to handle this problem one of the solutions can be to change the coordinate systems. This solution is somewhat interesting and new from a dat
2 min read
Exploring Data Distribution | Set 1
Whenever we work in data science and machine learning, our approach of handling the data and finding something useful out of it is based on the distribution of the data. Distribution means that how data can be present in different possible ways, the percentage of specific data, identifying the outliers. So, data distribution is the way of using gra
2 min read
Exploring Data Distribution | Set 2
Prerequisite: Exploring Data Distribution | Set 1Terms related to Exploration of Data Distribution -> Boxplot -> Frequency Table -> Histogram -> Density Plot Loading Libraries C/C++ Code import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt Loading Data C/C++ Code data = pd.read_csv("../data/s
2 min read
Plot Cumulative Distribution Function in R
In this article, we will discuss how to plot a cumulative distribution function (CDF) in the R Programming Language. The cumulative distribution function (CDF) of a random variable evaluated at x, is the probability that x will take a value less than or equal to x. To calculate the cumulative distribution function in the R Language, we use the ecdf
4 min read
Species Distribution Modeling in Scikit Learn
Species Distribution Modeling (SDM) is a crucial tool in conservation biology, ecology, and related fields. It involves predicting the geographic distribution of species based on environmental variables and species occurrence data. This article explores how to implement SDM using Scikit-Learn, a popular machine learning library in Python. Table of
5 min read
How to create a plot of cumulative distribution function in R?
Empirical distribution is a non-parametric method used to estimate the cumulative distribution function (CDF) of a random variable. It is particularly useful when you have data and want to make inferences about the population distribution without making any assumptions about its form. In this article, we will discuss how to create and visualize emp
4 min read
How Do I Fit a Set of Data to a Pareto Distribution in R?
Fitting a set of data to a Pareto distribution can be useful in various fields such as economics, finance, and social sciences where the Pareto principle (also known as the 80-20 rule) often applies. The Pareto distribution is a power-law probability distribution that is used to describe a phenomenon where a small number of events or items account
5 min read
How to Test if My Distribution is Multimodal in R?
Determining whether a distribution is multimodal (having multiple peaks) is an important aspect of data analysis. In many real-world scenarios, data distributions are not always unimodal. Identifying multimodal distributions can provide insights into the underlying data structure and can be crucial for further analysis and decision-making. This art
4 min read
Practice Tags :
three90RightbarBannerImg