Remembering COM

In the late ’90s I thought COM (Microsoft’s Component Object Model) was the way of the future. The whole architecture starting with the IUnknown interface was very elegant. And to hear Don Box explain it, COM was almost inevitable.

I was reminded of COM when I saw the slides for Kevlin Henney’s presentation Worse is better, for better or for worse. Henney quotes William Cook’s paper that has this to say about COM:

To me, the prohibition of inspecting the representation of other objects is one of the defining characteristics of object oriented programming.  …

One of the most pure object-oriented programming models yet defined is the Component Object Model (COM). It enforces all of these principles rigorously. Programming in COM is very flexible and powerful as a result.

And yet programming COM was painful. I wondered at the time why something so elegant in theory was so difficult in practice.  I have some ideas on this, but I haven’t thought through them enough to write them down.

Just as I was starting to grok COM, someone from Microsoft told me “COM is dead” and hinted at a whole new approach to software development that would be coming out of Microsoft, what we now call .NET.

COM had some good ideas, and some of these ideas have been reborn in WinRT. I’ve toyed with the idea of a blog post like “Lessons from COM,” but I doubt I’ll ever write that. This post is probably as close as I’ll get.

By the way, this post speaks of COM in the past tense as is conventional: we speak of technologies in the past tense, not when they disappear, but when they become unfashionable. Countless COM objects are running on countless machines, including on Mac and Unix systems, but COM has definitely fallen out of fashion.

More tech posts

How Lp spaces nest

A function f(x) is said to be in Lp if the integral of |f(x)|p is finite. So if you know f is in Lp for some value of p, can conclude that f is also in Lq for some q? In general, no. But for two important special cases, yes. The special cases have to do with the domain of f.

If f is a function on a finite measure space and p > q, then every function in Lp is in Lq. In particular probability spaces are finite measure spaces, and so if a random variable’s pth moment exists, the qth moment exists.

If the domain of f is the integers with counting measure, f is a sequence and the “integral” of |f(x)|p is simply a sum. Now the relationship between Lp spaces is reversed: p > q, every sequence in Lq is in Lp.

There are two reasons a function could fail to be in Lp: singularities and fat tails. In a finite measure space, there are no tails; you only have to worry about singularities. On the integers, there are no singularities; you only have to worry about tails.

Now consider functions on the whole real line. Membership in Lp says nothing about membership in Lq because now you can have singularities and fat tails.

Learn one Perl command

A while back I wrote a post Learn one sed command. In a nutshell, I said it’s worth learning sed just do commands of the form sed s/foo/bar/ to replace “foo” with “bar.”

Dan Haskin and Will Fitzgerald suggested in their comments that instead of sed use perl -pe with the same command. The advantage is that you could use Perl’s more powerful regular expression syntax. Will said he uses Perl like this:

    cat file | perl -lpe "s/old/new/g" > newfile

I think they’re right. Except for the simplest regular expressions, sed’s regular expression syntax is too restrictive. For example, I recently needed to remove commas that immediately follow a digit and this did the trick:

    cat file | perl -lpe "s/(?<=\d),//g" > newfile

Since sed does not have the look-behind feature or d for digits, the corresponding sed code would be more complicated.

I quit writing Perl years ago. I don’t miss Perl as a whole, but I do miss Perl’s regular expression support.

Learning Perl is a big commitment, but just learning Perl regular expressions is not. Perl is the leader in regular expression support, and many programming languages implement a subset of Perl’s regex features. You could just use a subset of Perl features you already know, but you’d have the option of using more features.

English to Spanish rules of thumb

Here are four rules of thumb for converting English words in to Spanish.

  1. Words ending in –ible or –able often stay the same.
  2. Change words ending in –ent and –ant to end in –ente and –ante respectively.
  3. Change words ending in –tion to end in –ción.
  4. Change words ending in –ical to end in –ico (or –ica if feminine).

These are only rules of thumb. There are exceptions. For example, two exceptions to the third rule are that the Spanish word for translation is traducción and the word for explanation is explicación. But these rules hold quite often.

Related posts

R without Hadley Wickham

Tim Hopper asked on Twitter today:

#rstats programming without @hadleywickham’s libraries is like ________ without _________.

Some of the replies were:

  • (skydiving, a parachute)
  • (gouging your own eyes out, NULL)
  • (dentistry, anesthesia)
  • (shaving, a razor)
  • (Internet shopping, credit card)

Clearly there’s a lot of love out there for Hadley Wickham’s R packages. I’m familiar with his ggplot2 graphics package, and it’s quite impressive. I need to look at his other packages as well.

Random inequalities and Edgeworth approximation

I’ve written a lot about random inequalities. That’s because computers spend a lot of time computing random inequalities in the inner loop of simulations. I’m looking for ways to speed this up.

Here’s my latest idea: Approximating random inequalities with Edgeworth expansions

An Edgeworth expansion is like a Fourier series, except you use derivatives of the normal density as your basis rather than sine functions. Sometimes the full Edgeworth expansion does not converge and yet the first few terms make a good approximation. The tech report explicitly considers Edgeworth approximations with just two terms, but demonstrates the integration tricks necessary to use more terms. The result is computed in closed form, no numerical integration required, and so may be much faster than other approaches.

One advantage of the Edgeworth approach is that it only depends on the moments of the distributions in the inequality. This means it provides an approximation that’s waiting to be used on new families of distributions. But because it’s not specific to a distribution family, its performance in a particular case needs to be explored. In the case of beta distributions, for example, even a single-term approximation does pretty well.

More blog posts on random inequalities

Self-licking ice cream cones

In a comment to yesterday’s post on leadership, Dave Tate introduced me to the phrase “self-licking ice cream cone.” According to Wikipedia,

In political jargon, a self-licking ice cream cone is a self-perpetuating system that has no purpose other than to sustain itself. The phrase appears to have been first used in 1992, in On Self-Licking Ice Cream Cones, a paper by Pete Worden about NASA’s bureaucracy.

I touched on this in Maybe you only need it because you have it. Now that I know a vivid description for such things, I’ll have to use it more often.

Update: Maybe there should be a term for something that isn’t quite a self-licking ice cream cone, a system that serves some purpose other than sustaining itself but is still primarily about self perpetuation. Or maybe there should be a scale, say from 0 to 100, for the extent to which an organization is useless and self-perpetuating.

I’m reminded of the Blue Bell Ice Cream advertising slogan: We eat all we can, and we sell the rest. A self-licking ice cream cone would correspond to the employees eating all the ice cream, a sort of ice cream co-op. An organization that isn’t entirely about self-preservation might be one in which the employees manage to sell 20% of the ice cream.

Related post: Parkinson’s law

General formula for normal moments

Yesterday I wrote about how to find moments of the normal distribution using Sage. Then GlennF left a comment saying it’s not too hard to work out the moments analytically and outlined a proof. I’ll fill in a few details here.

First, start with a standard normal distribution Z. That is, Z has mean 0 and variance 1. By symmetry, the odd moments of Z are 0. For the even moments, integration by parts shows that E(Z2m) = (2m – 1) E(Z2m – 2). Apply this relation recursively until you get E(Z2m) = (2m – 1)!!. (See this post if you’re unfamiliar with double factorial. Note that (-1)!! is defined to be 1.)

For a general normal random variable X with mean μ and variance σ2, define Z = (X – μ)/σ. Then Z is a standard normal and X = σZ + μ. Apply the binomial theorem and note that the odd terms are zero.

 E{(\sigma Z + \mu)^n} = \sum_{i=0}^n {n\choose i}\sigma^i E(Z^i) \mu^{n-i} = \sum_{j=0}^{\lfloor n/2 \rfloor} {n \choose 2j} (2j - 1)!!\, \sigma^{2j}\mu^{n - 2j}

 

Higher moments of normal distribution

Sometimes a little bit of Python beats a Google search.

Last week I needed to look up the moments of a normal distribution. The first two moments are common knowledge, the next two are easy to find, but I wasn’t able to find the higher moments.

Here is a little Sage code that produces a table of moments for the normal distribution. (Sage is a Python-based mathematical computing environment.) The code computes the expected value of Xn by taking the nth derivative of the moment generating function and setting its argument to zero.

    var('m, s, t')
    mgf(t) = exp(m*t + t^2*s^2/2)
    for i in range(1, 11):
        derivative(mgf, t, i).subs(t=0)

Here’s the output.

m
m^2 + s^2
m^3 + 3*m*s^2
m^4 + 6*m^2*s^2 + 3*s^4
m^5 + 10*m^3*s^2 + 15*m*s^4
m^6 + 15*m^4*s^2 + 45*m^2*s^4 + 15*s^6
m^7 + 21*m^5*s^2 + 105*m^3*s^4 + 105*m*s^6
m^8 + 28*m^6*s^2 + 210*m^4*s^4 + 420*m^2*s^6 + 105*s^8
m^9 + 36*m^7*s^2 + 378*m^5*s^4 + 1260*m^3*s^6 + 945*m*s^8
m^10 + 45*m^8*s^2 + 630*m^6*s^4 + 3150*m^4*s^6 + 4725*m^2*s^8 + 945*s^10

Update: Here is a formula for the higher moments of the normal.

Related post: Sage Beginner’s Guide