[SciPy-user] newbie python & optimization
Daniel Davidson
dbdavidson at yahoo.com
Thu Oct 3 08:16:07 EDT 2002
Im taking a course in fixed income asset pricing and professor *strongly* suggests matlab with optimization, financial toolbox, statistics toolbox and spline toolbox (~$360 ouch). Have following questions:
* What is probability that Python with scipy will do the trick?
* Have been reading python and playing. The NumPy pdf docs I read specify a certain behavior for ufuncs, which I see until I import optimize. What causes this and can I use optimize without this happening?:
>>> from Numeric import *
>>> a = array(([1,2,3]))
>>> a*a
array([1, 4, 9])
>>> import scipy.optimize
>>> a*a
array([0, 0, 0])
I can get around this by putting Numeric in everywhere Numeric.multiply(a,a), but I would prefer not.
* I successfully use fmin to minimize a particular form function
(Nelson Siegel) to curve fit a yield to some data points. I try 3 cases and the first two smaller cases succeed (approach 0) and the last does not really. Yet my partner was able to do the 3rd one in excel solver. When I take his spreadsheat and change the 'guess' (there are four parameters) and try to solve again it also fails - so maybe this is not the algorithm to use (maybe local minima)?
* When using optimizations routines you pass it a function to evaluate. The algorithm picks numbers to put in. What is a good strategy for handling errors due to the choice (e.g. OverflowError,....)?
Any ideas or suggestions welcome.
Cordially,
Dan
PS Here is the funcion and I welcome any criticisms.
def ZHat(args, P, C):
"""
args: optimization parameterized arguments (e.g. theta's and lambda)
P: Price Matrix
C: Cashflow Matrix
"""
j=arange(1,size(P)+1)
partialPeriod = Numeric.divide(j,args[3])
expPartialPeriod = Numeric.exp(Numeric.multiply(-1.0, partialPeriod))
r = args[0] + Numeric.multiply((args[1]+args[2]),(Numeric.divide((1.0-expPartialPeriod),partialPeriod))) \
- Numeric.multiply(args[2],expPartialPeriod)
return 100.0*(Numeric.power(Numeric.divide(1.0,Numeric.add(1.0,r)),j))
def PHat(args,P,C):
zhat = ZHat(args,P,C)
return matrixmultiply(C,zhat)
globalPrevObj = 1e+100
def funcFormHelper(args, P, C):
"""
The functional form to be curve fitted.
args: optimization parameterized arguments (e.g. theta's and lambda)
P: Price Matrix
C: Cashflow Matrix
"""
try:
p_hat = PHat(args,P,C)
difference = P-p_hat
difference_sq = Numeric.power(difference,2)
obj = add.reduce(difference_sq)
except Exception:
print "Unexpected Error:", sys.exc_info()[0]
obj = YieldExtractor.globalPrevObj + 1
YieldExtractor.globalPrevObj = obj
return obj
def createFuncForm(P, C):
"""
Helper function used just to pass P, C cleanly to functional form
"""
return lambda args: funcFormHelper(args, P, C)
Invoked like:
self.optimVars = fmin(createFuncForm(self.P,self.C), \
[0.1,1.0,0,1.1], \
maxfun=100000, maxiter=100000, \
xtol=1e-10, ftol=1e-10, disp=1)
---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20021003/a79a7582/attachment.html>
More information about the SciPy-User
mailing list