SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 24th, 2018, 4:37 pm

Hee Collector,

instead of
import math
...
math.sqrt(5.0)


you can also import a set of functions that are inside math and then drop the "math." prepending
from math import sqrt, exp, log, pow
...
sqrt(5.0)


you can also import all functions that are inside math 
from math import *
...
sqrt(5.0)


I would do the middle version. The first is indeed too verbose. The last imports lots of stuff, and maybe it import some "bs" function who's name clashes with a function  you made yourself. With the middle one you'll have an explicit list of functions you've imported from math.
 
User avatar
Collector
Posts: 3870
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 4:46 pm

excellent! cool!
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 24th, 2018, 4:48 pm

Cool!

I found another little thing:  :-)

I see you sometimes using "2" (integer) and sometimes "2." (float).  The "2." is shorthand for "2.0" and it's telling python that you want a float there instead of an integer. In python 2 division of integers (the "2" version) will return an integer. So 10/4 will return 2! Division of floats will however return a float, 10. / 4. = 2.5. This can cause confusion, and this was changes in python 3. In python 3 both 10/4 and 10.0/4.0 will give 2.5

.. so if you want your code to run on python 2, and if you want 10/4 to return 2.5 then you can do two things
1) replace "2" with "2." or "2.0" to make the constants floats instead of integers. You do this sometimes, but sometimes not.
2) specify at the top that you want the python 3 behaviour for your code even when run in python 2, like this:

from __future__ import division

print(10/4)

>> 2.5

..anyways, .. this issue is the reason that you sometimes see people with "2" as "2."
 
User avatar
Collector
Posts: 3870
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 5:08 pm

pretty good, and I see even the factorial function seems to work well at least for n=10000, not light speed, but nice language
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 24th, 2018, 7:06 pm

Collector wrote:
pretty good, and I see even the factorial function seems to work well at least for n=10000, not light speed, but nice language

There is a nice "arbitrary precision" library for python which is fast:

 http://mpmath.org/doc/current/functions/gamma.html
 
User avatar
Cuchulainn
Posts: 56926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

February 24th, 2018, 8:11 pm

Collector wrote:
Cuchulainn wrote:
What not just use Excel-DNA (does it work on Mac?). Piece of cake.

 not so promising either
"I have solved this problem with ExcelDNA. As stated in the ExcelDNA forums it does not support Mac for Office, however there is a workaround. Install Wine for OSX and then run Microsoft Office 2003 or 2010 within it. I used a commercial distribution of Wine called Crossover and it works well. The downside of this approach is that you must install Wine on every target for your AddIn."  https://stackoverflow.com/questions/22212833/f-or-c-sharp-based-excel-add-in-on-osx

The end. Why not install VM on  top of OSX or just buy a Windows machine with C#. Just sayin'

BYW if you still have Apple II then this is

https://en.wikipedia.org/wiki/VisiCalc

"Microsoft products work well together"
 
User avatar
tagoma
Topic Author
Posts: 18233
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 24th, 2018, 9:14 pm

And if you remain nostalgic about more demanding programming languages à la C++, the following shall work:

import tensorflow as tf
val_ = tf.constant(5.0)
sqrt_ = tf.sqrt(val_)
with tf.Session() as sess:
    print(sess.run(sqrt_)) 
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 24th, 2018, 9:53 pm

Thats a very good minimalistic intro into using Tensorflow in Python!

You don't easily see it, but tf builds a computational graph of operations on tensors and dependencies between them.  Typically you feed values in the graph and then recompute values that depend on them.

E.g. if you replace val_ with 16 and compute sqrt_ again you get 4

with tf.Session() as sess:
    print(sess.run(sqrt_, feed_dict={val_:16}))


Tensorflows strength is high performance and cross platform linear algebra and automatic differentiation, it also runs on GPU.

If you had to compute the implied vol of 100.000 Barrier Exchange Options then tf would perform really well. It would run on the GPU or parallel cores, and you could use automatic differentiation to solve for the implied.
 
User avatar
tagoma
Topic Author
Posts: 18233
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 24th, 2018, 10:48 pm

outrun wrote:
Thats a very good minimalistic intro into using Tensorflow in Python!

You don't easily see it, but tf builds a computational graph of operations on tensors and dependencies between them.  Typically you feed values in the graph and then recompute values that depend on them.

E.g. if you replace val_ with 16 and compute sqrt_ again you get 4

with tf.Session() as sess:
    print(sess.run(sqrt_, feed_dict={val_:16}))


Tensorflows strength is high performance and cross platform linear algebra and automatic differentiation, it also runs on GPU.

If you had to compute the implied vol of 100.000 Barrier Exchange Options then tf would perform really well. It would run on the GPU or parallel cores, and you could use automatic differentiation to solve for the implied.

Yes, I wrote it ugly purposefully! I like this intro on tensorflow.
 While we are at it, do you advise to go for eager execution?  I admit I am a bit confused as one cannot use e.g. placeholders with it (which I think makes sense).
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 25th, 2018, 10:34 am

I haven't used that because it's a recent feature aimed at are more interactive experience which I don't need.

I think it was added to offer the same experience as Pytorch, which is growing in popularity?
 
User avatar
Cuchulainn
Posts: 56926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

April 16th, 2018, 9:18 am

  1. I am looking for books on Numpy and Scipy, with focus on the numerical algorithms and background. I am not interested in having to wade in syntax before getting to these topics.

    Any suggestions? Thx! 

ABOUT WILMOTT

PW by JB

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


JOBS BOARD

JOBS BOARD

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