Thursday, October 1, 2015

Friday, September 18, 2015

How to read command line output directly into pandas dataframe?

cmd = r"zgrep abc application.log | perl -pe 's/pattern/subs/'"
# python 2
pd.read_csv(StringIO.StringIO(subprocess.check_output(cmd, shell=True)))
# python 3
pd.read_csv(BytesIO(subprocess.check_output(cmd, shell=True)))

Wednesday, August 26, 2015

From MySQL to pandas df with Python 3

Install mysql connector 


# http://conda.pydata.org/docs/faq.html#id1
conda install -n <your python 3 env> mysql-connector-python

Access MySQL from python 3 with mysql connector and put result into pd df

# http://dev.mysql.com/doc/connector-python/en/connector-python-tutorial-cursorbuffered.html

import mysql.connector

# Connect with the MySQL Server
cnx = mysql.connector.connect(user='scott', database='employees')

# note that we'll have to set dictionary=True to get column name into pd and fetchall afterwards
cur = cnx.cursor(buffered=True, dictionary=True)
cur.execute('SELECT now() from dual')
pd.DataFrame(cur.fetchall())

Sunday, August 16, 2015

ipython / jupyter - how to switch kernel?

With ipython and python 2.7 installed using anaconda, how do I switch kernel to use 3.*?

$ conda create -n py34 python=3.4 anaconda
$ source activate py34
$ ipython kernelspec install-self --user
$ ipython notebook --profile=nbserver --script

Tuesday, July 14, 2015

Tuesday, May 12, 2015

eigenvalue and eigenvector of a matrix (and why we bother)

These 2 links give a good review on it:
http://tutorial.math.lamar.edu/Classes/DE/LA_Eigen.aspx
https://www.math.hmc.edu/calculus/tutorials/eigenstuff/

say we've a matrix A, if we can satisfy this:
A*v_e = lambda*v_e

v_e = eigen vector of matrix A
lambda = eigen value of matrix A


example
|  2  7 |   | -1 |         | -1 |
| -1 -6 | * |  1 | = -5 *  |  1 |


why do we even need this?
see http://math.stackexchange.com/questions/23312/what-is-the-importance-of-eigenvalues-eigenvectors
in a nutshell, it allows us to transform from standard basis, which is sometimes computationally intensive to a different basis to work in, one which simplifies the calculations necessary"

taylor's series application

say we wanna know f(x1), but we only know
  • x1-x0 is small,
  • f(x0),
  • f'(x0), ie first derivative
  • f''(x0), ie 2nd derivative
  • higher order of derivatives, etc.
, what do we do?

using taylor's series, we can estimate by
f(x1) = f(x0) + (x1-x0)*f'(x0)/1! + (x1-x0)*f''(x0)/2! + ...

:D

a concrete example, say,

  • with the current underlying price, x0,
  • we calculate an option's value, f(x0),
  • the associated delta, f'(x0),
  • gamma, f''(x0)

if the underlying price moves a little bit from x0 to x1, how do we estimate the new option price, f(x1), without going through the option pricing model?