# 02.24.09

## Chart of time.h Functions

Posted in programming at 10:04 pm by danvk

Here’s a handy chart of the C Standard Library functions in time.h:

The ovals are data types and the rectangles are functions. The three basic types are:

• time_t: number of seconds since the start of the UNIX epoch. This is always UTC!
• struct tm: A broken-down date, split into years, months, seconds, etc. In Python, it’s a tuple.
• string: Any string representation of a time, e.g. “Wed Jun 30 21:49:08 1993″.

Generally you either want a time_t (because it’s easy to do arithmetic with) or a string (because it’s pretty to look at). So to get from a time_t to a string, you should use something like strftime("%Y-%m-%d", localtime(time())). To go the other way, you’d use mktime(strptime(str, "%Y-%m-%d")).

This library has been around since at least 1982. It’s been replicated in many other languages (Python, Perl, Ruby). We seem to be stuck with it.

Read the rest of this entry »

# 02.22.09

## Why You Invest When the Market Goes Down

Posted in finance, math at 2:51 pm by danvk

The S&P 500 certainly hasn’t made anyone rich over the last year:

Most people see this and think “an investment one year ago would have lost 45% of its value”. Others think “great, now stocks are all cheaper!”

In reality, most ordinary people invest portions of their paychecks, either through their 401(k) or a personal account. This means they’re doing time averaging. Sure, investments when the market was up aren’t doing well. But investments when the market was down are doing just fine.

This is all kind of wishy-washy, though. Let’s try to quantify it. Suppose a market drop and recovery looks like a parabola:

The prices here are parabolas
$P(t) = a(1-2t)^2 + (1-a)\,$

for various values of a. a=0 means the market is flat. a=0.5 means the market loses 50% of its value.

If you invest dt dollars in this market at each point in time, you can work out a nasty integral and show that the number of shares you have at time t is:

$S(t) = \int_{x=0}^{x=t} \frac{dx}{a(1-2x)^2+(1-a)}$

$= \frac{1}{2a \sqrt{1/a - 1}} \left(atan \frac{1}{\sqrt{1/a - 1}} - atan \frac{1-2t}{\sqrt{1/a - 1}} \right)$

and hence the value of your shares at the end is:

$S(1) = \frac{1}{a \sqrt{1/a - 1}} atan \frac{1}{\sqrt{1/a - 1}}$

Here’s what that looks like:

The x-axis is a, the fraction by which the market drops. The y-axis is your total return on investment. If the market drops by 50% (a=0.5) then your total return on investment is around 55%. With time-averaging, the more the market drops, the better you do.

This makes a lot of sense if you think about it. Say the market drops 99.9% and then recovers. The shares you bought when it was at its bottom earned you a return of 1000x. Investing at the bottom is important! You should keep investing, even as the market drops. If you don’t, you’ll miss that bottom.

# 02.19.09

## Sky-High Boggle Scores with Simulated Annealing

Posted in boggle at 1:09 am by danvk

Don’t let the sixteen month hiatus fool you. There’s just no end to Boggle posts on danvk.org!

In case you’d forgot, we’ve developed a blazing fast boggle solver capable of scoring 10,000 Boggle boards a second. What to do with this? Other than some interesting analyses, the most interesting question is:

What is the highest-scoring Boggle Board?

In this post, we’ll try to answer that question using Simulated Annealing. Here’s a sneak peak at one of the exceptionally word-rich boards we’ll find:

 p e r s l a t g s i n e t e r s 3625 points

Follow me past the fold for more…

# 02.16.09

Posted in web at 5:23 pm by danvk

Has anyone else noticed Twitter being mentioned in the news more and more the last few weeks?

These have all happened in the last month:

All this makes me think I should hop on the bandwagon. I just created an account. I’m danvdk.

# 02.13.09

## Fun with a Wireless Repeater

Posted in personal at 11:27 am by danvk