Search code examples
matlabsymbolic-mathinverse

"Unable to prove literally" error when trying to create a symbolic function based on an existing function


I need to find inverse of virtual value function of lognormal random variables. This is what I tried to do:

syms flogn(x,p1,p2)
% assume(p2<=0);             %Adding this doesn't change the error
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
glogn = finverse(flogn,x);

But I got the error:

Error using symengine
Unable to prove 'p2 <= 0' literally. Use 'isAlways' to test the statement mathematically.

Error in sym/subsindex (line 810)
                X = find(mupadmex('symobj::logical',A.s,9)) - 1;

Error in sym/privsubsasgn (line 1085)
                L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);

Error in sym/subsasgn (line 922)
            C = privsubsasgn(L,R,inds{:});

Error in logncdf>locallogncdf (line 73)
sigma(sigma <= 0) = NaN;

Error in logncdf (line 47)
[varargout{1:max(1,nargout)}] = locallogncdf(uflag,x,varargin{:});

Error in Untitled5 (line 3)
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);

I also tried with beta distribution, and got a similar error. How can I use logncdf with symbolic variables?


Solution

  • Note that you could have minimalize your sample code even more, like this:

    syms flogn(x,p1,p2)
    flogn(x,p1,p2) = logncdf(x,p1,p2);
    

    It's shorter, produces the same error and helps us to focus on the source of the error message.


    So, the error doesn't come from trying to inverse a function, but from trying to use an existing function for numerical calculations with symbolic variables.

    The error comes from the fact that you want to create a symbolic function flogn based on existing function logncdf, and logncdf has a multiple comparisons.

    With the command edit logncdf, you can read the source code of the function and see comparison at lines 73 and 76.

    % Return NaN for out of range parameters.
    sigma(sigma <= 0) = NaN;
    
    % Negative data would create complex values, which erfc cannot handle.
    x(x < 0) = 0;
    

    Matlab cannot compare symbols so it throws errors.

    Depending on what you really need, you can have different solutions.

    • Do you really need to symbolize the function flogn? Couldn't you just write it as a function then calculate the inverse of it (if it can be inversed...)?

    • If you really want to keep the symbolization, you can also rewrite your own function logncdf (with another name) so it does not have the comparisons. But it's still not guaranteed that you will find an inverse.