top of page

25% Discount For All Pricing Plans "welcome"

Introduction to Sampling



Overview of Sampling

Sampling is the act of selecting a subset of individuals from within a statistical population to estimate characteristics of the whole population. Here's a deeper look into the concept.

  • Definition of sampling: Imagine you want to know the average height of all the people in your city. Measuring everyone would be time-consuming and impractical. Sampling would mean measuring a small, randomly chosen group and using those measurements to estimate the entire population.

  • Importance and use cases: From market research to political polling, sampling is everywhere. It's the backbone of any statistical analysis and prediction.

History and Concepts

  • Pierre-Simon Laplace's contribution in 1786: Known as a brilliant mathematician, Laplace's work on sampling laid the groundwork for modern statistics.

  • Population vs. sample: Population refers to the entire group you want to study, while a sample is a smaller subset chosen from that population.


Examples and Techniques


Counting the population in France


In 1786, Laplace used a sample to estimate the population of France. Think of it like tasting a spoonful of soup to judge the whole pot.


Coffee rating dataset


Imagine you want to know the average rating of a particular brand of coffee. You could sample 100 cups from various cities and use that data to infer the overall rating.


Sampling in Python using pandas

import pandas as pd

# Creating a sample data frame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40]}
df = pd.DataFrame(data)

# Sampling 2 random rows
sample = df.sample(n=2)
print(sample)

Output:

      Name  Age
2  Charlie   35
0    Alice   25


Point Estimates


Population Parameters & Point Estimates


Point estimates are used to infer a population parameter like mean, median, or mode.

  • Definition of population parameter and point estimate: The population parameter is a numerical value that accurately represents the entire population. Point estimates are statistics derived from sample data used to estimate these parameters.

  • Calculating mean with NumPy and pandas:

import numpy as np

ages = [25, 30, 35, 40]
mean_age = np.mean(ages)

print("Mean Age:", mean_age)

Output:

Mean Age: 32.5


Points vs. Flavor Analysis


We can analyze the relationship between cup points and flavor in coffee rating data.

import matplotlib.pyplot as plt

# Simulated data
points = [90, 80, 85, 95]
flavor = [7, 6, 7.5, 8]

plt.scatter(points, flavor)
plt.xlabel('Points')
plt.ylabel('Flavor')
plt.show()

This code snippet will create a scatter plot showing the relationship between points and flavor.


Python Sampling for Series


In this part, we will explore how to work with pandas Series and use the sample method to draw random values.

  • How to sample pandas Series: Sampling from a pandas Series is as straightforward as sampling from a DataFrame. Here's how you can do it: import pandas as pd # Creating a pandas Series s = pd.Series([10, 20, 30, 40, 50]) # Sampling 3 random values sample_series = s.sample(n=3) print(sample_series) Output: 4 50 1 20 0 10

  • Using n argument to specify random values: The n argument allows you to specify the number of random samples you wish to draw from the Series.


Convenience Sampling and Bias


The Importance of Sample Representation


Here we explore how sample representation can impact the results and create bias.

  • The Literary Digest's failed election prediction in 1936: The Literary Digest's failure in predicting the U.S. presidential election of 1936 is a classic example of sampling bias. They sampled from a list of car owners and telephone users, ignoring those who couldn't afford these luxuries during the Great Depression.

  • Introduction to convenience sampling and its drawbacks: Convenience sampling means selecting what is easiest to reach. Think of it like picking apples from the lowest branches; you might miss the best ones at the top.

Practical Examples of Bias

  • Mean age estimation at Disneyland Paris: If you were to estimate the average age of visitors at Disneyland by sampling only during school holidays, the mean age might be skewed lower because of the higher presence of children.

  • Convenience sampling in coffee ratings: If you sample coffee ratings only from five-star hotels, you might miss the preferences of customers in casual coffee shops, leading to a biased estimate.


Visualizing Selection Bias


We can use histograms to visualize bias by comparing random sampling and convenience sampling distributions.

import matplotlib.pyplot as plt

# Simulated random and convenience sampling data
random_sample = [25, 30, 35, 40, 45]
convenience_sample = [40, 45, 45, 50, 50]

plt.hist([random_sample, convenience_sample], label=['Random', 'Convenience'])
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.legend()
plt.show()

This code snippet will create a histogram comparing random and convenience samples, illustrating how convenience sampling can introduce bias.


Pseudo-random Number Generation


Understanding Randomness

  • Definition and meanings of random: Randomness is often associated with unpredictability. In a truly random sequence, the next outcome is entirely independent of previous ones. Flipping a fair coin is a common example of a random process.

  • True random numbers vs. pseudo-random numbers: True random numbers are generated from a fundamentally random physical process, such as radioactive decay. Pseudo-random numbers, on the other hand, are generated algorithmically and are not truly random, but they are "random enough" for most purposes.

Pseudo-random Number Generation Process

  • Seed value and its role in generating numbers: The seed value is the starting point in the generation of a sequence of random numbers. Here's a simple analogy: think of the seed as planting a tree. The tree (random sequence) that grows from it is determined by the type and condition of the seed. Let's see how we can use a seed value in Python: import numpy as np np.random.seed(42) random_numbers = np.random.rand(5) print(random_numbers) # Output will be the same every time this code is run

  • Using a function to calculate pseudo-random values: You can use various functions in NumPy to generate pseudo-random numbers that follow different distributions. Here's how you can generate random numbers from a normal distribution: normal_random_numbers = np.random.normal(loc=0, scale=1, size=10) print(normal_random_numbers)

Random Number Generating Functions in NumPy

  • Introduction to various NumPy random functions: NumPy provides a wide variety of functions to generate random numbers. Here's a list of some commonly used ones:

    • np.random.rand(): Uniform distribution over [0, 1).

    • np.random.randn(): Normal distribution with mean 0 and variance 1.

    • np.random.randint(): Discrete uniform distribution over a specified interval.


  • Visualization of random numbers: Let's plot the randomly generated numbers from a normal distribution: import matplotlib.pyplot as plt plt.hist(normal_random_numbers, bins=10) plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram of Randomly Generated Numbers from a Normal Distribution') plt.show()


Working with Random Number Seeds

  • How to set a random seed with NumPy: By setting a seed, you can ensure that the sequence of random numbers is reproducible: np.random.seed(0) print(np.random.rand(3)) # Output will be the same every time

  • Reproducibility and variations in generating random numbers: Without a seed, the sequence will vary every time the code is run. With a seed, the sequence will be the same, allowing for reproducibility in experiments.

The exploration of pseudo-random number generation has shown us the intricate and useful techniques for generating and visualizing random numbers. Understanding this concept is vital in many areas of data science, including simulations, modeling, and statistical analysis.


Conclusion


In this tutorial, we embarked on a detailed exploration of various aspects of statistical sampling and randomness.

  • Introduction to Sampling: We began by defining what sampling is and understanding its importance. We explored historical concepts, such as Pierre-Simon Laplace's contribution, and practical techniques, including sampling using Python and pandas.

  • Point Estimates: We dug into population parameters and point estimates, learning how to calculate means and analyze relationships within data. Examples included analyzing coffee flavor ratings and working with pandas and NumPy for calculations.

  • Convenience Sampling and Bias: This section highlighted the critical aspect of sample representation, including the risks associated with biased sampling. We provided real-world examples like the Literary Digest's failed prediction and visualized biases in data.

  • Pseudo-random Number Generation: We delved into the world of randomness, distinguishing between true random numbers and pseudo-random numbers. We learned about the seed value and its role, worked with various random number generating functions in NumPy, and visualized random numbers.


Throughout this tutorial, we wove in code snippets, analogies, visuals, and real-world examples to provide a comprehensive and practical understanding of these complex topics.


In the field of data science, statistical sampling and randomness are foundational concepts. Whether you're conducting experiments, building models, or analyzing data, understanding these concepts will enhance your ability to make informed and accurate decisions.


Remember, statistics and data science are not just about numbers and calculations. They're about using data to tell a story, make decisions, and uncover the hidden patterns in our world. The skills and understanding gained from this tutorial will empower you to do just that.


Thank you for joining this enlightening journey into the world of statistical sampling and randomness. Happy data exploring!

Comments


bottom of page