0
$\begingroup$

I have defined the following function:

g = Compile[{{x0, _Real}},
    sol = NSolve[
        Rationalize[CDF[ExponentialDistribution[1], x] == x0, 10^-10],
        {x}, Reals];
   sol]

Running g[0.5] it fails with:

In[460]:= g[0.5]

During evaluation of In[460]:= CompiledFunction::cfse: Compiled expression {{x->0.693147}} should be a machine-size integer. >>

During evaluation of In[460]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 2; proceeding with uncompiled evaluation. >>

Out[460]= {{x -> 0.693147}}

If I try to extract the rhs from the transformation rule with sol[[1,1,2]] I get the following error:

Compile::part: Part specification sol[[1,1,2]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>

Compile::part: Part specification {{x->0.693147}}[[1,1,2]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>

How could I let Compile[] know that I intend to return {{a -> b}} or how could I extract the rhs of the transformation rule and return that only?

EDIT: It appears that none of my functions (Rationalize[], NSolve[], ExponentialDistribution[]) is "compilable" based on this link by @MichaelE2.

$\endgroup$
2

1 Answer 1

1
$\begingroup$

Just some observations (which may be counter the intention): 1. You could use in-built function: Quantile:

qf[x_] := Quantile[ExponentialDistribution[1], x]

qf[0.5] yields 0.693147

The uncompiled function could be done:

func[x0_] := 
 First[x /. 
   Quiet@NSolve[CDF[ExponentialDistribution[1], x] == x0, x, Reals]]

In the preceding I have not used `Rationalize. I was uncertain what the aim of its use was. If the aim is to express the quantile as a raitonal approximation than it can be applied post. If it was to aid calculation the compiled version seems problematic to me.

$\endgroup$
2
  • $\begingroup$ Thanks for the Quantile[] hint @ubpdqn! I will try it and it see if it speeds things up, because otherwise my function is veeery slow :( It takes ~2 secs for 200 samples. $\endgroup$
    – stathisk
    Commented Oct 25, 2013 at 18:36
  • 1
    $\begingroup$ @Zet There is also InverseCDF[ExponentialDistribution[1], x0], which probably has the same underlying function as Quantile for a distribution. $\endgroup$
    – Michael E2
    Commented Oct 25, 2013 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.