SERVING THE QUANTITATIVE FINANCE COMMUNITY

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Undocumented code?(?)
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

np.allclose() is kind of handy
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98
for n in range(N):
ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

# Go to next level
x += h;
y = ynP1;

return ynP1

OR

for n in range(N):
ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

# Go to next level
x += h;
y = ynP1;

return ynP1

http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

FaridMoussaoui
Posts: 507
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

my everything editor is emacs. The (right) tabing is done automagically through plugins.
For python, I use Tim Peters plugin available here https://launchpad.net/python-mode
Last edited by FaridMoussaoui on September 6th, 2019, 7:30 am, edited 1 time in total.

ISayMoo
Posts: 2275
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98
for n in range(N):
ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

# Go to next level
x += h;
y = ynP1;

return ynP1

OR

for n in range(N):
ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

# Go to next level
x += h;
y = ynP1;

return ynP1

The first one would not pass the linter. No indentation without an if, for etc.

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
Named parameters are a godsend. Boost has them but not {C++11,C++20]??
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98

The first one would not pass the linter. No indentation without an if, for etc.
Can we see the linter as a pre-compiler that we need to 'execute' before running?
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

ISayMoo
Posts: 2275
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

Yes, And mypy for type annotations.

ISayMoo
Posts: 2275
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
I am sure you know this library: https://nlopt.readthedocs.io/en/latest/

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
I am sure you know this library: https://nlopt.readthedocs.io/en/latest/
Cool. I installed it from Python and ran a POC test. Looks good. Just that

return -1

not work while (invalid argument)

return -1.0

did.

hmmm..
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

FaridMoussaoui
Posts: 507
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

If you check the source code on github,  it expects a double as an output for the function. If not,  it will throw an invalid argument error message.

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

If you check the source code on github,  it expects a double as an output for the function. If not,  it will throw an invalid argument error message.
I used 1. instead of 1 myself (in FORTRAN REAL*x = 1.0,  x .EQ. 1 always FALSE afair), newbies might get confused. I wonder where and by whom this type checking takes place?
I manage to pip nlopt directly.
Looks like a nice library.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

FaridMoussaoui
Posts: 507
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

Looking to source code, you can also see how to use SWIG to interface the C library to python.

If possible, I always install "packages" from source code. That's a good practise I learned working on Linux.

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Looking to source code, you can also see how to use SWIG to interface the C library to python.

If possible, I always install "packages" from source code. That's a good practise I learned working on Linux.
Good point.

Having precise black-box contract specifications would obviate the need for source code inspection.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

any one use scipy.interpolate.KroghInterpolate?
a bit quaint or maybe lost in action?
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...

 JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...

GZIP: On