SERVING THE QUANTITATIVE FINANCE COMMUNITY

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

### Re: Python tricks

Hee Collector,

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.

Collector
Posts: 4651
Joined: August 21st, 2001, 12:37 pm

### Re: Python tricks

excellent! cool!

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

### Re: Python tricks

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."

Collector
Posts: 4651
Joined: August 21st, 2001, 12:37 pm

### Re: Python tricks

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

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

### Re: Python tricks

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

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

### Re: Python tricks

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/222 ... -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"

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

### Re: Python tricks

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_)) 

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

### Re: Python tricks

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.

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

### Re: Python tricks

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).

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

### Re: Python tricks

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?

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

### Re: Python tricks

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!

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

### Re: Python tricks

Stupid question:
I use VS2017 and I have all Python libraries installed, including anaconda etc.

I want to run anaconda navigator but

1. I don't see it on my desktop
2. I don't know how to run it from VS2017 (I don't even know if it is even possible)

Any ideas? Thanks!

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

### Re: Python tricks

On my machine (Windows 10) I have a folder in Start Menu called "Anaconda3 (64-bit)", in which there's a shortcut to Anaconda Navigator.

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

### Re: Python tricks

Stupid me (must be the cornflakes again) I scrolled all the way down on Start! The rationale is to use Python as a conduit to learning numerical methods. e.g PDE. C++ and  C# are a bridge too far but the Python approach is optimal

https://www.datasim.nl/onlinecourses/10 ... -libraries

Later they can port the prototypes to C++.
It feels like the good old Fortran days and NAG libraries.

Thanks!

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

### Re: Python tricks

Be careful around the memory leaks in Pandas.