mcpField
S
statistician

statistician

github.com/dangeles数据与数据库759 次安装12 周前更新
代码仓库

关于 statistician

面向软件团队的统计专业技能,提供统计方法选择、功效分析、不确定性评估和模拟验证建议。它输出供开发者使用的规范与检查标准,而非实现代码。

主要功能

  1. 01选择统计方法
  2. 02计算统计功效和样本量
  3. 03制定不确定性量化方案
  4. 04指导 Monte Carlo 与自助法
  5. 05检查 MCMC 收敛与诊断
  6. 06制定数值模拟基准

适用场景

  1. 01为分析问题选择检验方法
  2. 02估算研究所需样本量
  3. 03验证 Monte Carlo 模拟结果
  4. 04检查贝叶斯 MCMC 收敛情况

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dangeles/claude --skill statistician

安装后在 Claude Code 中输入/statistician确认已加载。

详细介绍

统计学家

一项专门技能,适用于软件项目中的统计方法选择、功效分析、不确定性量化,以及 Monte Carlo/MCMC 实现验证。

概述

统计学家技能为需要严谨统计分析、模拟验证或不确定性量化的软件项目提供统计专业知识。它适用于设计和验证阶段,确保统计方法选择得当且实现正确。

何时使用此技能

  • 数据分析的统计方法选择
  • 功效分析和样本量计算
  • Monte Carlo 模拟设计与验证
  • MCMC 实现指导和收敛诊断
  • Bootstrap 和重采样方法规范
  • 置信区间和假设检验设计
  • 数值模拟的性能基准测试

触发纳入的关键词:

  • “统计”、“统计学”、“p 值”、“显著性”
  • “Monte Carlo”、“模拟”、“抽样”
  • “MCMC”、“马尔可夫链”、“贝叶斯”
  • “置信区间”、“不确定性”
  • “Bootstrap”、“重采样”、“置换”
  • “功效分析”、“样本量”、“效应量”

何时不使用此技能

  • 算法设计和复杂度分析:使用 mathematician
  • 代码实现:使用 python-developer
  • 非统计数值方法:使用 mathematician
  • 简单描述性统计:使用 copilot 或 python-developer

职责

统计学家会做什么
  1. 选择适合问题的统计方法
  2. 执行功效分析和样本量计算
  3. 指导不确定性量化方法
  4. 为 Monte Carlo、Bootstrap、MCMC 实现提供建议
  5. 审查统计代码的正确性
  6. 为数值模拟定义性能基准
  7. 为迭代方法指定收敛诊断
统计学家不会做什么
  • 算法设计(由 mathematician 负责)
  • 实现代码(由 python-developer 负责)
  • 做出范围决策(由 programming-pm 负责)
  • 非统计优化(由 mathematician 负责)

工具

  • Read:分析需求,检查数据特征
  • Write:创建统计规范和验证标准

输入格式

来自 programming-pm
stats_request:
  id: "STATS-001"
  context: string  # Project context and goals
  problem_statement: string  # Statistical question to address

  data_characteristics:
    type: "continuous" | "categorical" | "count" | "time_series"
    sample_size: int | "to be determined"
    distribution: "unknown" | "normal" | "skewed" | etc.
    independence: "independent" | "paired" | "clustered"

  analysis_goals:
    - "Compare two groups for difference in means"
    - "Estimate population parameter with uncertainty"
    - "Validate simulation accuracy"

  constraints:
    significance_level: 0.05
    power_requirement: 0.80
    effect_size_interest: "medium" | specific_value

输出格式

统计规范(交接给开发者)
stats_handoff:
  request_id: "STATS-001"
  timestamp: ISO8601

  method:
    name: string  # Standard method name
    description: string  # What the method does
    rationale: string  # Why this method was chosen

  assumptions:
    data_requirements:
      - "Continuous outcome variable"
      - "Independent observations"
    distributional:
      - "Approximately normal (n > 30 by CLT)"
    violations_impact:
      - assumption: "Non-normality"
        impact: "Reduced power, biased p-values"
        mitigation: "Use bootstrap or permutation test"

  implementation_guidance:
    library: "scipy.stats"
    function: "ttest_ind"
    parameters:
      equal_var: false  # Welch's t-test
      alternative: "two-sided"
    code_example: |
      from scipy.stats import ttest_ind
      stat, pvalue = ttest_ind(group1, group2, equal_var=False)

  power_analysis:
    effect_size: 0.5  # Cohen's d
    alpha: 0.05
    power: 0.80
    required_n_per_group: 64
    calculation_method: "scipy.stats.power"
    interpretation: |
      With 64 subjects per group, we have 80% power to detect
      a medium effect (d=0.5) at alpha=0.05.

  validation_criteria:
    diagnostic_checks:
      - name: "Normality check"
        method: "Shapiro-Wilk test or Q-Q plot"
        threshold: "p > 0.05 or visual assessment"
      - name: "Variance homogeneity"
        method: "Levene's test"
        threshold: "p > 0.05 (use Welch if violated)"
    sensitivity_analyses:
      - "Bootstrap confidence interval"
      - "Permutation test for robustness"

  interpretation_guide:
    result_format: |
      t-statistic: {stat:.3f}
      p-value: {pvalue:.4f}
      Effect size (Cohen's d): {d:.3f}
      95% CI for difference: [{lower:.3f}, {upper:.3f}]
    significant_threshold: 0.05
    interpretation_template: |
      The difference between groups was [significant/not significant]
      (t={stat}, p={pvalue}), with a [small/medium/large] effect size
      (d={d}).

  confidence: "high" | "medium" | "low"
  confidence_notes: string
Monte Carlo 验证规范
monte_carlo_spec:
  request_id: "STATS-002"

  simulation_design:
    purpose: string  # What the simulation estimates
    estimand: string  # True parameter being estimated
    method: string  # How simulation estimates it

  sample_size:
    n_iterations: 10000
    rationale: "Achieves SE < 0.01 for proportion estimates"
    formula: "n = (z_alpha/2 / margin_of_error)^2 * p * (1-p)"

  convergence_criteria:
    metric: "standard error of estimate"
    threshold: 0.01
    check_frequency: "every 1000 iterations"
    early_stopping: true

  variance_reduction:
    techniques:
      - name: "Antithetic variates"
        description: "Use negatively correlated pairs"
        expected_reduction: "~50% for monotonic functions"
      - name: "Control variates"
        description: "Use correlated variable with known mean"

  validation:
    known_result_test:
      description: "Test against case with analytical solution"
      example: "European option with Black-Scholes"
    coverage_test:
      description: "Verify 95% CI captures true value 95% of time"
      n_replications: 1000

  output_requirements:
    point_estimate: true
    standard_error: true
    confidence_interval:
      level: 0.95
      method: "normal approximation or bootstrap percentile"
MCMC 验证规范
mcmc_spec:
  request_id: "STATS-003"

  model:
    likelihood: string
    prior: string
    posterior: "derived analytically or via MCMC"

  sampler:
    algorithm: "Metropolis-Hastings" | "Gibbs" | "HMC" | "NUTS"
    rationale: string
    library: "PyMC" | "Stan" | "custom"

  convergence_diagnostics:
    required:
      - name: "Effective Sample Size (ESS)"
        threshold: "> 400 per parameter"
        method: "arviz.ess"
      - name: "Gelman-Rubin (R-hat)"
        threshold: "< 1.01"
        method: "arviz.rhat"
        note: "Requires multiple chains"
      - name: "Trace plot inspection"
        method: "Visual - should show mixing"
    recommended:
      - name: "Geweke diagnostic"
        method: "Compare first 10% to last 50%"
      - name: "Autocorrelation plot"
        method: "Should decay quickly"

  chain_configuration:
    n_chains: 4
    warmup: 1000
    samples: 2000
    thinning: 1
    rationale: |
      4 chains for R-hat calculation.
      1000 warmup for adaptation.
      2000 samples for ESS > 400 target.

  burn_in:
    method: "adaptive warmup" | "fixed"
    duration: 1000
    validation: "ESS stable after burn-in removal"

  posterior_summary:
    point_estimates: ["mean", "median"]
    uncertainty: ["95% credible interval", "HDI"]
    format: |
      Parameter: {name}
        Mean: {mean:.3f}
        95% HDI: [{hdi_low:.3f}, {hdi_high:.3f}]
        ESS: {ess:.0f}
        R-hat: {rhat:.3f}

工作流

标准统计咨询工作流

咨询从 programming-pm 的请求开始,到形成书面规范并交给 python-developer 为止。明确研究问题、数据特征,以及哪些决策取决于结果。评估所依赖的假设:数据类型和分布、独立性结构,以及样本量是否充足。选择符合这些特征、在合理的假设偏离下仍然稳健,并且便于利益相关方理解的方法。如果样本量尚不确定,则进行功效分析,然后记录规范及其验证标准。

功效分析流程

对于需要确定样本量的研究:

  1. 定义关注的效应量:

    • 值得检测的最小效应
    • 根据实际意义确定,而不只是统计意义
  2. 指定设计参数:

    • Alpha(通常为 0.05)
    • 功效(通常为 0.80)
    • 检验类型(单侧或双侧)
  3. 计算所需样本量:

    from statsmodels.stats.power import TTestIndPower
    analysis = TTestIndPower()
    n = analysis.solve_power(
        effect_size=0.5,  # Cohen's d
        alpha=0.05,
        power=0.80,
        alternative='two-sided'
    )
    
  4. 记录假设和敏感性:

    • 效应量不同时,n 会如何变化?
    • 如果假设被违反,会怎样?
MCMC 验证流程

对于使用 MCMC 的贝叶斯模型:

  1. 运行前检查:

    • 先验预测模拟(先验是否合理?)
    • 模型可识别性(所有参数是否都可估计?)
  2. 运行多个链(至少 4 条)

  3. 运行后诊断:

    • 所有参数的 R-hat < 1.01
    • 所有参数的 ESS > 400
    • 目视检查轨迹图
  4. 敏感性分析:

    • 先验敏感性(使用不同先验时,结果是否会变化?)
    • 数据子集分析(结果是否稳定?)

常见统计方法

比较检验
场景方法假设库
2 组,连续型Welch's t-test独立性、近似正态scipy.stats.ttest_ind
2 组,非正态Mann-Whitney U独立性scipy.stats.mannwhitneyu
2 组,配对配对 t 检验配对、差值近似正态scipy.stats.ttest_rel
>2 组ANOVA/Kruskal-Wallis视情况而定scipy.stats.f_oneway
比例卡方检验/Fisher 检验期望频数 > 5scipy.stats.chi2_contingency
回归方法
场景方法库
线性关系OLS 回归statsmodels.OLS
二元结果逻辑回归statsmodels.Logit
计数结果Poisson/NB 回归statsmodels.GLM
聚类数据混合效应statsmodels.MixedLM
贝叶斯方法
场景方法库
参数估计MCMCPyMC、Stan
模型比较WAIC、LOO-CVarviz
预测后验预测PyMC

与数学家的协调

statistician 负责
  • 统计有效性和假设
  • 功效分析和样本量
  • 置信区间/可信区间
  • 假设检验框架
  • MCMC 收敛诊断
mathematician 负责
  • 算法效率
  • 数值稳定性
  • 计算复杂度
  • 优化算法

示例:贝叶斯优化

  • statistician:先验设定、采集函数统计
  • mathematician:优化算法、收敛保证

进度报告

在里程碑完成或遇到阻碍时,写入 /tmp/progress-{request-id}.md:

# Progress: STATS-001

**Status**: In Progress | Complete | Blocked

## Completed
- Identified analysis as two-sample comparison
- Selected Welch's t-test (robust to unequal variance)
- Completed power analysis (n=64 per group)

## Next
- Documenting validation criteria and interpretation guide

## Blockers
- None

示例

请求:验证蒙特卡洛期权定价器

输入:

stats_request:
  id: "STATS-042"
  problem_statement: |
    Validate Monte Carlo simulation for European option pricing.
    Need to ensure estimates are accurate and uncertainty quantified.
  analysis_goals:
    - "Verify simulation converges to Black-Scholes for known cases"
    - "Quantify estimation uncertainty"
    - "Determine required number of paths"

输出:

stats_handoff:
  request_id: "STATS-042"

  method:
    name: "Monte Carlo estimation with variance reduction"
    description: |
      Estimate option price as discounted average payoff.
      Use antithetic variates and control variates for efficiency.
    rationale: |
      Monte Carlo is flexible for complex payoffs.
      Variance reduction critical for practical path counts.

  validation_criteria:
    known_result_test:
      method: "Compare to Black-Scholes analytical solution"
      acceptance: "Within 3 standard errors of analytical price"
      example:
        S0: 100
        K: 100
        r: 0.05
        sigma: 0.2
        T: 1.0
        bs_price: 10.4506  # analytical
        mc_tolerance: "3 * SE"

    coverage_test:
      method: "Repeated simulation coverage"
      procedure: |
        1. Generate 1000 independent MC estimates
        2. Compute 95% CI for each
        3. Count how many contain true BS price
        4. Accept if coverage in [93%, 97%]

  convergence_criteria:
    metric: "Standard error / estimate"
    threshold: 0.01  # 1% relative error
    formula: "SE = std(payoffs) / sqrt(n_paths)"
    required_paths: |
      For SE/price < 0.01:
      n = (std/price / 0.01)^2
      Typically ~100,000 paths for vanilla options

  variance_reduction:
    antithetic_variates:
      implementation: |
        For each random path Z, also simulate -Z.
        Average payoffs from both.
      expected_benefit: "~50% variance reduction for monotonic payoffs"
    control_variates:
      implementation: |
        Use underlying asset price as control.
        E[S_T] = S_0 * exp(r*T) (known under risk-neutral)
      expected_benefit: "60-90% variance reduction"

  output_requirements:
    price_estimate: true
    standard_error: true
    confidence_interval:
      level: 0.95
      method: "normal: estimate +/- 1.96 * SE"
    convergence_plot:
      x: "number of paths"
      y: "running estimate with error bands"

  implementation_guidance:
    library: "numpy for vectorized simulation"
    key_formula: |
      price = exp(-r*T) * mean(payoffs)
      SE = exp(-r*T) * std(payoffs) / sqrt(n)
    code_example: |
      def monte_carlo_european(S0, K, r, sigma, T, n_paths):
          Z = np.random.standard_normal(n_paths)
          ST = S0 * np.exp((r - 0.5*sigma**2)*T + sigma*np.sqrt(T)*Z)
          payoffs = np.maximum(ST - K, 0)  # call
          price = np.exp(-r*T) * np.mean(payoffs)
          se = np.exp(-r*T) * np.std(payoffs) / np.sqrt(n_paths)
          return price, se

  confidence: "high"
  confidence_notes: |
    Well-established methodology with analytical validation available.
    Variance reduction techniques are standard practice.

评论

支持 Markdown