SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
ISayMoo
Posts: 2054
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

September 13th, 2019, 4:41 pm

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?
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

September 15th, 2019, 1:08 pm

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..
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

September 17th, 2019, 9:46 am

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.
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

September 17th, 2019, 9:54 am

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.
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

September 17th, 2019, 2:04 pm

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)

 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

September 27th, 2019, 11:11 am

"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."
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

September 30th, 2019, 8:57 pm

"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.
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 4th, 2019, 2:47 pm

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?
 
User avatar
katastrofa
Posts: 8156
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

October 4th, 2019, 5:58 pm

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)
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 4th, 2019, 6:46 pm

  • "C++ treats you like a consenting adult. Python treats you like a naughty child. Ada treats you like a criminal"
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 4th, 2019, 7:08 pm

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.
 
User avatar
ISayMoo
Posts: 2054
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

October 5th, 2019, 1:04 pm

  • "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++" :D :D :D
 
User avatar
ISayMoo
Posts: 2054
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

October 5th, 2019, 1:07 pm

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
 
User avatar
FaridMoussaoui
Posts: 438
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

October 5th, 2019, 3:36 pm

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.
 
User avatar
Cuchulainn
Posts: 59949
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 5th, 2019, 5:06 pm

  • "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++" :D :D :D
Congratulations!
What did he say about Python in general?
ABOUT WILMOTT

PW by JB

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


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

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


GZIP: On