A daily average sentiment score is easy to compute and easy to misread. If one day has 38 scored snippets and the next has 8, their averages carry very different amounts of information, but a line chart draws them the same way. A confidence interval fixes that, and scipy.stats.bootstrap produces one without assuming the scores are normally distributed.

This tutorial applies SciPy bootstrap confidence intervals to a deliberately awkward dataset: sentiment scores clipped to [-1, 1], about a third of them exactly zero, heavy tails, and a shared "mood of the day" that makes snippets from the same day correlated. You will compare the percentile, basic and BCa methods, see why a bootstrap interval for the median can collapse to a single point, bootstrap a difference between two weeks, find out how much a naive bootstrap understates uncertainty when observations are clustered by day, and build a daily table with error bars.

The data is synthetic and the ticker is unnamed. The scores stand in for the output of any sentiment model that rates short texts.

Generating the sample

import numpy as np import pandas as pd from scipy import stats rng = np.random.default_rng(7) days = pd.date_range("2026-10-19", periods=14, freq="D") rows = [] for i, d in enumerate(days): n = int(rng.integers(8, 40)) # snippets per day varies a lot day_shift = rng.normal(0, 0.15) # shared mood of the day base = 0.10 if i < 7 else -0.05 # week 2 is slightly more negative for _ in range(n): if rng.random() < 0.35: s = 0.0 # neutral snippets else: s = np.clip(base + day_shift + rng.laplace(0, 0.35), -1, 1) rows.append((d, round(float(s), 3))) snips = pd.DataFrame(rows, columns=["date", "score"]) snips["week"] = np.where(snips["date"] < days[7], "w1", "w2") print(snips.groupby("week")["score"].agg(["size", "mean", "median"]).round(3)) 
 size mean median week w1 158 0.030 0.000 w2 138 -0.098 -0.007 

Two features of this data cause trouble later. The medians are pinned at or near zero by the neutral snippets, and day_shift means snippets are not independent: if a day starts gloomy, most of its snippets lean negative together.

Step 1: a basic bootstrap confidence interval for the mean

scipy.stats.bootstrap takes a tuple of samples and a statistic. The tuple matters: passing w1 instead of (w1,) raises an error or, worse, treats each value as a separate sample.

w1 = snips.loc[snips.week == "w1", "score"].to_numpy() w2 = snips.loc[snips.week == "w2", "score"].to_numpy() res = stats.bootstrap((w1,), np.mean, n_resamples=9999, confidence_level=0.95, method="BCa", random_state=1) print(res.confidence_interval) # ConfidenceInterval(low=-0.032, high=0.086) print(res.standard_error) # 0.030 

The result also carries res.bootstrap_distribution, the 9,999 resampled means. Plot a histogram of it once. If it is lumpy or strongly skewed, that tells you more than any single interval.

Set random_state so the interval is reproducible. With 9,999 resamples the endpoints move only in the third decimal between seeds. With 999 they can move noticeably, which matters if you compare intervals across days.

Step 2: percentile vs basic vs BCa

SciPy offers three methods. On the same sample:

for m in ("percentile", "basic", "BCa"): r = stats.bootstrap((w1,), np.mean, n_resamples=9999, method=m, random_state=1) print(f"{m:10s}", np.round(r.confidence_interval, 3)) t = stats.t.interval(0.95, len(w1) - 1, loc=w1.mean(), scale=stats.sem(w1)) print("t-interval", np.round(t, 3)) 
percentile [-0.031 0.086] basic [-0.027 0.09 ] BCa [-0.032 0.086] t-interval [-0.03 0.089] 

For the mean of 158 observations all four agree to about 0.005, which is reassuring rather than surprising: the central limit theorem is already doing its job. The methods differ when the statistic is skewed or the sample is small:

  • percentile takes the 2.5% and 97.5% quantiles of the bootstrap distribution. It is simple and works for most statistics.
  • basic reflects those quantiles around the observed value. It can produce endpoints outside the possible range (below -1 for a score clipped at -1).
  • BCa (bias-corrected and accelerated, SciPy's default) adjusts for bias and skew. It is usually the best choice for means and similar smooth statistics, but it needs a jackknife pass and is slower on large samples.

Use BCa by default. When it returns nan with a warning, the statistic itself is usually the problem, as the next step shows.

Step 3: the median trap

med = stats.bootstrap((w1,), np.median, n_resamples=9999, method="percentile", random_state=1) print(med.confidence_interval) # ConfidenceInterval(low=0.0, high=0.0) 

An interval of [0, 0] does not mean the median is known exactly. It means more than half the resamples have a median of exactly zero, because 35% of the snippets are zero and the rest are spread on both sides. Bootstrap intervals for quantiles of discrete or zero-inflated data are unreliable. With method="BCa" the same call returns nan endpoints and a warning that the BCa interval cannot be calculated, which is SciPy telling you the bootstrap distribution is degenerate.

Two practical alternatives:

  • Report the mean of the non-neutral snippets plus the neutral share as two separate numbers.
  • Report a trimmed mean (stats.trim_mean(x, 0.1)), which still resists outliers but does not collapse onto the zero mass.

Step 4: bootstrap a difference between two weeks

With two samples in the tuple, the statistic receives both. Write it with an axis argument so SciPy can vectorise the resampling:

def diff_means(x, y, axis=-1): return np.mean(y, axis=axis) - np.mean(x, axis=axis) d = stats.bootstrap((w1, w2), diff_means, n_resamples=9999, method="BCa", random_state=1) print(round(diff_means(w1, w2), 3), np.round(d.confidence_interval, 3)) # -0.128 [-0.212 -0.045] 

SciPy resamples each sample independently (paired=False is the default), which is correct when the two weeks contain different snippets. If you had two scores for each snippet, for example from two different models, use paired=True so the pairs stay together.

On its face, the interval excludes zero, which suggests week 2 really is more negative. The next step shows why that conclusion is too confident.

Step 5: clustered data needs a cluster bootstrap

The standard bootstrap assumes every snippet is an independent draw. Here they are not: each day has its own day_shift. Resampling individual snippets mixes them across days and hides the day-to-day variation, so the interval comes out too narrow.

The fix is to resample whole days. SciPy has no built-in cluster bootstrap, but it takes a dozen lines:

def cluster_boot_diff(df, n_iter=5000, seed=3): r = np.random.default_rng(seed) groups = {wk: [g["score"].to_numpy() for _, g in sub.groupby("date")] for wk, sub in df.groupby("week")} out = np.empty(n_iter) for i in range(n_iter): means = {} for wk, blocks in groups.items(): pick = r.integers(0, len(blocks), len(blocks)) # resample days means[wk] = np.concatenate([blocks[j] for j in pick]).mean() out[i] = means["w2"] - means["w1"] return out cb = cluster_boot_diff(snips) print(np.round(np.quantile(cb, [0.025, 0.975]), 3)) # [-0.249 -0.003] print(round(cb.std(), 4), "vs naive", round(d.standard_error, 4)) # 0.0639 vs naive 0.0431 

The standard error grows by about 50% and the upper end of the interval moves to -0.003, right next to zero. With only seven days per week, the honest answer is "probably more negative, but the evidence is thin". The naive bootstrap said "clearly more negative".

The general rule: resample at the level where the randomness is independent. For sentiment data that is often the day, the source, or the news event, not the individual snippet. For long, autocorrelated series, a moving-block bootstrap (resampling contiguous runs of days) applies the same idea.

Step 6: intervals for a proportion

The share of clearly negative snippets is often more readable than an average score. A bootstrap works, and for a simple proportion you can check it against the Wilson interval from statsmodels:

from statsmodels.stats.proportion import proportion_confint neg1 = (w1 < -0.2).astype(float) pr = stats.bootstrap((neg1,), np.mean, n_resamples=9999, method="percentile", random_state=1) print(round(neg1.mean(), 3), np.round(pr.confidence_interval, 3)) print(np.round(proportion_confint(int(neg1.sum()), len(neg1), method="wilson"), 3)) 
0.158 [0.108 0.215] [0.11 0.223] 

They agree closely. Prefer the Wilson formula for a plain proportion: it is exact enough, instant, and behaves well when the share is near 0 or 1, where the bootstrap struggles.

Step 7: a daily table with error bars

Finally, attach an interval to every day and refuse to compute one when the sample is too small:

def daily_ci(s, min_n=10): x = s.to_numpy() if len(x) < min_n: return pd.Series({"n": len(x), "mean": x.mean(), "lo": np.nan, "hi": np.nan}) r = stats.bootstrap((x,), np.mean, n_resamples=2000, method="percentile", random_state=0) return pd.Series({"n": len(x), "mean": x.mean(), "lo": r.confidence_interval.low, "hi": r.confidence_interval.high}) tab = snips.groupby("date")["score"].apply(daily_ci).unstack() tab["n"] = tab["n"].astype(int) print(tab.head(6).round(3)) 
 n mean lo hi date 2026-10-19 38 -0.049 -0.170 0.066 2026-10-20 28 0.129 -0.027 0.280 2026-10-21 28 0.014 -0.103 0.122 2026-10-22 12 -0.087 -0.279 0.042 2026-10-23 21 -0.047 -0.255 0.150 2026-10-24 8 -0.006 NaN NaN 

Interval widths across the 14 days range from 0.19 to 0.49. Nine of the 13 days with enough snippets have an interval that crosses zero, so most single days are not clearly positive or negative on their own. That is exactly what the chart without error bars hides. In a plot, draw the mean line with a shaded lo–hi band (ax.fill_between(tab.index, tab.lo, tab.hi, alpha=0.2)) and mark the days below min_n instead of leaving them out silently.

FAQ

How many resamples should I use with scipy.stats.bootstrap?
9,999 is SciPy's default and a good choice for final numbers. 1,000–2,000 is fine for exploratory tables. Fix random_state either way.

Why does scipy bootstrap return NaN or warn about a degenerate distribution?
Usually the statistic takes very few distinct values across resamples, as with the median of zero-inflated data, or the sample is tiny. Switching to method="percentile" removes the NaN but not the underlying problem, so change the statistic first.

Is the bootstrap valid for time series?
Not with independent resampling. Resample blocks (days, weeks or contiguous runs) so that the dependence inside each block is preserved.

Conclusion

scipy.stats.bootstrap gives a confidence interval for almost any statistic in one call, and for the mean of a reasonably large sample the method you choose barely matters. The real decisions come before the call: pick a statistic that does not collapse on zero-inflated scores, resample at the level where observations are independent, use a closed-form interval when one exists, and decline to report an interval for days with too few observations. To see how sentiment snippets about a single stock look over time in practice, open the CATL sentiment monitor in Lava.

中文简介:本文用 scipy.stats.bootstrap 给噪声较大的个股情绪分数加置信区间:比较 percentile、basic、BCa 三种方法,说明零值占比高时中位数区间会塌缩成一个点;演示两周均值差的 bootstrap,以及同一天内样本相关时按天整块重抽(cluster bootstrap)为何更可靠;再用 Wilson 区间核对比例,并生成带误差范围的每日表。数据为合成样本,仅作统计方法演示。