SERVING THE QUANTITATIVE FINANCE COMMUNITY

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

### Re: Python tricks

any one use scipy.interpolate.KroghInterpolate?
a bit quaint or maybe lost in action?
Someone thought that for completeness they also must implement the algorithms which are known to be unstable?

Cuchulainn
Posts: 61553
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?
Someone thought that for completeness they also must implement the algorithms which are known to be unstable?
It's a bit of a motley crew as far as methods are concerned. I had never heard of Krogh interpolation. It might be very good but no background seems to be available.
They do have Akima for some reason and there again not others (e.g. Hyman 89)

I think the average data scientist (whatever that means) would not appreciate the subtleties of B-Splines etc. etc. Feels a bit CAD/CAM..
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: 61553
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

The Zen of Python provides aphorisms to be a good Python burger and citizen of the world::

https://www.python.org/dev/peps/pep-0020/

Some rants:

1. C++ would never tell you this: C++ are adults
2. The list is context-sensitive; does one size fit all?
3. The most important one is missing: maintainability (readability is too superficial). "Lord make me pure but not yet".
4. The yugely evil copy and paste syndrome in Python is not mentioned
5. Forcing us to use loopless code is throwing the baby out with the bathwater
6. FORTRAN code written in C++ wants explicit loops; tell me how to implement an initial boundary value problem without addressing nitty-gritty indexes?
Last edited by Cuchulainn on September 17th, 2019, 10:19 am, edited 1 time in total.
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: 61553
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

There should be one-- and preferably only one --obvious way to do it.

Wrong. Even if there be one unique solution, tell us how how to find it!

There are several solutions, one of which is optimal in a Pareto sense in the eyes of various stakeholders. Remove the blinkers.
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: 61553
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Here is a snippet of code for ADE method that I ported from C++ to Python. Semantic integrity has been preserved and the results are correct. It can be made loopless maybe but 1) have to think hard 2) is it worth it?
while current <= pde.T:

# Dirichlet boundary conditions
VNew[0] = U[0] = V[0] = pde.bcl(current)
VNew[sz-1] = U[sz-1] = V[sz-1] = pde.bcr(current)

# Up Sweep
for j in range(1,sz-1): #1..n-1
U[j] = ((U[j] * fac2) + fac*(U[j + 1] + U[j - 1])) * fac1

for j in range(sz-2, 0, -1):
V[j] = ((V[j] * fac2) + fac*(V[j + 1] + V[j - 1])) * fac1;

for j in range(1,sz-1):
VNew[j] = 0.5*(U[j] + V[j])

current += k
VOld = copy.deepcopy(VNew)


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: 61553
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

"Syntactically, lambda is a way to define a function, which may be arbitrarily complex. But due to Python syntax choice of indentation, so full lambda will need indentation, and thus cannot be a inline argument to other functions.

The other issue is that Guido doesn't like functional programing."
http://www.datasimfinancial.com
http://www.datasim.nl

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

tagoma
Topic Author
Posts: 18351
Joined: February 21st, 2010, 12:58 pm

### Re: Python tricks

"Syntactically, lambda is a way to define a function, which may be arbitrarily complex. But due to Python syntax choice of indentation, so full lambda will need indentation, and thus cannot be a inline argument to other functions.

The other issue is that Guido doesn't like functional programing."
I'm not sure I get this one. Usual disclaimers: simple mortal here.
f = lambda x: (
x,
x*2,
x*3)
print(list(f(2)))
# [2, 4, 6]
I mean there is no indentation requirement in the example above.

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

### Re: Python tricks

It was an ad-lib quote. But we should probably be sparing with too fancy lambda stuff in Python. It wasn't built for that?
http://www.datasimfinancial.com
http://www.datasim.nl

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

katastrofa
Posts: 8961
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."
print("One way to do", it)
print("Another way to do %s" % it)
print("And another way to do {}".format(it))
print("And another way to do " + it)

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

### Re: Python tricks

• "C++ treats you like a consenting adult. Python treats you like a naughty child. Ada treats you like a criminal"
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: 61553
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

As I mentioned a few times.. I find Python to be a very good medium for learning numerical methods for those who want to learn numerical methods via scipy,numpy.

Having to learn PDE and C++ is a bridge too far for most MFE students. Porting C++ FDM to Python is very easy and kind of fun as well.
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: 2292
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

• "C++ treats you like a consenting adult. Python treats you like a naughty child. Ada treats you like a criminal"
BTW, Mr Stroustroup visited my company recently, and listed one of our AI projects among his examples of "best uses of C++"

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

### Re: Python tricks

https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."
print("One way to do", it)
print("Another way to do %s" % it)
print("And another way to do {}".format(it))
print("And another way to do " + it)
I "like" this inconsistency between how core Python and Numpy assign a boolean value to a list containing one element equal zero:
bool([0])
Out[94]: True

bool(np.array([0]))
Out[95]: False

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

### Re: Python tricks

I "like" this inconsistency between how core Python and Numpy assign a boolean value to a list containing one element equal zero:
bool([0])
Out[94]: True

bool(np.array([0]))
Out[95]: False
by the very definition of the bool method:  "objects of Classes which has __bool__() or __len()__ method which returns 0 or False" is considered false
The first one is a list of length 1. The bool method on the second one is false.

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

### Re: Python tricks

• "C++ treats you like a consenting adult. Python treats you like a naughty child. Ada treats you like a criminal"
BTW, Mr Stroustroup visited my company recently, and listed one of our AI projects among his examples of "best uses of C++"
Congratulations!
What did he say about Python in general?
http://www.datasimfinancial.com
http://www.datasim.nl

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