【问题标题】:inferring a continuous latent parameter from a discrete observed parameter从离散的观察参数推断出连续的潜在参数
【发布时间】:2022-10-07 19:59:02
【问题描述】:

我对观察数据是连续潜在参数离散化的模型感兴趣。

作为一个简单的例子,假设你有观察J_i

在哪里

      J_i = 1 if L_i >= 1

      J_i = -1 if L_i < -1

      J_i = 0 if -1 <= L_i < 1 

L_i = \mu + \epsilon_i

我们想推断\mu

这将如何在 Stan 中实现?

【问题讨论】:

    标签: stan


    【解决方案1】:

    假设L[i] 与平均值mu 和标准差epsilon[i] 正态分布,一种方法是考虑J[i] 是从3 个类别(即-1、0、1)的分类分布中得出的,其中参数theta[i](每个长度为3),其中每个theta[i][j]是参数(mu, epsilon[i])在对应区间的正态概率分布下的区域。下面可以看到一个例子。

    因此,我们可以将theta 作为参数矩阵包含在transformed parameters 块中,而无需在Stan 模型中指定L。下面是一个示例实现。请注意,为了方便使用categorical 函数,此处将类别视为1, 2, 3 而不是-1, 0, 1

    模型.stan:

    data {
      int<lower=0> N;   // number of samples
      int J[N];         // observed values
    }
    
    parameters {
      real mu;                    // mean value to infer
      real<lower=0> epsilon[N];   // standard deviations
    }
    
    transformed parameters {
      matrix[N, 3] theta;         // parameters of categorical distributions
      for (i in 1:N) {
        theta[i, 1] = Phi((-1 - mu) / epsilon[i]);      // Area from -Inf to -1
        theta[i, 3] = 1 - Phi((1 - mu) / epsilon[i]);   // Area from 1 to Inf
        theta[i, 2] = 1 - theta[i, 1] - theta[i, 3];    // The rest of the area
      }
    }
    
    model {
      mu ~ normal(0, 10);     // prior for mu
      for (i in 1:N) {
        epsilon[i] ~ lognormal(0, 1);     // prior for epsilon[i]
        J[i] ~ categorical(to_vector(theta[i]));
      }
    }
    

    R 中的一个示例用法如下。

    主.R:

    library(rstan)
    
    set.seed(100)
    
    # simulated data
    N <- 20
    mu <- -1.2      # This is the value we want to estimate
    epsilon <- runif(N, 0.5, 2)
    L <- rnorm(N, mu, epsilon)
    J <- ifelse(L < -1, 1, ifelse(L >= 1, 3, 2))
    
    mdl <- stan("model.stan", data = list(N = N, J = J))
    
    samples <- extract(mdl, "mu")
    mu_estimate <- list(mean = mean(samples$mu), sd = sd(samples$mu))
    print(mu_estimate)
    
    # $mean
    # [1] -1.177485
    # 
    # $sd
    # [1] 0.2540879
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多