Converting Decimal to Binary in R

February 9, 2012 § Leave a comment

A common task, say in conducting probability simulations, often involves enumerating every possible outcome of a set. It is often useful to use a binary representation of the size of our set to do this. A flexible way to get this representation is in matrix form, which will then allow the usual matrix operations. Here I present one such algorithm and given an implementation in R. The algorithm is iterative. It works on vectors of any length including 1 (a number). And, the algorithm returns an m x n matrix where m is the length of the longest binary digit and n is the length of the vector of numbers you give it.

To get m, the length of the longest binary digit, we use the logarithm (base 2):
m(x) = 1 + \lfloor log_{2}(X) \rfloor

For example, we know that
2 \rightarrow [1,0]

in binary, so
m(2) = 2.

Again,
10 \rightarrow [1,0,1,0], m(10) = 4.

I think the easiest way to explain what we’re going to do here is just walk through some pseudo-code:

Algorithm AS-BINARY
Input: A non-empty list of numbers
Output: A matrix representation of the binary form of each number in x
	b ← 2	// base
	m ← 1 + floor(logb(x))
	n ← length(x)
	R ← matrix(m x n)
	for i from m to 1
		R[i,] ← x mode b	// Ri, is entire row i; operations happen vector-wise
		if i > 1
			x ← integer(x/b)

In R we have:

as.binary <- function (x) { 
  base = 2 
  ndigits = 1 + floor(log(max(x), base)) 
  r = 1) 
    for (i in ndigits:1) { 
        r[i, ]  1) 
          x <- x%/%base 
    } 
  class(r) <- "binaryInt" 
  attr(r, "base") <- base 
  return(r) 
}

Let’s do a couple of examples:

Converting the number 10 to binary (1010)

x <- as.binary(10); x
#	     [,1]
#	[1,]    1
#	[2,]    0
#	[3,]    1
#	[4,]    0
#	attr(,"class")
#	[1] "binaryInt"
#	attr(,"base")
#	[1] 2

Converting each of the numbers in the vector (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to binary (1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010)

x <- as.binary(1:10); x
#	     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#	[1,]    0    0    0    0    0    0    0    1    1     1
#	[2,]    0    0    0    1    1    1    1    0    0     0
#	[3,]    0    1    1    0    0    1    1    0    0     1
#	[4,]    1    0    1    0    1    0    1    0    1     0
#	attr(,"class")
#	[1] "binaryInt"
#	attr(,"base")
#	[1] 2

Since it performs the operations vector-wise the entire algorithm is earth-shatteringly fast! Next time I will show how this function can be used as the workhorse in an “all-subsets” regression. That is, when we want to try out every possible model specification on some data.

Wise words from a decent man

February 8, 2012 § Leave a comment

Money is what fueled the industrial society. But the in information society, the fuel, the power, is knowledge. One has now come to see a new class structure divided by those who have information and those who must function out of ignorance. This new class has its power not from money, not from land, but from knowledge.

– John Kenneth Galbraith

On App Store Price Anchors

December 26, 2011 § Leave a comment

Dan Ariely is one of my favorite academics in the business world. He’s written such great books as Predictably Irrational (or the TED talk) and (my favorite) The Upside of Irrationality. He’s got a post on his blog about why it’s so hard for smartphone users to shell out a buck for an app on a phone, given that we spend so much on crap we don’t really need. For anyone familiar with basic concepts in behavioral economics, marketing, or psychology, this is a textbook example of anchoring. Consumers’ willingness to pay is very much determined by how much they have seen similar items priced, as opposed to how much they think an item is intrinsically worth. Ariely’s post nicely outlines this concept, you should read it. But, he says nothing of how app developers can overcome this problem. I propose a solution I will deem the Starbucks Solution.

Before Starbucks arrived the typical cup of coffee went for about a dollar. Four dollars for a cup of coffee?! Not a chance. But Starbucks made a point of charging people for the “experience“. They realized you can’t easily move price anchors. So they convinced people that they weren’t buying a cup of coffee (which is only worth $1), but were buying a lifestyle. It also didn’t hurt that they made damn good coffee.

This isn’t necessarily completely applicable to an iPhone or Android app, but it does illustrate a way in which developers can position their product to undermine consumers’ cognitive bias. If I were launching an app today I would charge a lot more for it than developers are charging now. First, I would build a great app, this is the sine qua non of ALL product market strategies. I would price in the $15 – $25 range. And by that I mean the $14.99 – $24.99 range of course. This may seem absurd. It is much more than people are used to paying, but that is the point. You may ask: “why not move the price point incrementally, say to $3 or $5?” Because you have to shock people out of their price anchor. You have to convince users that they are not buying an app, they are buying something different. If people are accustomed to paying $1 for an app and you get them to buy a something from their phone for $25, they will not think that something is just another app, it’s known as cognitive dissonance. Cognitive dissonance is real and more powerful than anchoring in terms of psychological pressure.

So my advice seems trite: charge more. But it’s a bit more nuanced than that: charge a lot more and when describing your product, don’t ever use the word “app”. This is not entirely novel. Remember the diamond app when the iPhone first came out? It cost $999 and put a picture of a diamond on your phone…I doubt those guys made a ton of money, but I’m SURE they broke even, which is more than most developers can say for themselves. Gimmicks aside, not many serious productivity- or utility-focused apps have tried to aggressively push out the price point.

Shoes and Ships

December 1, 2011 § Leave a comment

‘The time has come,’ the walrus said, ‘to talk of many things: of shoes and ships – and sealing wax – of cabbages and kings.’
– Lewis Carroll, Through the Looking Glass

The biggest factor to success in business is spotting an opportunity. This is what people call after-the-fact as being lucky or being “in the right place at the right time.” Opportunity arises periodically everywhere. So timing does matter, in the form of patience. But being in the right place is irrelevant. The key is to be somewhere. That is, having domain-specific knowledge is key to spotting an opportunity. It is not just technical acumen, access to capital, and connections that matter, it is having the knowledge, interest, and experience with a given industry to bet big when the time comes.

Bill Gates was a great programmer at the time and plenty smart. But he also had a desire to turn his interest in computers into a business and a deep understanding of the computer and electronics industry at the time.

Similarly, Michael Bloomberg was smart and had enough money to bootstrap his own software and data company, but the fact that he was deep into the financial data world around was crucial. Bloomberg pissed off a senior guy at Salomon and got shifted to Siberia: the back-office IT department. If you can even call it IT—think punch-cards and ticker-tape back then.

Despite what Harvard Business School would have you think, management is not a general trait that can yield success in any industry if properly developed. The reason most CEOs fail is that they lack deep knowledge of their industries. It does not take an entire life of study to become knowledgeable about an industry. Jeff Bezos learned all he could about selling books as he was starting Amazon. Bezos even turned up at local workshops for community booksellers to gain a deeper understanding of the industry he was preparing to destroy. It is called doing your homework. And, as with schoolwork, if you’re actually interested in what you’re learning about it’s actually a lot of fun.

It is not controversial to say that the greatest athletes, movie stars, directors, writers and musicians were not only students of their crafts, but also students of the history of their fields. If you have ever heard Martin Scorcese talk at length about the Golden Age of Cinema, or even Jay-Z trace the entire history of hip-hop via a single rhyme, you know what I mean.

Practice your craft. If you’re a programmer, get damn good at it. If you’re a marketing or business development specialist, do whatever it is that marketing or biz-dev people do. But immerse yourself deep within an industry (anything: cars, waste-water treatment, cosmetics) and you will be able to take advantage of opportunities that those that lack either subject-matter expertise or technical ability (broadly-defined) cannot.

Who’s saying no?

November 16, 2011 § Leave a comment

It’s been said that Steve Jobs’s greatest contribution at Apple wasn’t saying what to do, but saying what not to do.

With the introduction of a new music service[1], it’s clear Google desperately needs such a vocal naysayer. Their stated goal is “to organize the world’s information and make it universally accessible and useful.” It’s not clear that many of their products fit this aim.

This isn’t to say Google won’t be successful in the online music market. But Google must decide who or what they want to be. Do they want to be a media company? A social networking company? An internet search service? Mobile handset maker? Office application developer? Healthcare technology consultancy? The list goes on.

In his book, Winning, Jack Welch stated that General Electric had to be number one or two in every industry in which they competed. This was a way to maintain focus and profitability. It’s a lesson Google should take to heart.

[1]http://www.bloomberg.com/news/2011-11-16/google-chases-apple-music-store-eight-years-too-late-as-itunes-wins-tech.html

Where Am I?

You are currently browsing the Uncategorized category at 196sigma.