Wednesday, January 28, 2015

SQL database / stdout to kdb

// using mysql as an example
// perhaps a good way to just have parsed output (say from perl) to directly go into kdb

q)(::)test:("*TH"; enlist "\t") 0:system "mysql -u root -e 'select \"string\" str, time(now()) time, 99 short from dual' "
str      time         short
---------------------------
"string" 15:55:11.000 99  
 

q)meta test
c    | t f a
-----| -----
str  | C   
time | t   
short| h    

Wednesday, January 21, 2015

kdb load csv into splayed table with column type of character string (not symbol)

// using string, ie list of char, instead of symbol for some feedcode
// since domain is not limited with growing expiries
// http://code.kx.com/wiki/JB:KdbplusForMortals/splayed_tables#1.2.7.3_Symbols_vs._Strings
// note that the entire symbol file is loaded into memory

// To load csv into table of list type, use *
(::)Cols:("SNHS*SH"; "|") 0:`$"/text/file.pipe"
//(::)t: flip ColNames!Cols
// .Q.en is to enumerate the symbol columns
`:/kdb/2000.01.01/table/ set .Q.en[`:/kdb;] flip ColNames!Cols
delete Cols from `.

// load the splayed table
\l /kdb

Thursday, December 25, 2014

bayes theorem - an intuitive explanation



there are good attempts in explaining bayes theorem, such as yudkowsky (long), betterexplained.com (shorter) and wiki.  Here, I'll try to show how we can apply it (or rather, avoid the pitfall of not applying it) basing on those.

Let's get to a simplified version of a common example - test detecting drug use.
In hk, some high schools are considering mandatory test to detect drug use.

HYPOTHETICALLY, in hk, 0.1% students are drug users. Let's say we have a test that can detect drug use with 90% accuracy. I have a student here tested positive (meaning test says this student is a drug user). What's the chance of this student being a drug user?

90%, right? The test is 90% accurate, so that's gotta be the answer, right?

No. No. No.

The 1st objective of this post is to prevent you from ever giving this wrong answer again, even if u aren't interested in learning how to get the right answer. 

To let u recognize how absurd that answer was, imagine I now have 100 students and I know 99 of them are drug users.
If I randomly pick a student out of this group, we can all agree that there's a 99% chance that this student is a drug user.
Now, if I randomly pick a student out of this group to have him take the same drug test as before and the test result is positive, what's the chance of this student being a drug user?

It's 90% with the rationale (well, the test is 90% accurate) fron the previous answer.
U see now how absurd the answer is, right?
Basically,

  • we know the student has a 99% chance of being a drug user
  • the drug test (90% accurate) he takes confirms it
  • yet, we actually DECREASE his probability being a drug user from 99% to 90% after the test!
That's absurd!

INTUITIVELY, given that we know the student has a 99% chance of being a drug user (prior), a positive result from a reasonably accurate test (90% in this case) confirming the prior should SLIDE the probability even higher (say, 99.xx%) and we should be more sure of this student being a drug user with the test result.

Hopefully, this example will prevent us giving another seemingly logical, but completely absurd answer again.

Essentially, this is saying 

  • P(tested +| drug user) != P(drug user | tested +)
Will elaborate further in next post.

Sunday, December 14, 2014

how do we valuate a call option?

(post from old site)

1)by establishing a hedged portfolio (ie a portfolio value will not be affected by the underlying price) and work backward!!
2) risk-neutral valuation

1)
for example, we have 
an underlying with price @t0 = 100
a call option that can be exercised @t1 at 100
what is the call option worth at t0?

hedged portfolio: buy a fraction of the underlying and sell the call
(hedge ratio)*Underlying - c
h*u-c

let's assume @t1, underlying price will either go up by 15 to 115 (u=1.15) or down by 5 to 95 (d=0.95)
if up
underlying is worth 115
call option is worth 15
if down
underlying is worth 95
call option is worth 0

h=? if we want the portfolio to have the same value no matter the underlying goes up or down?

h*100*1.15-15 = h*100*.95-0
h = (15-0)/[100(1.15-0.95)]
h= 15/20 = 0.75

=> it means that if we establish our portfolio at t0 by buying 0.75 share of the underlying and selling the call option @t0, then
@t1, our portfolio will be worth 71.25 regardless of the underlying price going up or down
up = 0.75*100*1.15 -15 = 71.25
down = 0.75*100 *0.95 = 71.25

ok, so what does the above have anything to do with the call price at t0?
since the hedged portfolio will be worth $71.25 @t1 without RISK (ie, no matter the price going up or down), the portfolio has to earn risk less rate due to no arb pricing

say the risk-free rate is 5% and t1-t0 = 1 year, we discount the hedged portfolio value at t1 with the risk-free rate
hedged portfolio value @t0 = 71.25/1.05 ~= 67.857
solve for c
67.857 = h*u-c
= 0.75*100 -c
c = 7.143

in general, h = (cu - cd)/S(u-d)
= (call value if up - call value if down)/Share price@t0(underlying price up change - underlying price down change)

in the example h= (15-0)/[100(1.15-0.95)]

note that the call price is independent of the likelihood of the underlying going up or down!!!!!!!! (that's quite counter intuitive if u think about it!)

2) risk-neutral approach
continuing with the above example, we assumed that risk-free rate=5% and the underlying/Share we use will either go up by 15% or down by 5% from t0 to t1

so what is the implied probability of it going up and going down?
=> ALL ASSETS should be expected make risk-free return in a risk neutral world. if not, arb opportunities exist.
so the Share should make 5% too =>
5% = 15%(prob of going up) + -5%(prob of going down)
= 0.15(p)+ -0.05(1-p)
p = 50% <- implied prob of going up
1-50% = 50% <- implied prob of going down

going back to the call option, with 50% going up (worth $15) and 50% going down (worth 0), the call option should be worth @t1:
0.5*$15+0.5*0= 7.5
discounting w/ rfr to get call value @t0 => 7.5/1.05 = 7.143

guess what? c is worth the same as with approach 1!! (whew...)


---------------------------------------------------
wait a second! we assumed that the S price will only go from 100 up to 115 or down to 95. that doesn't sound realistic!

u're right. the above simplifies everything with a simple one-step binomial tree. to model the real world, we will have to create multiple-step binomial trees. with the above one step binomial tree, we've got 2 expected values of the S price. As we increase the number of steps (n-> infinity), the binomial distribution would approx to a continuous normal distribution.

Game Theory


"Game theory is a study of strategic decision making. More formally, it is "the study of mathematical models of conflict and cooperation between intelligent rational decision-makers."
Think "Prisoner's dilemma".


Application of or Game theory won him 2012 nobel prize:
http://en.wikipedia.org/wiki/Alvin_E._Roth#Case_Study_in_Game_theory

Stable marriage problem is very interesting.  Algo is clear and easy to understand.  Interesting part is that there can be multiple solutions and stable may not = optimal:
http://en.wikipedia.org/wiki/Stable_marriage_problem#Optimality_of_the_solution

Forward Price

(post from old site)

What is Forward Price?
the AGREED price to buy something in the FUTURE.

Why is Forward Price > Spot price?
Intuitively:
say the underlying is worth $100.  if you enter a forward contract to buy that underlying at $100 in 1 year, u will have the freedom to spend/invest that $100 this year (and make interest).  this advantage is priced into the forward price so that's why forward price > spot price. 

Mathematically: 
Arbitrage.
if that's not the case, you'd:
@t=0
enter into the forward contract
short sell the underlying at spot price
lend the $ from above
@t=1

buy the underlying as per the forward contract at forward price determined t=0
use that underlying to even the short sell
receive the $ + interest  (proceed of this is to use to buy the contract in 1. above)
Say if the forward price = spot price at t=0, you'd execute the above and risk-free profit (interest from lending the $)!!  :D
So forward price has to = spot price (1 + r) so that no arbitrage to happen.

Put call parity

(post from old site)

C = max{S-K,0}
P = max{0,K-S}


C + Kd(0,T) = S + P
@maturity 
If S > K,
S-K + K = S + 0
If K > S,
0 + K = S + K-S

Graphically,
lhs is call curve shift up by K
Rhs is stock curve (45 degree) with left tail shifted up and flattened.