Modern Portfolio Theory - Part 2 - Cash and the Risk-free rate

This post continues the Modern Portfolio Theory - Part 1.

Remember from last time:

  • Assume investments have random returns that follow a normal distribution.
  • You must invest all your money, but by wisely choosing your portfolio, you could create a portfolio of a variety of different average returns and variability (standard deviation).
  • A rational investor would only choose to create a portfolio that minimized variability for any given level of return. These portfolios are on the efficient frontier.
  • The blue dots are also on the efficient frontier technically (minimum variance for a given level of return) but not very interesting, as a rational investor could choose to get both more return and less variance.

Cash

One thing we didn't mention was the possibility of holding cash. Let's see what introducing this possibility does to our graph.

Continue reading

Modern Portfolio Theory - Part 1

Ever wonder what "Beta" meant in the context of investments? Or why people tell you that diversification is a good idea - or why some people say all you need to do is invest in an index fund (the whole market) and forget about it?

It all has to do with modern portfolio theory, an influential theory developed from the 50s to the 70s that netted a Nobel Prize. It's not perfect, but it's nice to understand, so I'll explain it.

First, assume that financial securities (i.e. stocks, bonds) have rates of return that are uncertain according to a normal distribution. Securities don't actually have normally distributed returns, but this assumption enables the whole theory.

Continue reading

Syria and the Pottery Barn Rule

The international system, much like nature, abhors a vacuum.

It is why Gen. Colin Powell advised President George W. Bush, during the lead up to the 2003 invasion of Iraq, of the Pottery Barn Rule: You Break It, You Bought It. Even though Secretary of Defense Donald Rumsfeld (as documented in Thomas Rick’s excellent book “Fiasco”) believed that the US could simply topple Saddam then leave the United States ended up staying in the country for almost a decade. As much as Rumsfeld may have wanted to, you can’t simply create an anarchic state and leave, or alternatively plop down a pre-fabricated government and expect citizens of another nation to accept it as their new legitimate government (both lessons the Bush administration learned the hard way in Iraq). One way or the other, any nation that takes a direct role in destroying the existing political order in a territory will be stuck there for years while a new one emerges.

It is the reason President Obama wanted to keep a light foot-print in Libya: If American boots were on the ground when Ghaddafi fell, the United States would end up being responsible for the security and political transition in Libya. This would effectively end up looking like a repeat of the occupation of Iraq. The US, nor any other nation for that matter, does not want to take on that burden. Consequently, any assistance to the Syrian rebels has to be indirect and not drag the benefactor into the war.

Continue reading

Fear: Why I produced more when I was younger

When I was younger, I produced more things. If you look at my blogging alone, when I was fifteen, I would write three or four blog posts a month, even though I was a much worse writer. These days I'm lucky if I write three or four per year.

I could say that I'm busier now. It might be true. I do work at least a 40 hour workweek now... Though when I think back on it, I certainly spent about that much time at school - and I had homework too. No, being less busy couldn't have been it.

I think I know what it is. I was just much less of a pussy. In fact, I was fearless. I sucked, but I didn't know I sucked. In fact, I thought I was awesome at writing. The same goes for programming. These days, I would never attempt to create a full blown content management system. I now have the wisdom to know how much of an endeavor that would be. But back then, I didn't - I just started hacking shit together with PHP and MySQL, learning as I went.

Being older, more experienced, and having the wisdom to pick your battles can be a great asset, but I think it can also be a big weakness. It's easy to get caught up in analysis paralysis - to know all the difficulties of the task at hand up front, and spend all your time thinking about how to do it perfectly, rather than just doing it.

Continue reading

Making Mac keyboard palatable for Windows/Linux users

My work has me using a Mac on one of the projects. At first, despite the well designed interface, I was infuriated by how the keyboard shortcuts were different from the common Windows/Linux standards.

However, after dicking around with ~/Library/KeyBindings/DefaultKeyBinding.Dict with some success and much frustration, I found a program that solves all my problems:

http://pqrs.org/macosx/keyremap4macbook/

Here is how to use it to replicate PC style keyboard (mostly... I'm sure I'll run into other frustrations):

  1. System Preferences -> Keyboard -> Modifier Keys -> Remap CapsLock to Command
  2. Install KeyRemap4MacBook.
  3. Here are the settings I'm using right now:
    • Command+Arrow to Option_L+Arrow
    • Command+Delete to Option_L+Delete
    • Use PC Style Home/End
    • Use PC Style PageUp/PageDown
    • Use PC Style Control+Up/Down/Left/Right
    • Use PC Style Delete-Last-Word

I'm still kind of torn between making my CapsLock -> Command or CapsLock -> Control.

Making CapsLock->Control preserves shortcuts in terminal and X11 apps, but breaks them in native apps, though Keyremap4Macbook does allow you to remap many common native shortcuts from Command to Control...

Anyways, this makes Mac usable and pretty palatable again.

Memory sensitive cache in Java

Use com.google.common.cache.CacheBuilder from guava-libraries with strong-reference keys and soft-reference values.

WeakHashMap is inappropriate, as weak-references are collected too quickly to be an effective cache. [1] Soft references seem designed for caching. [2]

CacheBuilder also allows you to specify a method to compute and store cache misses transparently:

    Cache graphs = CacheBuilder.newBuilder()
       .concurrencyLevel(4)
       .weakKeys()
       .maximumSize(10000)
       .expireAfterWrite(10, TimeUnit.MINUTES)
       .build(
           new CacheLoader() {
             public Graph load(Key key) throws AnyException {
               return createExpensiveGraph(key);
             }
           });

[1] http://www.codeinstructions.com/2008/09/weakhashmap-is-not-cache-understanding.html

[2] http://download.oracle.com/javase/6/docs/api/java/lang/ref/SoftReference.html

Programmers should read "The Writing Life"

I'd read before that programming is like writing, but pg. 56 from The Writing Life has really persuaded me:

First you shape the vision of what the projected work of art will be. [...]

Many aspects of the work are still uncertain, of course; you know that. You know that if you proceed you will change things and learn things, that the form will grow under your hands and develop new and richer lights. But that change will not alter the vision or its deep structures; it will only enrich it. You know that, and you are right.

But you are wrong if you think that in the actual writing, or in the actual painting, you are filling in the vision. You cannot fill in the vision. You cannot even bring the vision to light. You are wrong if you can take the vision and tame it to the page. The page is jealous and tyrannical; the page is made of time and matter; the page always wins. The vision is not so much destoryed, exactly, as it is, by the time you have finished, forgotten. It has been replaced by this changeling, this bastard, this opaque lightless chunky ruinous work.

I love Annie Dillard.

Simultaneous iteration of multiple lists without indices

Often we will want to iterate through multiple lists simultaneously. Say we are programming a game, and have a list of technologies and a list of research allocations.

Python:

techs = ["Propulsion", "Weapons", "Armor"]
allocations = [0.1, 0.5, 0.4]

Here is the old, and worse, way of iterating through both lists:

for i in range(techs.length):
  print techs[i] + " " + allocations[i]

Seems reasonable for this trivial case, but managing indices is often annoying and or dangerous. Moreover, indices are blatantly wrong for data structures without constant time random access.

Here’s a more awesome way to do it:

for tech, allocation in zip(techs, allocations):
  print tech + " " + allocation

Okay, so what does zip do? Best way is to illustrate by example on the interactive interpreter:

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> zip(x,y)
[(1, 4), (2, 5), (3, 6)]

It makes a new list composed of the elements of the old list put into tuples together. So iterating through both lists simultaneously is completely natural. This works for an abitrary number of lists in Python, and is also very handy for list comprehensions.

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> z = [7, 8, 9]
>>> t = [10, 11, 12]
>>> zip(x,y,z,t)
[(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)]
>>> [a+b+c+d for a,b,c,d in zip(x,y,z,t)]
[22, 26, 30]

The reason it works so naturally in the for loop is because of the tuple unpacking, which looks like this if the above is still confusing:

>>> a,b = (1,2)
>>> print a
1
>>> print b
2
>>> for a,b in [(1,2), (3,4)]:
...   print a
...   print b
... 
1
2
3
4

Openid Delegation

I’ve long known about and resisted adopting OpenID for one reason: I feared that I would be at the mercy of whatever company I chose as my OpenID provider, be it Google, Yahoo, Verisign, whatever…

If that company went out of business, decided to “realign with core competencies”, start charging exorbitant fees for using the OpenID, I’d be screwed, as my digital identity for all these sites would be under their control.

The alternative is running your own OpenID provider, but that’s a good amount of work, and kind of defeats the purpose of OpenID (if everyone has to run their own provider).

I learned today that there’s a great solution to this problem, called ‘delegation’. How it works is that you put an html fragment in the HEAD section of a webpage you control, and suddenly that URL can act as your OpenID, with the actual authentication delegated to a party of your choice.

If say, I have an OpenID with Verisign (I do), I put this fragment in the HEAD section of URL I control:

<link rel="openid.server" href="http://pip.verisignlabs.com/server" />
<link rel="openid.delegate" href="http://tommycli.pip.verisignlabs.com" />
<link rel="openid2.provider" href="http://pip.verisignlabs.com/server" />
<link rel="openid2.local_id" href="http://tommycli.pip.verisignlabs.com" />
<meta http-equiv="X-XRDS-Location" content="http://pip.verisignlabs.com/user/tommycli/yadisxrds" />

So now when I go to login to an OpenID page, I can just put in: tommycli.keepword.com, or whatever webpage I choose to put that fragment above on – and my OpenID is: tommycli.keepword.com.

So if Verisign becomes ghey, I can always switch to a new OpenID provider – I just have to change a page on a domain I control. Essentially – I can keep my OpenID so long as I can keep my domain name: I am only dependent on the integrity of domain name property rights on the internet, and if that collapses, so does the internet at large, and I won’t even need an OpenID.

SPS Final Speech

I gave this speech when I turned over the Physics club to a classmate as I was leaving UCLA. The speech references http://en.wikipedia.org/wiki/File:Ishihara_Mishima.jpg .

In fourteen years, one of them, Yukio Mishima, would lead a group to storm the Tokyo office of the Japan Self-Defense Forces. Decrying Japan’s post war loss of identity, he would go on the balcony and try to incite the soldiers to rebellion. Failing, he’d go inside and commit ritual suicide by disemboweling himself with a knife. He was also considered thrice for the Nobel Prize in Literature.

The other, Shintaro Ishihara, would have been by that time been elected to the Japanese Senate as a member of the Liberal Democratic Party. He would eventually write the famous essay “The Japan That Can Say No”, and in 1999, would be elected governor of Tokyo.

Not yet though.

Right now, it’s still 1956, and none of that has happened yet. Ishihara has just graduated college – and Mishima isn’t not much older. Right now they’re just friends chilling on a roof pondering their places in post-war Japan.

And in the picture, can you guess which one’s Mishima and which one’s Ishihara? I bet you can’t, and that’s no surprise, because they’re not those people yet. Just like us today sitting here in this room, what they possess in this photo is promise and potential – the beginnings of great things.

Today is the beginning of something great. I’m leaving. Patrick’s going to run the club next year and he will, I’m sure, do a terrific job. Patrick, I give to you today, the right to speak – the conch shell. Pass it on when you’re done huh?