Skip to content
Laviyx Learn

Try

Statistics and Time Series Practice

Practise descriptive statistics, outliers, correlation, hypothesis tests, A/B tests, dates and rolling windows.

Advanced8 problemsUpdated

Review first: Descriptive Statistics, Correlation, Hypothesis Testing, A/B Testing, Dates and Time Series, Rolling Windows and Cumulative Values

  1. 1.Mean or median?

    Intermediate

    House prices (₹ lakh) in a street are [55, 60, 58, 62, 57, 300]. Print the mean, the median and the IQR (1 decimal each). Which is the better "typical price"?

    Show hint

    mean(), median(), and quantile(0.75) - quantile(0.25).

    Show solution
    Python
    import pandas as pd
    
    prices = pd.Series([55, 60, 58, 62, 57, 300])
    print(round(prices.mean(), 1), prices.median())
    print(round(prices.quantile(0.75) - prices.quantile(0.25), 1))
    Output
    98.7 59.0
    4.2

    One mansion pulls the mean to 98.7 while the median, 59, reflects the typical house. The median is the better summary here.

  2. 2.Find outliers with the IQR rule

    Intermediate

    Daily orders were [42, 38, 45, 40, 160, 41, 39, 44, 2, 43]. Using the 1.5 × IQR rule, print the lower and upper limits and the outlier values.

    Show hint

    Q1 and Q3 from quantile, limits Q1 − 1.5·IQR and Q3 + 1.5·IQR.

    Show solution
    Python
    import pandas as pd
    
    orders = pd.Series([42, 38, 45, 40, 160, 41, 39, 44, 2, 43])
    q1, q3 = orders.quantile([0.25, 0.75])
    low, high = q1 - 1.5 * (q3 - q1), q3 + 1.5 * (q3 - q1)
    print(low, high)
    print(orders[(orders < low) | (orders > high)].tolist())
    Output
    32.5 50.5
    [160, 2]
  3. 3.Pearson and Spearman

    Intermediate

    Advertising spend [10, 20, 30, 40, 50] and sales [12, 25, 33, 41, 200] were recorded. Print both the Pearson and Spearman correlations (3 decimals) and explain the difference.

    Show hint

    a.corr(b) and a.corr(b, method="spearman").

    Show solution
    Python
    import pandas as pd
    
    spend = pd.Series([10, 20, 30, 40, 50])
    sales = pd.Series([12, 25, 33, 41, 200])
    print(round(spend.corr(sales), 3), round(spend.corr(sales, method="spearman"), 3))
    Output
    0.797 1.0

    Sales always increase with spend, so the rank-based Spearman correlation is exactly 1. The last, very large value bends the relationship away from a straight line, so Pearson is lower.

  4. 4.Compare two groups

    Advanced

    Session lengths (minutes) for an old and a new app design were old [5, 7, 6, 8, 5, 6, 7, 6] and new [8, 9, 7, 10, 9, 8, 11, 9]. Run Welch's t-test and print the two means and the p-value (4 decimals).

    Show hint

    stats.ttest_ind(new, old, equal_var=False).

    Show solution
    Python
    import numpy as np
    from scipy import stats
    
    old = np.array([5, 7, 6, 8, 5, 6, 7, 6])
    new = np.array([8, 9, 7, 10, 9, 8, 11, 9])
    result = stats.ttest_ind(new, old, equal_var=False)
    print(old.mean(), new.mean(), round(result.pvalue, 4))
    Output
    6.25 8.875 0.0005
  5. 5.A/B test on conversion

    Advanced

    Version A had 2,000 visitors and 120 purchases; version B had 2,000 visitors and 156 purchases. Print both conversion rates, the z statistic (3 decimals) and the two-sided p-value (4 decimals) of a two-proportion z-test.

    Show hint

    Pool the rates, compute the standard error, then z = (pb - pa) / se and p = 2 * (1 - norm.cdf(abs(z))).

    Show solution
    Python
    import math
    from scipy.stats import norm
    
    na, ca, nb, cb = 2000, 120, 2000, 156
    pa, pb = ca / na, cb / nb
    pool = (ca + cb) / (na + nb)
    se = math.sqrt(pool * (1 - pool) * (1 / na + 1 / nb))
    z = (pb - pa) / se
    print(pa, pb, round(z, 3), round(2 * (1 - norm.cdf(abs(z))), 4))
    Output
    0.06 0.078 2.246 0.0247
  6. 6.Monthly totals from daily data

    Intermediate

    Daily sales are 1, 2, 3, … 90 for the 90 days starting 1 January 2026. Print the total for each month, with month names as the index.

    Show hint

    Build a Series with a pd.date_range index, resample("ME").sum(), then format the index with strftime("%b").

    Show solution
    Python
    import pandas as pd
    
    sales = pd.Series(range(1, 91), index=pd.date_range("2026-01-01", periods=90, freq="D"))
    monthly = sales.resample("ME").sum()
    monthly.index = monthly.index.strftime("%b")
    print(monthly)
    Output
    Jan     496
    Feb    1274
    Mar    2325
    dtype: int64
  7. 7.Moving average and growth

    Intermediate

    Weekly revenue is [200, 220, 210, 260, 300, 280]. Print a table with the 3-week moving average (1 decimal), the running total and the week-over-week growth in percent (1 decimal).

    Show hint

    rolling(3).mean(), cumsum() and pct_change() * 100.

    Show solution
    Python
    import pandas as pd
    
    revenue = pd.Series([200, 220, 210, 260, 300, 280], index=range(1, 7))
    print(pd.DataFrame({
        "revenue": revenue,
        "avg_3w": revenue.rolling(3).mean().round(1),
        "total": revenue.cumsum(),
        "growth_pct": (revenue.pct_change() * 100).round(1),
    }))
    Output
       revenue  avg_3w  total  growth_pct
    1      200     NaN    200         NaN
    2      220     NaN    420        10.0
    3      210   210.0    630        -4.5
    4      260   230.0    890        23.8
    5      300   256.7   1190        15.4
    6      280   280.0   1470        -6.7
  8. 8.Days since last order

    Intermediate

    Customers' last order dates are {"Asha": "2026-03-28", "Ravi": "2026-01-15", "Meera": "2026-03-01"}. Taking 1 April 2026 as today, print the days since each last order and the names of customers inactive for more than 30 days.

    Show hint

    Subtract the dates from pd.Timestamp("2026-04-01") and use .dt.days.

    Show solution
    Python
    import pandas as pd
    
    last = pd.to_datetime(pd.Series({"Asha": "2026-03-28", "Ravi": "2026-01-15", "Meera": "2026-03-01"}))
    days = (pd.Timestamp("2026-04-01") - last).dt.days
    print(days)
    print(days[days > 30].index.tolist())
    Output
    Asha      4
    Ravi     76
    Meera    31
    dtype: int64
    ['Ravi', 'Meera']