简体   繁体   English

创建(正常)均值和标准差的 2 层级估计

[英]Create a 2-hierarchy estimate of (normal) mean and standard deviation

I have a normally distributed variable x (like product demand), an index id_1 (like product number) and a second index id_2 (like product group).我有一个正态分布的变量 x(如产品需求)、一个索引 id_1(如产品编号)和第二个索引 id_2(如产品组)。 My goal is to estimate the mean and the standard deviations for x hierarchically (all > product group > product).我的目标是按层次估计 x 的均值和标准差(全部 > 产品组 > 产品)。 That's my data:这是我的数据:

import numpy as np
import pymc3 as pm
import arviz as az

# data
my_step_1 = 0.4
my_step_2 = 4.1
sd_step_1 = 0.1
sd_step_2 = 0.2
my = 10
sd = .1
grp_n = 8
grps_1 = 5
grps_2 = 4

x = np.round(np.concatenate([np.random.normal(my + i * my_step_1 + j * my_step_2, \
                      sd + i * sd_step_1 + j * sd_step_2, grp_n) \
    for i in range(grps_1) for j in range(grps_2)]), 1) # demand
id_1 = np.repeat(np.arange(grps_1 * grps_2), grp_n) # group, product number
id_2 = np.tile(np.repeat(np.arange(grps_2), grp_n), grps_1) # super-group, product group
shape_1 = len(np.unique(id_1))
shape_2 = len(np.unique(id_2))

I've managed a single hierarchy:我管理了一个层次结构:

with pm.Model() as model_h1:
    #
    mu_mu_hyper = pm.Normal('mu_mu_hyper', mu = 0, sd = 10)
    mu_sd_hyper = pm.HalfNormal('mu_sd_hyper', 10)
    sd_hyper = pm.HalfNormal('sd_hyper', 10)
    #
    mu = pm.Normal('mu', mu = mu_mu_hyper, sd = mu_sd_hyper, shape = shape_1)
    sd = pm.HalfNormal('sd', sd = sd_hyper, shape = shape_1)

    y = pm.Normal('y', mu = mu[id_1], sd = sd[id_1], observed = x)

    trace_h1 = pm.sample(1000)

#az.plot_forest(trace_h1, var_names=['mu', 'sd'], combined = True)

But how can I code 2 hierarchies?但是我如何编码 2 个层次结构?

# 2 hierarchies .. doesn't work
with pm.Model() as model_h2:  
    #
    mu_mu_hyper2 = pm.Normal('mu_mu_hyper2', mu = 0, sd = 10)
    mu_sd_hyper2 = pm.HalfNormal('mu_sd_hyper2', sd = 10)
    sd_mu_sd_hyper2 = pm.HalfNormal('sd_mu_sd_hyper2', sd = 10)
    sd_hyper2 = pm.HalfNormal('sd_hyper2', sd = 10)    
    #
    mu_mu_hyper1 = pm.Normal('mu_hyper1', mu = mu_mu_hyper2, sd = mu_sd_hyper2, shape = shape_2)
    mu_sd_hyper1 = pm.HalfNormal('mu_sd_hyper1', sd = sd_mu_sd_hyper2, shape = shape_2)
    sd_hyper1 = pm.HalfNormal('sd_hyper1', sd = sd_hyper2, shape = shape_2)
    #sd_hyper1 = pm.HalfNormal('sd_hyper1', sd = sd_hyper2[id_2], shape = shape_2)??
    #
    mu = pm.Normal('mu', mu = mu_mu_hyper1, sd = mu_sd_hyper1, shape = shape_1)
    sd = pm.HalfNormal('sd', sd = sd_hyper1, shape = shape_1)
    
   y = pm.Normal('y', mu = mu[id_1], sd = sd[id_1], observed = x)

    trace_h2 = pm.sample(1000)

You could try looping through product groups and use the mean, std for a group as constraints for the products belonging to this particular group.您可以尝试遍历产品组并使用组的均值、标准差作为属于该特定组的产品的约束。

# sample product group to product mapping
group_product_mapping = {0: [1, 2, 3], 1: [4, 5, 6]}
total_groups = len(group_product_mapping.keys())
with pm.model() as model_h:
    mu_all = pm.Normal('mu_all', 0, 10)
    sd_all = pm.HalfNormal('sd_all', 10)
    sd_mu_group = pm.HalfNormal('sd_mu_group', 10)
    
    # group parameters constrained to mu, sd from all
    mu_group = pm.Normal('mu_group', mu_all, sd_all, shape=total_groups) 
    sd_group = pm.HalfNormal('sd_group', sd_mu_group, shape=total_groups)
    
    mu_products = dict()
    # iterate through groups and constrain product parameters to the product group they belong to
    for idx, group in enumerate(group_product_mapping.keys()):
        mu_products[group] = pm.Normal(f'mu_products_{group}', mu_group[idx], sd_group[idx], shape=len(group_product_mapping[group]))
        sd_produtcs[group] = pm.HalfNormal(f'sd_mu_products_{group}', 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为多个元素创建具有均值和标准差的直方图 - Create histogram with mean and standard deviation for multiples elements 创建一个具有预先确定的平均值和标准偏差的数组 - Create an array with a pre determined mean and standard deviation 给定均值和标准差,如何计算正态分布的概率? - How to calculate probability in a normal distribution given mean & standard deviation? 直方图、均值、标准差的正态分布,在数组的某个部分内 - Normal distribution of histogram, mean, standard deviation, within a certain part of an array 求出特定点距cdf的正态标准偏差和分布平均值 - Find normal standard deviation from the cdf at a specific point and the distribution mean 给定均值和标准差,生成二维正态分布 - Generate two-dimensional normal distribution given a mean and standard deviation tf.compat.v1.random_normal() 中 random_normal 的均值和标准差是什么意思? - what does mean and standard deviation in random_normal in tf.compat.v1.random_normal() mean? 绘制平均值和标准偏差 - Plot mean and standard deviation Plotly:如何将均值和标准差添加到 create_displot 图? - Plotly: How to add mean and standard deviation to a create_displot figure? Python估计数据拟合后的标准差 - Python Estimate the standard deviation after data fitting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM