1
votes

It is a bit hard to explain this in the title, so let's say that I have the standard sigmoid function in a symbolic expression and its derivative

f = 1/(1+exp(-x))
f2 = exp(-x)/(1+exp(-x))^2

again easily obtainable as symbolic expression. What I want is somehow to check that in fact f2=f1(1-f1), e.g., (which is always the case for any injective function f), but do this automatically.

1
Do you want to check if such a relation exists, or to actually find it? For the former, an (imperfect) method is to sample (f2(x)-f2(y))/(f1(x)-f1(y)) over various x,y; if the ratio stays bounded, then f2 is a (Lipschitz) function of f1. This does not detect non-Lipschitz dependencies such as f2=sqrt(f1).user3717023
I need to actually find it, and I hope there is a symbolic way for doing this. The main idea is to analyse the expression simplified and find out given the fact that I need to evaluate f, should I use f2(x) or f2(f) as a faster numerical calculation, potentially and in terms of numerical stabilityAlex Botev
Basically, you want to eliminate x from the two equations. I don't have Matlab symbolic toolbox, but I have Maple which is somewhat similar. In Maple, it's eliminate([f = 1/(1+exp(-x)), f2 = exp(-x)/(1+exp(-x))^2], x) and the output is f2 + f^2 - f = 0 which is the sort of result you wanted.user3717023
For functions that matlab symbolic toolbox can handle in a good way (the example is one of these), you may be able to use symbolic arithmetics: f3=f2+f^2-f; simpify(f3). I am not sure how the proposal from @FamousBlueRaincoat can be implemented in matlab, but if that works it seems as a good solution as well.patrik
I checked and in the symbolic toolbox unfortantely does not have this capability. And you are right that it might simplify it, but what essentially I would like to do is to have only an analytical expression for f, than use the symbolic toolbox to calculate Jf (jacobian) and have a way of analysing whether the Jacobian is easier to compute from the values of x or from the values of f(x), since I would be computing both f(x) and Jf(x).Alex Botev

1 Answers

1
votes

At least in this case you should be able to do this just using subs, which works for expressions as well as variables:

syms x f1;
f = 1/(1+exp(-x))
f2 = exp(-x)/(1+exp(-x))^2
f2 = subs(f2,f,f1)

which in R2014b and R2015a returns f1^2*exp(-x). For more complex expression this may not be completely robust or may need to be applied more than once or in conjunction with simplify.

If you just need to compare expressions, you can use sym/isequaln or isAlways:

syms x;
f = 1/(1+exp(-x));
f2 = exp(-x)/(1+exp(-x))^2;
isequaln(f*(1-f),f2)
isAlways(f*(1-f)==f2)

However, both of these return Boolean false (0). for some reason. This may be a bug or maybe it's because the to two forms have different singularities when evaluated at -Inf. The following

syms x;
f = 1/(1+exp(-x));
f2 = exp(-x)/(1+exp(-x))^2;
subs(f2,x,-Inf)
subs(f*(1-f),x,-Inf)

yields NaN and 0, respectively. Taking the limit at -Inf does yield the correct values, however. The documentation does not indicate how sym/isequaln and/or isAlways handle this situation. This might be a good case to file a service request with The MathWorks.