stan变量声明:var_type var_name [length]和vector [length] var_name之间使用差异

发布于 2025-02-02 02:18:08 字数 390 浏览 4 评论 0原文

我是Stan的新手,我正在努力了解如何使用不同的变量声明样式的区别。特别是,我对何时应该在变量类型之后放置方括号以及何时将它们放在变量名称之后感到困惑。例如,给定int< lower = 0> l; //我的数据长度,让我们考虑:

real n [l]; //我的变量

vers

vector [l] n; //我的变量

我从我的理解中都声明了变量n为长度L的向量。 第一种方式指定变量类型的两个方法之间的唯一区别是吗?它们可以互换使用吗?他们应该属于stan代码的不同部分(例如, data vs 参数model)?

感谢您的解释!

I am new at Stan and I'm struggling to understand the difference in how different variable declaration styles are used. In particular, I am confused about when I should put square brackets after the variable type and when I should put them after the variable name. For example, given int<lower = 0> L; // length of my data, let's consider:

real N[L]; // my variable

versus

vector[L] N; // my variable

From what I understand, both declare a variable N as a vector of length L.
Is the only difference between the two that the first way specifies the variable type? Can they be used interchangeably? Should they belong do different parts of the Stan code (e.g., data vs parameters or model)?

Thanks for explaining!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2025-02-09 02:18:08

真实名称[size]vector [size]名称可以互换使用。它们的内部存储方式不同,因此您可以通过一个或另一个获得更好的性能。某些操作也可能仅限于一个和另一个操作(例如向量乘法),而循环循环的最佳顺序会更改。例如,使用矩阵与一个2-D数组,首先循环首先与列循环更有效,但是如果您有一个更具体的示例,则会出现。读取此内容的方法是:

real name[size];

Means name是类型Real的数组,因此一堆reals被存储在一起。

vector[size] name;

表示名称是大小size的向量,它也是一堆存储在一起的REAL。但是vector Stan中的数据类型基于eigen c ++库(C ++),这允许其他操作。

您还可以创建这样的向量数组:

vector[N] name[K];

它将产生k size n的矢量的数组。

底线:您可以通过使用vectorreal进行任何模型,但在计算效率中不一定等效。

real name[size] and vector[size] name can be used pretty interchangeably. They are stored differently internally, so you can get better performance with one or the other. Some operations might also be restricted to one and the other (e.g. vector multiplication) and the optimal order to loop over them changes. E.g. with a matrix vs. a 2-D array, it is more efficient to loop over rows first vs. columns first, but those will come up if you have a more specific example. The way to read this is:

real name[size];

means name is an array of type real, so a bunch of reals that are stored together.

vector[size] name;

means that name is a vector of size size, which is also a bunch of reals stored together. But the vector data type in STAN is based on the eigen c++ library (c++) and that allows for other operations.

You can also create arrays of vectors like this:

vector[N] name[K];

which is going to produce an array of K vectors of size N.

Bottom line: You can get any model running with using vector or real, but they're not necessarily equivalent in the computational efficiency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文