【问题标题】:Stan: Local scopes in functionsStan:函数中的局部作用域
【发布时间】:2020-06-25 14:24:47
【问题描述】:

我对 Stan 还很陌生,并且通过了manual (version 2.23)。对我来说新的是不允许隐藏变量:您不能使用已在全局(即在 for 循环之外)定义的局部变量(例如在 for 循环中)(chapter 7.9, Local Variable Declarations)。

用户定义的函数也一样吗?即,您可以在用户定义的函数中声明与其他变量同名且已在函数外部声明的变量吗?在我们的例子中,我们有

functions{
real[] my_function (x) {
    real init[K*2] = some_declaration_involving_x
    return(some_other_value_involving_init[])
  }
}

transformed data {
  real init[K*6] = some_other_declaration; // initial values
}

transformed parameters {
  yet_another_variable = my_function(some_variable)
}

【问题讨论】:

    标签: scope rstan


    【解决方案1】:

    是的,测试很容易:

    functions {
      int fun() {
        int N = 1;
        return N;
      }
    }
    
    model {
      real N = 2;
      print(fun(), N);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多