Mathematica 中的符号条件期望

发布于 2024-10-25 23:24:28 字数 1322 浏览 1 评论 0 原文

我想实现条件期望运算符。我将使用大写的 epsilon E 来表示运算符。我期望至少以下输入(下划线表示下标)

E_2[a]
E_2[x_1]
E_2[x_1 + y_5]
E_1[3 a + b - 4 + 2 x_]
E_6[x_5 x_7]
E_t[x_t]
E_t[3 x_{t - 1} x_{t + 2}]

产生以下输出

a
x_1
E_2[y_5] + x_1
-4 + 3 a + b + 2 E_2[x_5]
E_6[x_7] x_5
x_t
3 E_t[x_{t + 2}] x_{t - 1}

上面的示例并不是我需要生成的唯一输入/输出对,而是作为我喜欢的语法的测试和说明。

我已经走到这一步了ce 表示 Conditional Expectation,其第三个组成部分是“期望传播”是否最终确定(否则乘积规则中会出现无限递归),mv 表示 Measurable Variable 。

Notation[Subscript[E, t_][y_]  ==> ce[y_, t_, False]];
Notation[Subscript[E, t_][y_] <==  ce[y_, t_, _]];

Notation[Subscript[x_, t_] <==> mv[x_, t_]];

(* Atomic Elements and Measurable Variables *)
ce[x_, t_, _] := x /; (AtomQ[x] || Head[x] === mv && 0 <= t - x[[2]]);

(* Distribution over Addition *)
ce[x_ + y__, t_, s_] := ce[x, t, s] + Plus @@ (ce[#, t, s] & /@ {y});

(* Distribution over Product *)
ce[x__Times, t_, False] := Module[{v, m, n},

   (* All Variables in the Product *)
   v = List @@ x;

   (* Measurable Among Them *)
   m = Select[v, AtomQ[#] || Head[#] === mv && 0 <= t - #[[2]] &];

   (* The Rest is not Measurable *)
   n = Complement[v, m];

   Times[Times @@ m, ce[Times @@ n, t, True]]

];       

I want to implement the conditional expectation operator. I will use the capital epsilon E to denote the operator. I expect at least the following input (underscore means subscript)

E_2[a]
E_2[x_1]
E_2[x_1 + y_5]
E_1[3 a + b - 4 + 2 x_]
E_6[x_5 x_7]
E_t[x_t]
E_t[3 x_{t - 1} x_{t + 2}]

to produce the following output

a
x_1
E_2[y_5] + x_1
-4 + 3 a + b + 2 E_2[x_5]
E_6[x_7] x_5
x_t
3 E_t[x_{t + 2}] x_{t - 1}

The examples above are not the only input/output pairs I need to produce, but rather serve as a test and an illustration for the syntax I prefer.

I've got this far. ce means Conditional Expectation, its third component is whether the "expectation propagation" is finalized or not (otherwise the infinite recursion occurs in the product rule), mv stands for Measurable Variable.

Notation[Subscript[E, t_][y_]  ==> ce[y_, t_, False]];
Notation[Subscript[E, t_][y_] <==  ce[y_, t_, _]];

Notation[Subscript[x_, t_] <==> mv[x_, t_]];

(* Atomic Elements and Measurable Variables *)
ce[x_, t_, _] := x /; (AtomQ[x] || Head[x] === mv && 0 <= t - x[[2]]);

(* Distribution over Addition *)
ce[x_ + y__, t_, s_] := ce[x, t, s] + Plus @@ (ce[#, t, s] & /@ {y});

(* Distribution over Product *)
ce[x__Times, t_, False] := Module[{v, m, n},

   (* All Variables in the Product *)
   v = List @@ x;

   (* Measurable Among Them *)
   m = Select[v, AtomQ[#] || Head[#] === mv && 0 <= t - #[[2]] &];

   (* The Rest is not Measurable *)
   n = Complement[v, m];

   Times[Times @@ m, ce[Times @@ n, t, True]]

];       

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

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

发布评论

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

评论(1

毅然前行 2024-11-01 23:24:28

我想我可以让你接近你想要的;不过,我不会全部完成,因为这可能很棘手,但我会为您指出正确的方向。

首先,在 Mathematica 中使用下标来表示不同的变量是很棘手的,因为它将 E0 解释为 Subscript[E,0] 以及 ESubscript 均被保留。 (正如 Sjoerd 所说,E = 2.718...。)让 Mathematica 识别 作为一个独特的符号,您需要通过 < 加载 Notations 包。然后使用符号面板,Symbolize Subscript[E,0]。 (需要注意的是,如果没有使用调色板正确设置代码,请勿尝试这样做,否则它可能无法工作。)

一旦所有变量都根据需要进行了符号化,您需要设置适当的变换规则。前两个是最简单的,请输入

E_0[a] = a
E_0[x_0] = x_0

规则 3 和 4:

E_0[x_Plus]:=Distribute[E_0[x]]
E_0[x_Times]:=Distribute[E_0[x], Times]

这些是简单的规则,接下来的三个需要不同类型的关联,两者都不是 Set 也不是 SetDelayed 将在这里工作,因为正在评估的外部符号是 Dt,并且您无法将新规则与其关联,因为它是受保护。但是,有两种方法可以将此类表达式与内部符号关联起来: UpSet (^=) (或 UpSetDelayed (^:=)) 或 TagSet (/:)。我更喜欢使用 TagSet 因为它更明确,但两者都应该有效。

规则 5 和 6:

E_0 /: Dt[ E_0[ x_ ], y_ ] := E_0[ Dt[x,y] ]

这也将使您接近规则 7,但是将其与规则 3 和 4 一起添加会导致递归限制错误,因为它来回跳动试图找出如何评估它。相反,应将规则 3 和 4 替换为

E_0[x_ + y__]:= E_0[x] + Plus@@( E_0 /@ {y} )
E_0[x_ y__ ] := E_0[x] Times@@( E_0 /@ {y} )

Which Making Define Limits on the recursion。就规则 7 而言,您得到的结果

E_0[D[x_1[t_1,q_0], t_1]] E_0[Dt[t_1, y_0]] 
+ E_0[D[x_1[t_1,q_0], q_0]] E_0[Dt[q_0,y]]

Dt 规则和规则 4 的结果。让 E_0 不通过 D 进行分发code> 和 Dt 留作练习。


编辑
我想对您提供的解决方案代码发表一些评论。首先,巧妙地使用布尔值来停止递归,并且它与您的 Notation 配合得很好。不过,我建议您对产品分销进行一些更改。首先,我会使用 x__Times 而不是条件 (/; Head[x] == Times),因为它更容易阅读,而且我相信(但还没有) t 测试)它可能会更快,即处理它的开销更少。其次,将 Table 的使用替换为 List @@ x,其中 @@,称为 Apply,将 Times 替换为 < code>List,并且它再次更容易阅读和编写。对于 n 的定义,请考虑使用 补足;我不知道它是否更快,但我倾向于更喜欢此类事物的集合论构造。最后,除非您需要在使用时重新评估变量,否则不要使用 SetDelayed (:=),使用设置=)。通过使用 :=,m 被计算两次,v 被计算 3 次!

优点和缺点
这样做的主要原因是易于使用和可读性。通过定义您自己的对象及其行为方式,您可以为自己提供很大的灵活性并简化代码。仅此一点就值得了。然而,我过去很难做到这一点,而且这样的设置可能很挑剔,我建议进行彻底的测试。其次,通过添加这些额外的层,您可能会减慢代码速度,因此如果要在关键任务应用程序中使用它,请务必小心。此外,每次使用时都必须包含 Notation,并且调色板在某些时候会变得烦人。不过,可以通过在加载 Notation 包之前设置 AutoLoadNotationPalette = False 来处理调色板。

I think I can get you close to what you want; I'm not going to do it all, though, as it can be tricky, but I'll point you in the right direction.

First of all, using subscripts to denote different variables is tricky in Mathematica as it interprets E0 as Subscript[E,0] and both E and Subscript are reserved. (As Sjoerd said, E = 2.718....) To get Mathematica to recognize <anything><something> as a distinct symbol, you need to load the Notations package via <<Notations`. Then using the Notations Palette, Symbolize Subscript[E,0]. (As a word of caution, don't try to do that without using the palette to set up the code correctly, otherwise it may not work.)

Once all of your variables are symbolized, as needed, you need to set up the appropriate transformation rules. The first two are simplest, enter

E_0[a] = a
E_0[x_0] = x_0

Rule 3 and 4:

E_0[x_Plus]:=Distribute[E_0[x]]
E_0[x_Times]:=Distribute[E_0[x], Times]

Those were the easy ones, the next three require a different kind of association, neither Set nor SetDelayed will work here as the outer symbol being evaluated is Dt, and you can't associate new rules with it as it is Protected. However, there are two methods of associating such expressions with an internal symbol: UpSet (^=) (or UpSetDelayed (^:=)) or TagSet (/:). I prefer to use TagSet as it is more explicit, but either should work.

Rule 5 and 6:

E_0 /: Dt[ E_0[ x_ ], y_ ] := E_0[ Dt[x,y] ]

This will also get you close to rule 7, but adding this alongside rules 3 and 4 causes a Recursion Limit error as it bounces back and forth trying to figure out how to evaluate it. Instead, replace rule 3 and 4 with

E_0[x_ + y__]:= E_0[x] + Plus@@( E_0 /@ {y} )
E_0[x_ y__ ] := E_0[x] Times@@( E_0 /@ {y} )

Which puts definite limits on the recursion. As far as rule 7 is concerned, you get this

E_0[D[x_1[t_1,q_0], t_1]] E_0[Dt[t_1, y_0]] 
+ E_0[D[x_1[t_1,q_0], q_0]] E_0[Dt[q_0,y]]

which is a consequence of the Dt rule and rule 4. To get E_0 not to distribute over D and Dt is left as an exercise.


Edit:
I'd like to make a few comments on the solution code you've provided. First, clever use of a Boolean to stop the recursion, and it works well with your Notation. I'd suggest several changes to you product distribution, though. First, I'd use x__Times instead of the condition (/; Head[x] == Times) as it is easier to read, and I believe (but haven't tested) it may be faster, i.e. less overhead to process it. Second, replace you use of Table with List @@ x, where @@, called Apply, replaces Times with List, and it is again easier to read and write. For your definition of n, consider using Complement; I don't know if it is faster, but I tend to prefer set theoretic constructs for this type of thing. Lastly, unless you need a variable to be reevaluated whenever it is used, do not use SetDelayed (:=), use Set (=). By using :=, m is evaluated twice, and v is evaluated 3 times!

Pros and Cons:
The chief reasons to do this is ease of use and readability. By defining your own objects and how they behave, you give yourself a lot of flexibility and simplify your code. That alone makes it worth it. However, I've had difficulty doing this in the past, and such a setup can be persnickety, and I'd recommend thorough testing. Secondly, by adding these extra layers, you may slow down your code, so be careful if this is to be used in mission critical applications. Additionally, you have to include Notation every time you use it, and the palette will become annoying at some point. Although, the palette can be dealt with by setting AutoLoadNotationPalette = False prior to loading the Notation package.

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