0

Suppose I have have two kinds of variables $X_{\alpha_j}, X_{\beta_k}$ in the form of Subscript[x,Subscript[α,j]] and Subscript[x,Subscript[β,k]]. These two variables are embedded in a long expression for which I want to replace their symbols. Assume that I have already applied Expand to such an expression so that the terms in question are nicely isolated.

Question: How can one write an association rule, that takes in Subscript[x,Subscript[α,j]] and Subscript[x,Subscript[β,k]], and replaces them such that it satisfies all three of the following at the same time:

(1) $X_{\alpha_j} \mapsto \mu_{\alpha_j}$ and $X_{\beta_k} \mapsto \mu_{\beta_k}$; so the result should have the form Subscript[μ,Subscript[α,j]] and Subscript[μ,Subscript[β,k]], respectively.

(2) $X_{\alpha_j} X_{\beta_k} \mapsto c_{\alpha_j, \beta_k} + \mu_{\alpha_j} \mu_{\beta_k}$ (and also the same map when the terms are commuted; so $X_{\beta_k} X_{\alpha_j}$ yields the same result; so the result (and its commuted form) should have the form Plus[Times[Subscript[μ,Subscript[α,j]],Subscript[μ,Subscript[β,k]]],Subscript[c,Subscript[α,j],Subscript[β,k]]]

(3) $X_{\alpha_j}^2 \mapsto \sigma_{\alpha_j}^2 + \mu_{\alpha_j}^2$ and $X_{\beta_k}^2 \mapsto \sigma_{\beta_k}^2 + \mu_{\beta_k}^2$; so the result should have the form Plus[Power[Subscript[μ,Subscript[α,j]],2],Power[Subscript[σ,Subscript[α,j]],2]], and Plus[Power[Subscript[μ,Subscript[β,k]],2],Power[Subscript[σ,Subscript[β,k]],2]], respectively.

Remark: As one may can already guess from the notations being used, I'm simply taking a sequence of random variables $X_{\alpha_j}, X_{\beta_k}$, after taking expectations, rewriting them into their conventional mean, variance and covariances. I assume the expressions in question only involve up to the usual second moments, and hence expressions like $X_{\alpha_j}^3$ or $X_{\alpha_j} X_{\beta_k}^2$ are not of a concern.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user32416
  • 1,203
  • 8
  • 14
  • 1
    @belisarius Thanks! I should've read that a long time ago before I started this project. Unfortunately at this point, I've accumulated so much stuff that it's too difficult to rewrite things in a different symbol. But thanks for the reference, useful to keep in mind for the future. – user32416 Aug 27 '15 at 16:34

1 Answers1

2

You may try with this list of rules, in this order since your transformations are overlapping:

rules = {
    Power[Subscript[X_,Subscript[a_,j_]], n_] :>
        Power[Subscript[σ,Subscript[a,j]], n]+
        Power[Subscript[μ,Subscript[a,j]], n],
    Times[rest1___,
        Subscript[X_,Subscript[a_,j_]],
        Subscript[X_,Subscript[b_,k_]],
        rest2___
    ] :> Times[Subscript[c,Subscript[a,j],Subscript[b,k]], rest1, rest2],
    Subscript[X_,Subscript[a_,j_]] :>
         Subscript[μ,Subscript[a,j]]
}

In the multiplication case, additional terms are preserved.

The transformations are applied with:

expr /. rules

or with

expr //. rules

if multiple transformation passes are required.

LCarvalho
  • 9,233
  • 4
  • 40
  • 96
user8074
  • 1,592
  • 8
  • 7
  • Works! Thanks :) – user32416 Aug 27 '15 at 16:37
  • I'm just wondering if you could amend your code slightly? In my expr, I have other symbols that are also subscripted, say $Y_{\rho_m}$. But using your current rules, applying it to Subscript[Y,Subscript[\[Rho],m]] would change it to Subscript[\[Mu],Subscript[\[Rho],k]] as well. Is it possible to amend the code so that the subscript changes only affect those that are subscripted by X? Thanks so much! – user32416 Aug 27 '15 at 17:08
  • Oh never mind. Silly me. I just needed to remove _ everywhere where you wrote X_. Thanks again! – user32416 Aug 27 '15 at 17:10
  • Sorry for the delay: I am on holidays and do not connect ofter. Anyway you could manage the situation properly! – user8074 Aug 28 '15 at 10:00
  • Thanks again for getting back! Really helpful! – user32416 Aug 28 '15 at 17:24