# Defining a property of a variable that includes another function (sign)

I have a function that has a lot of sign functions that depend on a variable $\sigma$ and I know the property that $\sigma=1$ with probability $m$ and $-1$ with probability $1-m$. For example we can consider

s = Sign[1 + .3 σ] + Sign[.1 σ]


I want to use the property that the average over all possibilities of $\sigma$ implies $$\langle\mathrm{sgn} (a+b\cdot \sigma)\rangle _\sigma =m\cdot \mathrm{sgn} (a+b) +(1-m) \cdot\mathrm{sgn} (a-b)$$

so that in this case $\langle s\rangle$ will be simplified to $2m$. I tried to use TagSet or ReplaceAll but I don't know how to do it properly. My intuition was to try something like

σ /: Sign[a + b σ] = m Sign[a + b] + (1 - m) Sign[a - b]


and apply this property whenever I want to evaluate the average, but I can't do it.

Thanks in advance.

## 2 Answers

Maybe you can use the built-in distributions in Mathematica. For example:

σ = TransformedDistribution[2 x - 1, Distributed[x,  BinomialDistribution[1, m]];


Then, you can do things like:

Expectation[Sign[a + b x], Distributed[x, σ]]


(1 - m) Sign[a - b] + m Sign[a + b]

which recovers your expected average. For your first example:

Expectation[Sign[1 + .3 x] + Sign[.1 x], Distributed[x, σ]]


2 m

• What an elegant solution! Feb 28 '18 at 17:42

Using UpValues the 'straightforward' way:

Evaluating Sign[a_+b_ σ]^=m Sign[(a+b)]+(1-m)Sign[(a-b)] produces an UpSet::write error message, informing the user that "Tag Plus in Sign[a+b\ σ] is Protected.". This makes sense, as UpSet associates the rhs with the head of the expression in Sign, which is Plus; Plus is a built-in Protected symbol, hence the error.

Using UpValues more productively:

The failure, to produce an acceptable definition, points towards a way around the problem; replacing the head of the expression in Sign with something else, rather than Plus, seems to work.

Evaluating

Sign[wrapper[a_ + b_ σ]] ^= m Sign[(a + b)] + (1 - m) Sign[(a - b)]


doesn't produce any error messages and eg Sign[wrapper[A+B σ]] evaluates-as expected-to (1 - m) Sign[A - B] + m Sign[A + B].

A solution using UpValues:

The question needs a way to tackle expressions like s (see question). The following definition seems to do the trick:

Sign[wrapper[a_, b_]] ^:= Plus @@ Plus @@ ({m, 1 - m} Sign[{a + b, a - b}])


Evaluating the example for s, evaluates to

Sign[wrapper[{1, 0}, {0.3, 0.1}]]

 2m


An alternative route:

Using a wrapper to make UpValues work for this problem seems a bit contrived. A more natural way is to use a user-defined sign function that operates of expressions like s (see question).

ClearAll[s]
Options[s] = {Probabilities -> m};
sign[a_List, b_List, OptionsPattern[]] := With[{prob = OptionValue[Probabilities]},
Plus @@ Plus @@ ({prob, 1 - prob} Sign[{a + b, a - b}])
]


Again, seeing as there is little change in the core functionality of the code used, the example for s ie sign[{1, 0}, {0.3, 0.1}] evaluates to

2m