Sunday, August 28, 2016

kdb group by weekday, hour

select count i by weekday:-1+`int$date mod 7, "n"$0D01:00:00.0 xbar date+time from table

Note: 

  1. `int$date cast date to the number of days since year 2000
  2. by hour can be done with time.hh, but the above will actually return a time column instead of int column

Thursday, July 28, 2016

one-line if then else

C++
<condition> ? <if true return> : <else return>

Python
<if true return> if <condition> else <else return>

example:
rsl = f[len(prefix):] if f.startswith(prefix) else f

https://mail.python.org/pipermail/python-list/2010-July/581933.html

Wednesday, June 15, 2016

Friday, February 12, 2016

putty ssh X11 forwarding not working?

have you installed xorg-x11-xauth at the server?  (Thanks to http://praveen.kumar.in/2006/11/15/xorg-x11-xauth-needed-for-ssh-x-forwarding-to-work/)

how to allow remote server to access my local resource via ssh tunnel?

Sunday, January 31, 2016

How to install additional kernel to ipython / jupyter?

Assuming you already have python 3 and ipython installed with anaconda,

Create python 2.7 env
$ conda create -n py27 python=2.7

Activate the env
$ source activate py27

Install ipykernel
$ conda install notebook ipykernel

Write kernel spec
$ ipython kernelspec install-self --user
which should write to ~/.local/share/jupyter/kernels/python2

Deactivate 2.7 env
$ source deactivate

link the kernel spec to the location where ipython will read
$ cd ~/.ipython/
$ ln -s ~/.local/share/jupyter/kernels/

Confirm now that we have additional kernel
$ ipython kernelspec list

Start ipython notebook and you should now have both kernels
$ ipython notebook --no-browser

Wednesday, December 23, 2015

How to interpret Beta?

How to interpret beta?

sensitivity of the expected excess asset returns, E(Ri) - Rf, to the expected excess market returns, E(Rm) - Rf
so, beta = (E(Ri) - Rf) / (E(Rm) - Rf)

in other words, beta tells u how many times your instrument should return above rfr given the market return above rfr

say the market return = 5%, rfr = 2%, and your stock has a 1.5 beta
beta tells you that your instrument should be getting 6.5% return
if your instrument returns above 6.5%, ALPHA!!  :D
if below, then why are u investing in that instrument rather than the market portfolio?


how to calculate beta?

Cov(Ri, Rm)/Var(Rm)
= (corr(Ri, Rm) * sigma i * sigma m) / (sigma m)^2
= corr(Ri, Rm) * sigma i / sigma m


putting in back to capm
Ri = rfr + beta * (Rm - rfr)
Ri - rfr = corr(Ri, Rm) * sigma i * (Rm-rfr)/sigma m
(Ri -rfr) /sigma i = corr(Ri, Rm) * (Rm-rfr)/sigma m

now we can interpret the corr(Ri, Rm) as
the ratio between sharpe ratio of the instrument and the sharpe ratio of the market


what is variance?
avg squared deviation from mean
= E[(X-E[X])^2]

can also be thought of as the covariance with itself
= Cov(X,X)
= E[(X-E[X])(X-E[X])]

which brings us to the covariance of 2 diff variables
Cov(X,Y)
= E[(X-E[X])(Y-E[Y])]

Tuesday, December 8, 2015

setup linux vm in 64-bit Windows host without installation

Download binary from lassauge.free.fr/qemu and extract

Create a 40G hard disk by running
qemu-img create hd.img 40G

Install by Linux os with
qemu-system-x86_64 \
-drive file=hd.img,index=0,media=disk,format=raw \
-L Bios -m 1024 \
-cdrom fedora-install.iso


Start your vm with ssh port forwarded with
qemu-system-x86_64 \
-drive file=hd.img,index=0,media=disk,format=raw \
-L Bios -m 1024 \
-redirect tcp:2222::22

ssh -p 2222 localhost
to get to your vm