liveBook · Manning

Statistical parameter

As explained in Starting Data Analytics with Generative AI and Python

A numerical characteristic of an entire statistical population, such as its mean or proportion.

A statistical parameter is a numerical characteristic of an entire population, such as its mean or proportion. Inferential analysis estimates these population values from sample data, while recognizing that the sample may not represent everyone perfectly. section 3.4 section 3.4.1

Use the distinction between a sample and a population to guide analysis:

  • Estimate a population parameter from a sample rather than treating the sample statistic as exact. section 3.4
  • Use a confidence interval to express plausible values for the parameter and communicate uncertainty around an estimate. section 3.4.1
  • If the dataset contains every customer or transaction in the population, confidence intervals are unnecessary for historical analysis, though inference remains useful for predictive analytics. section 3.4.1

Average monthly spending

Suppose a sample suggests average monthly spending of $200. A 95% confidence interval from $180 to $220 communicates uncertainty about the true population mean rather than presenting $200 as certain. When the distribution is difficult to determine analytically, bootstrap resampling can repeatedly recompute a statistic and use its distribution to estimate the interval. section 3.4.1 section 3.4.4

Listing 3.8 Calculating confidence intervals for sales and review scores by state
# Prepare datasets
# Merge df_products with df_product_category_translation
products_merged = pd.merge(
df_products, df_product_category_translation, on='product_category_name')

# Merge df_orders with df_order_items
order_items_merged = pd.merge(df_orders, df_order_items, on='order_id')

# Merge the resulting DataFrame with df_customers
order_customer_merged = pd.merge(
order_items_merged, df_customers, on='customer_id')

# Merge the resulting DataFrame with products_merged
order_product_merged = pd.merge(
order_customer_merged, products_merged, on='product_id')

# Merge the resulting DataFrame with df_sellers
order_seller_merged = pd.merge(
order_product_merged, df_sellers, on='seller_id')

# Merge the resulting DataFrame with df_order_reviews
order_review_merged = pd.merge(
order_seller_merged, df_order_reviews, on='order_id')

# Merge the resulting DataFrame with df_order_payments
analysis_df = pd.merge(
order_review_merged, df_order_payments, on='order_id')

# Choose a specific product category to analyze
product_category = "health_beauty"
filtered_df = analysis_df[
analysis_df['product_category_name_english'] == product_category]

# Group the data by seller state or city, and calculate 
the metrics of interest
grouped_data = filtered_df.groupby('seller_state')
group_metrics = grouped_data.agg(
{'price': ['sum', 'mean', 'count'], 'review_score': 'mean'})

# Calculate the standard deviations
group_metrics['price', 'std'] = grouped_data['price'].std()
group_metrics['review_score', 'std'] = grouped_data['review_score'].std()

# Use bootstrapping to estimate the 95% confidence intervals for 
the average sales revenue and average review score for each city or state.

import numpy as np
# Bootstrap the confidence intervals
def bootstrap_CI(data, func, n_bootstraps=1000, ci=95, axis=0):
    """
    Generate a confidence interval for a given statistic using 
    bootstrapping.

    Parameters:
    data (numpy.ndarray or pandas.DataFrame): 
    The data to calculate the statistic from.
    func (callable): The function used to calculate the statistic.
    n_bootstraps (int): The number of bootstrap samples to generate.
    ci (float): The confidence interval percentage (e.g., 95 for a 95% CI).
    axis (int): 
    The axis along which to apply the statistic function 
    (0 for columns, 1 for rows).

    Returns:
    tuple: The lower and upper bounds of the confidence interval.
    """
    bootstrapped_statistics = []
    for _ in range(n_bootstraps):
        bootstrap_sample = np.random.choice(
        data, size=len(data), replace=True)
        bootstrapped_statistic = func(bootstrap_sample, axis=axis)
        bootstrapped_statistics.append(bootstrapped_statistic)

    lower_bound = np.percentile(bootstrapped_statistics, (100 - ci) / 2)
    upper_bound = np.percentile(
    bootstrapped_statistics, 100 - (100 - ci) / 2)

    return lower_bound, upper_bound

# Create a new DataFrame to store the confidence intervals
def calculate_ci(group):
    return pd.Series({
        'price_ci_lower': bootstrap_CI(group['price'], np.mean, 
        n_bootstraps=1000, ci=95)[0],
        'price_ci_upper': bootstrap_CI(group['price'], np.mean, 
        n_bootstraps=1000, ci=95)[1],
        'review_score_ci_lower': bootstrap_CI(group['review_score'], 
        np.mean, n_bootstraps=1000, ci=95)[0],
        'review_score_ci_upper': bootstrap_CI(group['review_score'], 
        np.mean, n_bootstraps=1000, ci=95)[1]
    })

ci_df = grouped_data.apply(calculate_ci)

# Create visualizations with error bars for the average sales revenue and 
average review score by seller state.

def plot_error_bars_only(df, metric_name, ylabel='', title='', 
figsize=(10, 6)):
    fig, ax = plt.subplots(figsize=figsize)

    x = np.arange(len(df.index))
    y = df[metric_name + '_ci_lower'] + (df[metric_name + '_ci_upper'] – 
    df[metric_name + '_ci_lower']) / 2
    yerr = (df[metric_name + '_ci_upper'] – 
    df[metric_name + '_ci_lower']) / 2

    plt.errorbar(x, y, yerr=yerr, fmt='o', capsize=5, capthick=2, 
    ecolor='black', elinewidth=2)

    plt.xticks(x, df.index, rotation=90)
    plt.xlabel('Seller State')
    plt.ylabel(ylabel)
    plt.title(title)
    plt.grid(axis='y')

    plt.show()

# Plot the confidence intervals
plot_error_bars_only(ci_df, 'price', ylabel='Average Sales')
plot_error_bars_only(ci_df, 'review_score', ylabel='Average Review Score')

Inspect a Python workflow that calculates confidence intervals for sales prices and review scores by state after merging customer, product, order, seller, and review data. section 3.4.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Explains estimation, confidence intervals, complete-population boundaries, and bootstrap resampling without developing statistical theory in depth.
Examples
Illustrates average customer spending, equipment breakpoint temperatures, and state-level sales prices and review scores.
What it assumes
Assumes familiarity with samples, populations, and basic statistics while distinguishing complete datasets from samples.
What you can do after
Apply confidence intervals and bootstrap resampling to estimate population values from sample data.
How it connects
Connects population parameters with inferential statistics, sampling, confidence intervals, hypothesis testing, and bootstrap resampling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.