0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ strongly related. Point 2. $\endgroup$ Commented Aug 27, 2015 at 14:32
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Aug 27, 2015 at 16:34

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Works! Thanks :) $\endgroup$ Commented Aug 27, 2015 at 16:37
  • $\begingroup$ 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! $\endgroup$ Commented Aug 27, 2015 at 17:08
  • $\begingroup$ Oh never mind. Silly me. I just needed to remove _ everywhere where you wrote X_. Thanks again! $\endgroup$ Commented Aug 27, 2015 at 17:10
  • $\begingroup$ Sorry for the delay: I am on holidays and do not connect ofter. Anyway you could manage the situation properly! $\endgroup$ Commented Aug 28, 2015 at 10:00
  • $\begingroup$ Thanks again for getting back! Really helpful! $\endgroup$ Commented Aug 28, 2015 at 17:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.