Thursday, March 26, 2015

dividend yield

@end of q3, a company announces a div, payable @end of q4.
(let's simplify the question by assuming
* ex-div date = payable date
* company pays div only once a year)

1) is it to better buy later towards the end of q4?
some may think that it's better to buy later toward the end of q4, since they think they're getting X amount of div while holding a shorter period of time and thus increasing the return.
it's actually not the case.  let's see why.
@end of q3, a company announces a div of $10.
We wait 'til the day right before ex-div @end of q4 and buy the stock at mkt price of $200.
On ex-div, we'll have
$10 div
the share of stock

it's a win, right?
No, it's not..
by definition, on ex-div open, the stock's price will drop by that div amount, trading at $190.  (think about it, you own a company worth $200.  after it gives away $10, it's only worth $190, right?)
so basically, you pay $200 to get something that's worth $190 and $10 cash div.
It's actually a loss since you have to pay brokerage, etc.

2) what kinda return am I actually getting anyway if I buy sometime between div announcement and ex-div?

let's look at an example.  a company announces $5 div at t3 (end of q3) payable at t4.  Say this company for that year pays 5%.
in an ideal world with simple interest, the stock price should go like this:
t0    100
t1    101.25
t2    102.5
t3    103.75
t4    105    => 100 + 5

in reality, at t3, that's when it announces a div of $5.  say, coincidentally, it's trading at 103.75.
how SHOULD we calculate the div yield of the co for that year?
1 way is to:
    div / (current price - div*(3/4))    # 3/4 of the year has passed
=    5 / (103.75 - 5 * 3/ 4)
=    5%

in reality,
    we just do
    div / current price
=    5 / 103.75
=    4.82%
the thinking (probably) is that, ignoring the actual cash flow, we assume the stock will pay the same amount of div in the next 12 months
thus, the div yield will be 4.82% instead



Monday, March 9, 2015

where clause to match string (char list)

// if you use column = "xxx"
// you are gonna get
// ERROR: length
// incompatible lengths
// Try:

select from table where column like "xxx"

Sunday, March 1, 2015

top n per group with python pandas

In [11]: df = pd.DataFrame({'cat':pd.Categorical(['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']),
   ....:                    'v1':np.random.randint(100, size=(9)),
   ....:                    'v2':np.random.randint(10, size=(9)) })

In [12]: df
Out[12]:
  cat  v1  v2
0   A  79   7
1   A  97   5
2   A  81   9
3   B  75   3
4   B  43   7
5   B  27   8
6   C  47   6
7   C  23   9
8   C  53   0

In [13]:

In [13]: # top n per group

In [14]: # 1) sort by the "top n" column (s)

In [15]: df.sort(['v1', 'v2'])
Out[15]:
  cat  v1  v2
7   C  23   9
5   B  27   8
4   B  43   7
6   C  47   6
8   C  53   0
3   B  75   3
0   A  79   7
2   A  81   9
1   A  97   5

In [16]: # 2) group by column of your choice

In [17]: # 3) select the top n of it

In [18]: df.sort(['v1', 'v2']).groupby('cat').head(2)
Out[18]:
  cat  v1  v2
7   C  23   9
5   B  27   8
4   B  43   7
6   C  47   6
0   A  79   7
2   A  81   9

In [19]: # 4) optionally sort the output nicely

In [20]: df.sort(['v1', 'v2']).groupby('cat').head(2).sort(['cat', 'v1', 'v2'])
Out[20]:
  cat  v1  v2
0   A  79   7
2   A  81   9
5   B  27   8
4   B  43   7
7   C  23   9
6   C  47   6

In [21]: