tisimst / ad Goto Github PK
View Code? Open in Web Editor NEWFast, transparent calculations of first and second-order automatic differentiation
License: BSD 3-Clause "New" or "Revised" License
Fast, transparent calculations of first and second-order automatic differentiation
License: BSD 3-Clause "New" or "Revised" License
ad supports numpy well, but doesn't work well with scipy.sparse. Many calculation involves sparse matrix. Does ad support one or plan to support?
`Collecting ad (from bundle_adjust==0.1.0.dev0)
Using cached ad-1.3.2.zip (26 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
_ python setup.py egg_info did not run successfully.
_ exit code: 1
__> [1 lines of output]
error in ad setup command: use_2to3 is invalid.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
_ Encountered error while generating package metadata.
__> See above for output.
`
How to solve it?
Hi,
Can the derivative functions, Jacobian and Hessian, produced by gh() be converted to c code by cpython for speed up? Or any other way?
Thanks.
Hello!
I am doing fresh installation of GPkit, a package that has ad
as a dependency and I encountered some issues during installation.
Collecting ad
Downloading ad-1.3.2.zip (26 kB)
ERROR: Command errored out with exit status 1:
command: /Users/philippe.kirschen/opt/anaconda3/envs/hops/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/6q/tfsts5jx6tnd_747qkgznwfwtz9v7b/T/pip-install-viy9b_j2/ad_0486c429c0824b5da1d683d85e2776af/setup.py'"'"'; __file__='"'"'/private/var/folders/6q/tfsts5jx6tnd_747qkgznwfwtz9v7b/T/pip-install-viy9b_j2/ad_0486c429c0824b5da1d683d85e2776af/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/6q/tfsts5jx6tnd_747qkgznwfwtz9v7b/T/pip-pip-egg-info-er2yb91p
cwd: /private/var/folders/6q/tfsts5jx6tnd_747qkgznwfwtz9v7b/T/pip-install-viy9b_j2/ad_0486c429c0824b5da1d683d85e2776af/
Complete output (1 lines):
error in ad setup command: use_2to3 is invalid.
----------------------------------------
pip
then went down through the versions until it got to 1.1.2 which then gave me a NameError that is presumably no longer relevant in the current versions of ad
.
Anyway as the error above suggests it seems like use of use_2to3
is the issue. A quick search suggests that setuptools>=58 breaks support for use_2to3. I checked and my freshly-installed setuptools
is version 58.0.4.
I confirmed that using setuptools<58 (57.5.0 seems like the latest version) fixes the issue on my end. Just wanted to give you a heads up!
On Mac with Python 3 I get this error:
$ python3.2 setup.py install --user
Traceback (most recent call last):
File "setup.py", line 3, in <module>
import ad
File "/Users/deil/code/ad/ad/__init__.py", line 25, in <module>
CONSTANT_TYPES = (float, int, long, complex)
NameError: name 'long' is not defined
2to3
suggests to replace long
with int
:
https://gist.github.com/cdeil/6384302
Is that OK or do you need long
for some reason or Python version?
I really like this package but unless I am misusing it I think it suffers terribly from lack of speed. I am currently trying to calculate the greeks of a portfolio of 4 call options by two methods, namely AD and finite differences. Even when using the gradient function from ad the finite differences method still outperforms the ad implementation in terms of speed by quite a bit. I am still relatively new to python, so I may just be implementing it in a way so that it is slower. I have attached my code below, any advice would be appreciated.
from ad import adnumber
from ad.admath import *
import numpy as np
import time
time_start = time.clock()
def AD_vega_singlesim(M,S,v,T,r,delta,K,S1,v1,T1,r1,K1,S2,v2,T2,r2,K2,S3,v3,T3,r3,K3):
sensi1 = np.zeros(4)
sensi = np.zeros(4)
sensi2 = np.zeros(4)
sensi3 = np.zeros(4)
for j in range(1, int(M)):
z = np.random.normal(0,1)
price = ((S_exp((r - delta - .5_(v2))T + ( v * z_sqrt(T))) - K)* exp(-r_T))/M
price1 = ((S1.exp((r1 - delta - .5*(v12))T1 + ( v1 * z_sqrt(T1))) - K1)* exp(-r1_T1))/M
price2 = ((S2_exp((r2 - delta - .5(v2__2))T2 + ( v2 * z_sqrt(T2))) - K2) exp(-r2_T2))/M
price3 = ((S3_exp((r3 - delta - .5_(v3__2))T3 + ( v3 * z_sqrt(T3))) - K3) exp(-r3*T3))/M
if price>0:
sensi += price.gradient((r,v,T,S))
if price1>0:
sensi1 += price1.gradient((r1,v1,T1,S1))
if price2>0:
sensi2 += price2.gradient((r2,v2,T2,S2))
if price3>0:
sensi3 += price3.gradient((r3,v3,T3,S3))
return sensi,sensi1,sensi2,sensi3
print AD_vega_singlesim(1000,adnumber(100),adnumber(.2),adnumber(1.0),adnumber(.05),.01,100.0,adnumber(110),adnumber(.25),adnumber(2),adnumber(.06),125,adnumber(125),adnumber(.31),adnumber(2.5),adnumber(.07),122,
adnumber(150),adnumber(.13),adnumber(1.5),adnumber(.05),143)
time_elapsed = (time.clock() - time_start)
print time_elapsed
As far as I can see ad
doesn't have any unit tests.
Can you add some?
Or maybe as a starting point the examples in README.rst
and user_guide.rst
could be run as doctests (or be re-written as unit tests)?
(with this I could help)
Consider the following code:
>>> import ad
>>> import numpy as np
>>> x = ad.adnumber(1.0)
>>> y = np.sin(x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-bb2b9cec7df6> in <module>()
----> 1 y = np.sin(x)
AttributeError: 'ADV' object has no attribute 'sin'
Typically in the past I have allowed np
to be replaced with a module like ad.math
when I needed derivatives, but I believe that the ad
package could become much simpler if the ADF
class simply grew methods like those in ad.math
. For example, the following seems to work nicely.
class ADF(object):
...
def sin(self):
return ad.admath.sin(self)
>>> import ad
>>> import numpy as np
>>> x = ad.adnumber(1.0)
>>> y = np.sin(x)
ad(0.8414709848078965)
Are there any issues to be aware of? (I don't recall seeing this behaviour before so maybe the call to self.sin
etc. is a new feature in numpy? I can't seem to find a reference to this behaviour though. I am using version 1.11.0.)
While ad
integrates with numpy array
s by becoming array
s of adnumber
s, it doesn't allow easy ways to take derivatives of the entire array. For example, let's say I have a function, and I want to plot it and its derivative:
def f(x):
return x**2 exp(-x**2)
x = adnumber(linspace(0,4,200))
y = f(x)
plot(x, y)
But now how do I plot its derivative? I can't do dy = y.d(x)
, because y
and x
are both arrays; I have to do something a list comprehension:
dy = [v.d(v2) for v,v2 in zip(y,x)]
ddy = [v.d2(v2) for v,v2 in zip(y,x)]
plot(x, y)
plot(x, dy, '--')
plot(x, ddy, ':')
That works, but it would be really nice to have a function in ad
that takes care of that, preferably by a numpy broadcasting function (so that it doesn't have to create a list, and then an array from the list).
See this notebook for an example.
P.S. Thank you so much for making this! Its an awesome library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.