Pages

Wednesday, December 21, 2016

Quantopian(Python) vs Quantmod(R)

1, Sid-by-side comparison

2, Code snippet:

    from rpy2.robjects import r
    from pandas_datareader import data, wb
    import talib
    import numpy
    import matplotlib.pyplot as plt

    IBM = r("getSymbols('IBM', src='google', from='2013-01-01')")
    f = data.DataReader(ticker,'google')
    f['SMA_50'] = talib.SMA(numpy.asarray(f['Close']), 50)
    f.plot(y= ['Close','SMA_20','SMA_50'], title='AAPL Close & Moving Averages')
    plt.show()

3. Module Installation

    conda install rpy2
    conda install -c quantopian ta-lib

    # more inteteresting modules
    conda install -c quantopian zipline
    conda install -c quantopian pyfolio
    conda install seaborn
    conda install quandl

Sunday, August 21, 2016

Covariance formula with CDF (Hoeffding's Covariance Identity)

{\displaystyle \operatorname {cov} (X,Y)=\int _{\mathbb {R} }\int _{\mathbb {R} }F_{XY}(x,y)-F_{X}(x)F_{Y}(y)dxdy}


A complete proof of above lemma can be found on page 241 (Lemma 7.27) of Quantitative Risk Management: Concepts, Techniques and Tools.

Hint: 2\(cov(X_1, X_2) = E[(X_1-\tilde{X_1})(X_2-\tilde{X_2})]\),
where \( (\tilde{X_1}, \tilde{X_2}) \) is an independent copy with the same joint distribution function as  \( (X_1, X_2) \).

Link to MathJax

Monday, August 15, 2016

Mathematical Programming and its modeling langues

Python is widely used in Mathematical Programming as a modeling language.




In commercial products, Gurobi has built its interactive shell in Python.




In open source world, Pyomo from Sandia National Lab use Python to offer an AMPL-like modeling language. Pyomo uses GLPK solver by default, but other solvers, such as GLPK, Gurobi, COIN CBC, can also be selected.




GLPK (GNU Linear Programming Toolkit) supports MathProg, which is also referred as GMPL (GNU Mathematical Programming Language). GLPK provides a command line tool glpsol, which is convenient for users to solve various optimization problems with well designed reports.




PuLP is an LP modeler written in Python. PuLP can generate MPS or LP files, and can call GLPK, COIN CLP.CBC, CPLEX and Gurobi to solve linear problems.


SolverStudio makes it easy to develop models inside Excel using Python. Data entered into the spreadsheet is automatically available to the model. SolverStudio supports PuLP, COOPR/Pyomo, AMPL, GMPL, GAMS, Gurobi, CMPL, SimPy.



Wednesday, August 10, 2016

OLS in Python

There are a few ways to perform Linear Regression(OLS) in Python.


Here is a short list of them:


1: > pandas.ols(y, x)
2: > pandas.stats.api.ols(y ,x)
3: > scipy.stats.linregress(x, y)
4: > import statsmodels.formula.api as smf
    > results = smf.ols('Close ~ Open + Volume', data = df) # df is a DataFrame



Friday, July 29, 2016

Interior Point Methods

Interior Point Methods are a class of optimization algorithms for solving linear or nonlinear programming problems.


It finds the optimum solution by moving inside the polygon rather than moving around its surface.


History:


In 1984, Karmarkar "invented" interior-point method.
In 1985, Affine-scalling method was "invented" as an intuitive version of Karmarkar's algorithm.
In 1989, it was realized that Dikin(USSR) invented Affine-scaling(Barrier method) in 1967.


Interior point method was the first practical polynomial time algorithm for solving linear programing problems. Ellipsoid method's run time is polynomial, but in practice, the Interior Point Method and variants of Simplex Methods are much faster.


Here goes technical in summary:


1. Primal objective with log barrier function: G(μ) = cx - μ Σ ln(xj)


2. Central path algorithm: μ from infinity to 0.


3. Min with constraint Ax=b?

    ∇G(μ) perpendicular to Ax=b;
    <cj-μ/xj> is linear combination of A's rows;
    <cj-μ/xj> = yA for some y;


    Let sj = μ/xj, then
          yA + s = c  ==> yA ≤  c, this the dual constraints.


4, Duality gap: cx - yb = (yA+s)x - y (Ax)
                                     = sx
                                     = nμ

5. Conversely, if all sj xj = μ, then on central path.
    To follow Central Path, use "predictor-corrector".


6. Improvement direction? "Affine-scaling"
    From current x, s, μ ==>  x+dx, s+ds, μ+dμ
                                    ==> sj dxj + xj dsj = dμ     (1)
    Also A(x+dx) = b    ==> Adx = 0                      (2)
            yA + s = c        ==> (dy)A + ds = 0           (3)


    To solve (1)-(3), rescale "affine scaling", all xj = 1 ==> sj = μ
    The equations say
           μdx + ds = 1
           Adx = 0                ==> dx  A
           (dy)A + ds = 0      ==> ds  A


    ==> project 1dμ into A and A




Note: some of the information comes from course "Advanced Algorithms" as follows:


MIT 6.854/18.415J: Advanced Algorithms (Fall 2014, David Karger)
MIT 6.854/18.415 Advanced Algorithms (Spring 2016, Ankur Moitra)

Monday, May 23, 2016

Memoization in Python

# -*- coding: utf-8 -*-
"""


File name: fib_mem.py

Created on Mon May 23 14:50:39 2016


Source: MITx: 6.00x Introduction to Computer Science and Programming

Output:  222232244629420445529739893461909967206666939096499764990979600

PEP8 Style Compliant:
In Spyder: Preferences -> Editor -> Code Introspection/Analysis,
 near the bottom right, check Style analysis (pep8).


The 2nd way to run it:
 - Comment out "@my_memoize"
 - Uncomment "fib = my_memoize(fib)"
 - Run


The 3rd way to run it:
 >> from fib_mem import fib
 >> print(fib(300))


Tested on Python 3.x
"""


##########################################
# Example 1



def my_memoize(f):
    cache = {}


    def helper(*x):  # Refer to Item 18 of "Effective Python"
        if x not in cache:
            cache[x] = f(*x)
        return cache[x]
    return helper


@my_memoize
def fib(n):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)

# fib = memoize(fib)
print(fib(300))



##########################################
# Example 2 (stack overflow)
def functionDecorator(f):
    def new_f():
        print("Begin", f.__name__)
        foo() # using f() instead
        print("End", f.__name__)
        return new_f


@functionDecorator
def foo():
    print("inside foo()")


foo()
print(foo.__name__)


###############################################################


"Python is basically pseudo code, ..." -- Brett Slatkin, The author of "Effective Python".


Saturday, May 7, 2016

Matrix Calculus

What's the partial derivatives (w.r.t. μ and Σ) of this function?

       \ln(L)= -\frac{1}{2} \ln (|\boldsymbol\Sigma|\,) -\frac{1}{2}(\mathbf{x}-\boldsymbol\mu)^{\rm T}\boldsymbol\Sigma^{-1}(\mathbf{x}-\boldsymbol\mu) - \frac{k}{2}\ln(2\pi)

Yes, it's a beautiful formula: log-likelihood function of mvn distribution.

The answer can be found on page 40 of this book: The Matrix Cookbook

You will find equation (81), (57) and (61) are useful to get the partial derivatives.


The partial derivatives are used in Vibrato Monte Carlo method, which is a Path-wise/LRM hybrid method.

Note that there are a few alternative approaches to valuate financial derivatives which have non-differentiable payoff functions.

  • Likelihood Ratio Method (LRM) 
  • Mallianvin Calculus (Stochastic Calculus of Variations)
  • "Vibrato" Monte Carlo Method


Sunday, January 24, 2016

QuantLib in C++

QuantLib is an open-source C++ Library for quantitative analysis in Finance, and the QuantLib project was started by a few Quants in 2000. Now QuantLib project is Luigi Ballabio and ferninando Ametrano.

Secondly, QuantLib has been ported to other languages:

    R: RQuantLib
    Python: PyQL
    Java: JQuantLib
    Excel: QuantLibXL

QuantLib.org provides a very good API Doc, but you may still want to take a look at other sources for API documents. The following is a short list of links for QuantLib API Docs.

QuantLib SourceCodeBrowser
QuantLib Java API Docs
QuantLib API Docs generated by Doxygen(v0.3.4)
Implementing QuantLib
C++ Design Patterns and Derivatives Pricing 2e
QuantLib on YouTube

In addition, some commercial software products are also available: QRM, FinCAD, Numerix, SunGard-FastVal, Savvysoft, Quantifi, Pricing Partners Cie, Bloomberg, Intex.

http://libguides.caltech.edu/LindeFinance

Saturday, January 9, 2016

Print a float or double in C++?

#include <iostream>
#include <bitset>
#include <cassert>

using namespace std;

int main(void)
{
const int n = sizeof(float)* 8; //32 bits
float f = 975.75;
unsigned int u;
assert(sizeof(f) == sizeof(u));
std::memcpy(&u, &f, sizeof(f));
std::cout << n << ": " << bitset<n>(u) << endl;
//32: 01000100011100111111000000000000

const int nd = sizeof(double)* 8;
double d = 975.75;
unsigned long long ull;
assert(sizeof(d) == sizeof(ull));
std::memcpy(&ull, &d, sizeof(d));
std::cout << nd << ": " << bitset<nd>(ull) << endl;
//64: 0100000010001110011111100000000000000000000000000000000000000000

std::system("pause");
return 0;
}

To confirm the conversion, please check out: http://www.binaryconvert.com/index.html