<div dir="ltr"><div class="gmail_extra"><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Hello everyone,</span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px">I am writing this mail to enquire about implementing numerical differentiation package in scipy.</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">There have been discussions before (Issue <a href="https://github.com/scipy/scipy/issues/2035">#2035</a>) and some PRs (PR <a href="https://github.com/scipy/scipy/pull/2835">#2835</a>) to include tools to compute derivatives in scipy.</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">According to the comments made in them, as far as I can understand, I see that there are some ways to do derivatives on the computer with varying  generality and accuracy ( by <a href="https://github.com/scipy/scipy/issues/2035#issuecomment-23628615">Alex Griffing</a> ) :</div><div style><ol style><li style><span style="font-size:12.8000001907349px">1st order derivatives of special functions</span></li><li style><span style="font-size:12.8000001907349px">derivatives of univariate functions</span></li><li style><span style="font-size:12.8000001907349px">symbolic differentiation</span></li><li style><span style="font-size:12.8000001907349px">numerical derivatives - finite differences</span></li><li style><span style="font-size:12.8000001907349px">automatic or algorithmic differentiation</span></li></ol></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Clearly, as suggested in the thread, the 1st option is already done in functions like <i>jv</i> and <i>jvp</i> in <b>scipy.special. </b></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">I think everyone agreed that symbolic derivatives is out of scope of scipy. </span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Though I would like to hear more about the univariate functions.</span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Coming to finite differences, the modules described there, <b>statsmodels </b>and <b>numdifftools, </b>they vary in aspects of speed and accuracy, in terms of approaches followed as mentioned in <a href="https://github.com/scipy/scipy/pull/2835#issuecomment-52372036">Joseph Perktold comment</a></span></div><div style><ul style><li style><span style="font-size:12.8000001907349px"><b>Statsmodels </b>used complex step derivatives, which are for first order derivatives and have only truncation error, </span><span style="font-size:12.8000001907349px">no roundoff error since there is no subtraction.</span></li><li style><span style="font-size:12.8000001907349px"><b>Numdifftools </b>uses adaptive step-size to calculate finite differences, but will suffer from dilemma to choose small step-size to reduce truncation error but at the same time avoid subtractive cancellation at too small values</span></li></ul><div><span style="font-size:12.8000001907349px">I have read the papers used by both the implementations:</span></div><div><span style="font-size:12.8000001907349px"> <b>Statsmodels </b><a href="https://drive.google.com/file/d/0BwUeCS0FJLRucXdCc1JOTEY0cGc/view?usp=sharing">Statistical applications of the complex-step method of numerical differentiation, Ridout, M.S.</a></span></div><div><span style="font-size:12.8000001907349px"> <b>Numdifftools </b>The pdf attached in the github repository <a href="https://drive.google.com/file/d/0BwUeCS0FJLRuYW1MNlp2enJCaHM/view?usp=sharing">DERIVEST.pdf</a></span></div><div><br></div><div><span style="font-size:12.8000001907349px">Just pointing out in this platform, I think there is an error in equation 13 in DERIVEST, It should be </span></div><div><span style="color:rgb(80,0,80);font-size:12.8000001907349px"><br></span></div><div><span style="color:rgb(80,0,80);font-size:12.8000001907349px">f'-0() = 2f'-delta/2() - f'-delta(),  instead of </span><span style="color:rgb(80,0,80);font-size:12.8000001907349px">f'-0() = 2f'-delta() - f'-delta/2()</span></div><span style="color:rgb(80,0,80);font-size:12.8000001907349px"><div style><span style="color:rgb(80,0,80);font-size:12.8000001907349px"><br></span></div>as also correctly mentioned in the matlab code that followed the equation</span></div><div style><font color="#500050"><span style="font-size:12.8000001907349px"><br></span></font><div><span style="font-size:12.8000001907349px">As much as my understanding from the discussions goes, the statsmodels implementation uses elegant broadcasting. Though I get the idea seeing the code, I would really appreciate some examples that clearly explain this.</span></div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">Also the complex-step method is only for first order derivatives and that function is analytic, so that Cauchy-Riemann equations are valid. So, is it possible to differentiate any function with this ?</span><br><br></div><div><span style="font-size:12.8000001907349px">Also as I was discussing with Christoph Deil, the API implementation issue of whether to use classes, as in numdifftools or as functions, as in statsmodels came to the fore. Though I am not an expert in it, I would love to hear some suggestions on it.</span></div></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Though at this point AD seems ahead of time, it is powerful in forward and reverse methods, moreover complex-step is somewhat similar to it. The packages <b>ad </b>and <b>algopy </b>use AD. Also, there were concerns with interfacing these methods with C/ Fortran functions. </span><span style="font-size:12.8000001907349px">It would also be great if there could be suggestions regarding whether to implement these methods.</span><span style="font-size:12.8000001907349px"> </span></div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">At the same time, it would be really helpful if any new methods or packages to be looked into could be suggested.</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">Waiting in anticipation for your feedback and response. Happy to learn :)</div><div style><span style="font-size:12.8000001907349px">Thanks for reading along my lengthy mail. Please do correct if I did some mistake.</span></div><div style><span style="font-size:12.8000001907349px"><br></span></div><div style><span style="font-size:12.8000001907349px">I have attached the documents I have related to these issues, most importantly <b>The Complex-Step Derivative Approximation by </b></span><span style="font-size:12.8000001907349px"><b>JOAQUIM R. R. A. MARTINS</b></span></div><div style><span style="font-size:12.8000001907349px"><b><br></b></span></div><div style><span style="font-size:12.8000001907349px"><b><a href="https://drive.google.com/folderview?id=0BwUeCS0FJLRufnJaVko3MGpJX0Nud3R0dHgyc2JBYUgxVkhBTkNvbkhFQWZucmhWSzlaVVk&usp=sharing">Numerical differentiation</a></b></span></div><div style><span style="font-size:12.8000001907349px"><br></span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Cheers,</span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">Maniteja.</span></div><div style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">______________________________</span><span style="font-size:12.8000001907349px">_________________</span><br style="font-size:12.8000001907349px"><span style="font-size:12.8000001907349px">SciPy-Dev mailing list</span><br></div><div style="font-size:12.8000001907349px"><a href="mailto:SciPy-Dev@scipy.org" style="font-size:12.8000001907349px" target="_blank">SciPy-Dev@scipy.org</a><br style="font-size:12.8000001907349px"><a href="http://mail.scipy.org/mailman/listinfo/scipy-dev" style="font-size:12.8000001907349px" target="_blank">http://mail.scipy.org/mailman/listinfo/scipy-dev</a></div></div></div>