# Symbolic differentiation with CDF function

**URL:** <https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 5, 2018, 2:34am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720 "2018-05-05T02:34:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [May 5, 2018, 2:34am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720/1 "2018-05-05T02:34:12Z")

</div>

My code is as below:

```julia

using SymEngine, Distributions

dist = Normal(0, 1)

    
    V = PTB*cdf(dist, 1/SP*log(PTB/(K*PTB))+SP/2) - K*PT0*cdf(dist, 1/SP*log(PTB/(K*PTB))-SP/2)

    diff(V, PTB)

```

I got the error message:

```julia

ERROR: MethodError: no method matching cdf(::Distributions.Normal{Float64}, ::SymEngine.Basic)

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 5, 2018, 2:53am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720/2 "2018-05-05T02:53:38Z")

</div>

I’d follow an issue there, or look into how you can add the appropriate methods yourself.  
The derivative should of `cdf(dist, g(x))` with respect to `x` should of course be `pdf(dist, g(x))*g'(x)`, so I’d expect it to be pretty straightforward to support Distributions.jl.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [May 5, 2018, 3:40am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720/3 "2018-05-05T03:40:00Z")

</div>

Can you be more specific?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 5, 2018, 4:03am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720/4 "2018-05-05T04:03:30Z")

</div>

What is your problem?

1. Do you want an answer to the specific code you posted?
2. Do you want some algorithm that takes many derivatives of the sort you posted?
3. Or do you want to get SymEngine.jl to support Distributions.jl?
4. Other?

I don’t know the implementation details of SymEngine, but if it is “3.”, I would start by looking here:  
[https://github.com/symengine/SymEngine.jl/blob/master/src/mathfuns.jl](https://github.com/symengine/SymEngine.jl/blob/master/src/mathfuns.jl)

If it is “2.”, I would just drop the dependency on Distributions, and use SpecialFunctions instead. Eg, the cdf of a normal distribution is just (1+erf((x-mu)/sigma\*sqrt(2)))/2.

This could also work for “2”, depending on the algorithm, but definitely in case of “1”, it’d probably be easier to just apply the chain rule. Ie, you can find the derivative of

```julia
d = diff(1/SP*log(PTB/(K*PTB))+SP/2,PTB) 
@assert d == diff(1/SP*log(PTB/(K*PTB))-SP/2,PTB)

```

and just find

```julia
cdf(dist, 1/SP*log(PTB/(K*PTB))+SP/2) + PTB*pdf(dist, 1/SP*log(PTB/(K*PTB))+SP/2)*d - K*PT0*df(dist, 1/SP*log(PTB/(K*PTB))-SP/2)*d

```

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [May 5, 2018, 4:16am UTC](https://discourse.julialang.org/t/symbolic-differentiation-with-cdf-function/10720/5 "2018-05-05T04:16:28Z")

</div>

Thanks for your reply. I was wondering if SymEngine enables symbolic integration, if that it does, then I can hand code pdf and use symbolic integration to create cdf.
