25 Aug 2010

HTML5 Up and Running View Comments

I preordered Mark Pilgrim's HTML5, Up and Running from Amazon back in March. It's available for free as Dive into HTML5. I've been a fan of Mark's writing for a while, so buying the book was a way to thank him "with a buck". Interestingly, the online book and paper book are not identical. For starters, the titles are different. In addition, the beautiful fonts and cutesy images of the online version are gone. The paper book is your run-of-the-mill O'Reilly book. I'm not sure if it'll make much of a difference in my enjoyment, but we'll see.

As I said, I preordered the book in March, so I had no idea when it would be finished and delivered. It showed up on my doorstep this morning, on my birthday! We're on our way to NYC tomorrow for our first trip back there since we moved to North Carolina in 2007, so I know have some light pleasure reading. Perfect Timing!

I've only read the first chapter so far, but I think I'm going to love it. I've done a lot of web programming in my life, but only maintain my own sites now. I'm reading this book like a programmer might read a book on astronomy... purely for the love of it. The first chapter goes into the details of how the IMG tag was invented. Marc Andreessen mentioned it on a mailing list back in 1993, and after some back and forth, shipped it in Mosaic, an early browser. Because it shipped and because it was popular, it got included in the HTML standards.

I still remember when Mosaic came out. I got my first email address in 1992 (vkurup@macc.wisc.edu) and somehow was on a mailing list announcing the impending release of Mosaic. I remember the text of the email basically saying that "This will change the world." Once I saw it, I thought it was cool, but way over-hyped. Who is going to want to wait for pictures to download over a 14.4K modem? Ridiculous. This is why you should not take my advice about new trends.

Anyway, the point of the IMG example is to show that HTML standards have never been "pure", but have always been influenced by the implementers and the users of the web. The way it should be. HTML5 is an acknowledgment of that fact.

web| review| HTML| programming| book

05 Aug 2010

Maps are functions, too View Comments

Braindump time. Here's what I've read about clojure in the past few days. I don't understand what this means yet, but maybe someday I will:

Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.

There are multiple languages that target the JVM (besides Java, of course). It seems that there are 2 classes of languages. Ported languages such as JRuby and Jython aim to be identical to their base languages (Ruby & Python). Native languages such as Clojure and Groovy are meant to target the JVM natively without satisfying any desire to be identical to another language. Native languages have the advantage that they only have 1 set of libraries to use, so you don't get stuck between two languages.

I installed clojure 1.0 from the ubuntu repositories, but promptly uninstalled it once I found leiningen. Leiningen is a build tool for clojure. You can easily create a new project and specify its dependencies and leiningen will download those dependencies, including a full version of clojure for each project. You can have 1 project using leiningen 1.0 and another using version 1.2 sitting right next to it. That seems smart!

I installed clojure-mode on emacs.

Right now, I'm working my way through the labrepl project. It's an interactive tutorial and REPL (read-eval-print-loop) where you can run the examples in the tutotial.

It's always important to know how to find help:

  • (doc blah) gives documentation about blah.
  • (find-doc "blah") searches for documentation.
  • (javadoc blah) opens Java docs in your browser
  • (show java.util.Date) shows method signatures

Here's something cool: Maps are associative arrays. Here's a simple one:

(def me {:first-name "Vinod", :last-name "Kurup"})

To get the data, you use the get function, like you would in other languages:

(get me :first-name) -> "Vinod"

But, maps and keywords are functions as well, so you can do:

(me :first-name) -> "Vinod"
(:last-name me) -> "Kurup"

clojure| programming

19 Jul 2010

Stock Market Timing View Comments

If you asked me a few years ago, I would have said you absolutely can't time the stock market. The 2008 crash hurt enough to make me review my convictions a little. I still believe that accurate market timing is somewhere between difficult and impossible. Trying to analyze stock charts to figure out what will happen in the short term is mostly a crap shoot. The way to win in the stock market is to buy value stocks, especially when everyone else is selling.

I do, however, now believe that there is a place for market timing in deciding when to be in the stock market, in the first place. I think there are some trends that have repeatedly predicted poor stock performance. There are some super smart people that have analyzed these trends over at the Motley Fool message boards. They've come up with many indicators that supposedly tell you when to get in and out of the stock market, but my favorite is the 99 day rule 1.

The basic premise of the 99 day rule is that when the S&P 500 Index stops making new highs, investors get pessimistic and stocks fall. When it starts making new highs again, optimism takes over and bull markets start. The 99 day rule has 2 parts and coincidentally uses 99 days as the cut-off for both parts. It looks at whether a new high has been made recently. It defines "high" as a 99-day high. It defines "recently" as 99 days. So, it looks for a new 99-day high within the last 99 days. The number is arbitrary. Hop over the the Motley Fool boards to see how the dates have been tuned and pick different ones, if you like.

I like this rule, because it's simple, easy to calculate and doesn't have many "signals", so you're not constantly buying and selling. Of course, it's not perfect, but it would have gotten you out of most of the major bear markets.

Here is how the rule has done over time:

Period        S&P 500   Switching   Improvement 
---------     -------   ---------   -----------
1930-1935     -12.2%       4.3%       16.4% 
1935-1940      10.3%      16.2%        6.0% 
1940-1945       7.4%       5.6%       -1.8% 
1945-1950      10.2%       7.4%       -2.8% 
1950-1955      23.6%      22.5%       -1.1% 
1955-1960      15.2%      13.8%       -1.4% 
1960-1965      10.7%      11.7%        1.0% 
1965-1970       5.0%       7.1%        2.2% 
1970-1975      -2.4%       6.8%        9.2% 
1975-1980      14.8%      12.0%       -2.8% 
1980-1985      14.8%      20.1%        5.2% 
1985-1990      20.4%      18.8%       -1.7% 
1990-1995       8.7%       6.7%       -2.0% 
1995-2000      28.6%      27.9%       -0.7% 
2000-2005      -2.3%       4.3%        6.6% 
2005-early2008  2.0%       4.5%        2.5%

You may have gotten to this point and are now wondering why it's worth following this rule. In at least 3 of the past 5 periods, this signal would have lost money versus staying in the market. That is true. The key is to understand that there is no free lunch. Any rule that tries to get you out of major bear markets will be imperfect and will often leave you with less money than if you had chosen to act on a different date (or not act at all). The overall goal is not to increase your returns, but to decrease your risk without sacrificing your returns. This signal does that. Overall, it does actually increase your returns by about 2% annually since 1930.

The other nice feature is that it doesn't have a "slam-dunk" feel to it. My experience is that things that work in the stock market have to have a little of a "gut wrenching" feel to them. If it sounds too good to be true, it is. This rule feels correct, improves returns over the long run, is backtested well, but does so at the risk of some lower performing years. That feels a little gut-wrenching to me.

Here is the author's (mungofitch) description:

By using the rule that I described above, you would have had a total return of 11.80%, with a total risk of 6.71%. So, you would have had 2.3% per year higher returns on average, while experiencing only about 56.9% of the risk. Higher returns, lower risk. That sounds good, right?

But, is this one of those iffy timing systems that really only works because it managed to avoid the crash of 1987 of something? No, actually it has you in the market in the 1987 crash: it's not perfect. But does it really add value on average through the years? Not always, but pretty darned well. Sometimes you do a little worse, but never a lot worse. And when you do better, you do a LOT better. So, overall, it's worth it.

In order to keep track of the signal, I wrote a little python program to calculate whether we are in buy mode or sell mode. It uses the excellent ystockquote module written by Corey Goldberg.

investing| python| programming

18 Jul 2010

First Steps with Clojure View Comments

As mentioned yesterday, I'm teaching myself clojure. I started by trying to install it.

vinod@ike$ aptitude install clojure

Ubuntu has packaged version 1.0.0 of clojure. I always default to the OS-installed version of programs, just for ease of maintenance. The current stable version of clojure is 1.1 and it appears that 1.2 is in beta. I have no idea what has been changed in 1.1 or 1.2, but I'm going to try to get along using 1.0.0 and update only if I need to for a specific reason.

I then read through the tutorial for non-lisp programmers, which succinctly describes the basics of the language.1 Everything in lisp is either an atom or a list. Atoms include numbers, booleans, strings, symbols, keywords and the nil value. Lists (delimited by parentheses ()) are the basic data structure, but include representations such as vectors (delimited by square brackets []) and maps (delimited by curly brackets {}). Clojure programs are themselves simply lists, which leads to the power of lisp-like languages because code and data are interchangeable.

You use def to create variables and defn to create functions. The language includes loops and conditional statements, just like any other useful language. Only false and nil are false in clojure; zero (0), and the empty string ("") are true, unlike a lot of languages.

The interesting part is the integration with Java. To instantiate a java object, simply call new:

user=> (new java.util.Date)
#<Date Sun Jul 18 16:20:54 EDT 2010>

To call object methods or get instance/class variables, use the dot "." method.

user=> (. (new java.util.Date) (toString))
"Sun Jul 18 16:24:09 EDT 2010"

user=> (. Integer MAX_VALUE)
2147483647

The article contains this interesting quote:

Sequences are in a sense, the core of idiomatic Clojure programming. Understand sequences and the forms that work with them, and you will have cleared one of the biggest hurdles in writing significant Clojure programs.

So, add that to my to-do list. Where to go next? A StackOverflow thread has pointed to me to a few options. I'll probably read one of these next:


  1. I later read that this tutorial is out of date, so I may have to "unlearn" things later. It still comes up as the first hit on Google for "clojure tutorial"

clojure| programming

17 Jul 2010

Getting some clojure View Comments

Steve Yegge is one of my favorite bloggers. I read "You Should Write Blogs" a long time ago and it is one of the main reasons that I still try to keep this blog going at all. He recently took a blogging hiatus and in his return post, he left a little hint that he was interested in a programming language called clojure.

My programming skills have been rusting away (Darn those patients, always getting sick and stuff!), so I've been looking for a little inspiration. I'm hoping that clojure is it. I've always wanted to learn lisp. I know a little elisp and scheme, but I've never gotten comfortable with them. Clojure compiles code that runs on a JVM (java virtual machine). This gives you a lot of the "power" of java, namely the extensive libraries and the widespread platforms on which it will run. But instead of having to write java, you get to write in a dynamic, lisp-like language. At least that's how I understand it.

Specifically, I hope that I'll eventually be able to create applications that run on android phones, which is a java environment. Smartphones are clearly going to be a huge part of the future, if not the way of the future. I don't see myself ever buying an iPhone and Mala already has a Droid Eris that I'd be able to play with if I needed. (Didn't tell you that, did I Mala?) So... Android it is.

Starting today, I'm going to start reading about clojure and taking notes (and maybe even posting them!). I've never been very good about taking notes, ever in my life. But, I clearly need to learn.

clojure| programming

Older Posts