SERVING THE QUANTITATIVE FINANCE COMMUNITY

  • 1
  • 2
  • 3
  • 4
  • 5
  • 16
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

October 18th, 2017, 8:32 pm

oops. posted in the wrong thread!

or not?

you can use .resample('Q").interpolate('cubic')  (or instead of cubic pick a different spline type)

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

Re: Python tricks

October 22nd, 2017, 9:21 pm

This one is quite handy
  1. I have downloaded

    1. Text editor Anaconda
    2. Visual Studio Python

    2. has nice IDE but I don't know how to install NumPy etc.
    1. looks complete + all libraries but is a text editor (which is not to nice).

    Ideally, I want to use both options in a Visual IDE. Any ideas?
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 10th, 2018, 11:58 am

This one is quite handy
  1. I have downloaded

    1. Text editor Anaconda
    2. Visual Studio Python

    2. has nice IDE but I don't know how to install NumPy etc.
    1. looks complete + all libraries but is a text editor (which is not to nice).

    Ideally, I want to use both options in a Visual IDE. Any ideas?
Sorry, I missed this one. Fortunately, you're now all set up.
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 10th, 2018, 12:29 pm

Is there an elegant way to order (asc, desc) a dictionary by values that wouldn't require any import?
dic = {'Raul' :  0.8, 'tag' : 100.0, 'Cich' : 1.1, 'outrum' : 0.6}
sorted(dic.items(), key=operator.itemgetter(1), reverse=False)
#[('outrum', 0.6), ('Raul', 0.8), ('Cich', 1.1), ('tag', 100.0)]
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: Python tricks

February 10th, 2018, 9:10 pm

Is there an elegant way to order (asc, desc) a dictionary by values that wouldn't require any import?
dic = {'Raul' :  0.8, 'tag' : 100.0, 'Cich' : 1.1, 'outrum' : 0.6}
sorted(dic.items(), key=operator.itemgetter(1), reverse=False)
#[('outrum', 0.6), ('Raul', 0.8), ('Cich', 1.1), ('tag', 100.0)]
I think like this?
sorted(dic.items(), key=lambda kv: kv[1] )
sorted(dic.items(), key=lambda kv: kv[1], reverse=True)
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 10th, 2018, 9:29 pm

Is there an elegant way to order (asc, desc) a dictionary by values that wouldn't require any import?
dic = {'Raul' :  0.8, 'tag' : 100.0, 'Cich' : 1.1, 'outrum' : 0.6}
sorted(dic.items(), key=operator.itemgetter(1), reverse=False)
#[('outrum', 0.6), ('Raul', 0.8), ('Cich', 1.1), ('tag', 100.0)]
I think like this?
sorted(dic.items(), key=lambda kv: kv[1] )
sorted(dic.items(), key=lambda kv: kv[1], reverse=True)
Nice! I had not thought about lambda here. Thank you very much, indeed.
 
User avatar
Cuchulainn
Posts: 59940
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

February 19th, 2018, 9:05 pm

Is there an elegant way to order (asc, desc) a dictionary by values that wouldn't require any import?
dic = {'Raul' :  0.8, 'tag' : 100.0, 'Cich' : 1.1, 'outrum' : 0.6}
sorted(dic.items(), key=operator.itemgetter(1), reverse=False)
#[('outrum', 0.6), ('Raul', 0.8), ('Cich', 1.1), ('tag', 100.0)]
What about using XML text and using the XML DOM API. Does Python support this? Anyway, C# is like so
https://stackoverflow.com/questions/949 ... -innertext

maybe XSLT
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

February 19th, 2018, 9:34 pm

Is there an elegant way to order (asc, desc) a dictionary by values that wouldn't require any import?
dic = {'Raul' :  0.8, 'tag' : 100.0, 'Cich' : 1.1, 'outrum' : 0.6}
sorted(dic.items(), key=operator.itemgetter(1), reverse=False)
#[('outrum', 0.6), ('Raul', 0.8), ('Cich', 1.1), ('tag', 100.0)]
What about using XML text and using the XML DOM API. Does Python support this? Anyway, C# is like so
https://stackoverflow.com/questions/949 ... -innertext

maybe XSLT
In my use-case -- which involves small amount of data -- data quite naturally come as a (Python) dictionary.
BTW, Python can natively handle xml manipulations, such as re-ordering, e.g. this example on SO.
 
User avatar
Cuchulainn
Posts: 59940
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

February 20th, 2018, 10:36 am

Nice. XML is language-independent. I reckon a Python-generated XML file can be read by C++/C#.
 
User avatar
Collector
Posts: 4261
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 9:30 am

    I just started building a small library in python, my first function seems to spit out correct values, any suggestion for improvements apprichiated.

Must I use the math function every time calling on for example sqrt?, if so this is irritating. (of course I could build a new sqrt function etc).


   
import math

    # Barrier Exchange Option based on  Haug-Haug: Knock-in/out Margrabe
def KnockInExcahngeOneAssetForAnother(CallPutFlag,S1,S2,H,T,r,b1,b2,v1,v2,rho):

    v=math.sqrt(v1*v1+v2*v2-2*rho*v1*v2)
    b=b1-b2
    S=S1/S2

    d1 = (math.log(S)+(b+v*v/2)*T)/(v*math.sqrt(T))
    d2 = d1-v*math.sqrt(T)
    
    
    if CallPutFlag=='c':
        return S2*math.exp((b2-r)*T)*math.pow(S/H,1-2*b/(v*v))*GeneralisedBT('c',H*H/S,1,T,0,b,v)
    else:
        return S2*math.exp((b2-r)*T)*math.pow(S/H,1-2*b/(v*v))*GeneralisedBT('p',H*H/S,1,T,0,b,v)

   #    Generalized BT Function
def GeneralisedBT(CallPutFlag,S,X,T,r,b,v):

    d1 = (math.log(S/X)+(b+v*v/2.)*T)/(v*math.sqrt(T))
    d2 = d1-v*math.sqrt(T)
    
    if CallPutFlag=='c':
        return S*math.exp((b-r)*T)*CND(d1)-X*math.exp(-r*T)*CND(d2)
    else:
        return X*math.exp(-r*T)*CND(-d2)-S*math.exp((b-r)*T)*CND(-d1)
Last edited by Collector on February 24th, 2018, 11:48 pm, edited 2 times in total.
 
User avatar
Collector
Posts: 4261
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 10:47 am

xlwings seems pretty cool to interfacing python and excel.
and nice it is compatible with Mac. So do this means much easier to link phyton functions into excel for Mac than C++? When I last time built a C++ function library I had to build the excel add-ins for my pc and was not able to use it on my Mac (at least not with my limited skills).
 
User avatar
Cuchulainn
Posts: 59940
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

February 24th, 2018, 11:50 am

Must I use the math function every time calling on for example sqrt?, if so this is irritating. (of course I could build a new sqrt function etc).

If sqrt(T) is called in a loop it might be an idea to put it in a variable outside the loop. Maybe Python is clever enough (optimising compiler).
 
User avatar
Collector
Posts: 4261
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 12:56 pm

installed xlwings on my Mac, but looks like I was overoptimistic with respect to building Mac functions?

"3. UDFs: User Defined Functions (Windows only)" 

http://docs.xlwings.org/en/stable/udfs.html
Last edited by Collector on February 24th, 2018, 1:02 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 59940
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

February 24th, 2018, 1:02 pm

Are you pine-ing for the Mac fjords?
What not just use Excel-DNA (does it work on Mac?). Piece of cake.
C++ with Excel is a pain. I wrap it in C++/CLI. So easy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ExcelDna.Integration;
using ExcelDNA_CPPCLI; // Legacy C++ wrapper

namespace ExcelDNA
{
    public static class MyFunctions
    { // This is now a proxy to Excel; the real stuff is C++

        [ExcelFunction(Description = "My first")]
        public static string SayHello(string name)
        {
            //  return "Hello, there " + name;
            return MyFunctionsCLI.SayHelloCLI(name);
        }

        [ExcelFunction(Description = "My Sum")]
        public static string MySum()
        {
                double sum = MyFunctionsCLI.MySum();
                string result = sum.ToString();

                return result;
         }

        [ExcelFunction(Description = "BS")]
        public static double CallPrice(double K, double T, double r, double v, double S)
        {
            return MyFunctionsCLI.CallPrice(K, T, r, v, S);
        }
    }
}

 
User avatar
Collector
Posts: 4261
Joined: August 21st, 2001, 12:37 pm

Re: Python tricks

February 24th, 2018, 1:17 pm

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