Thursday, January 14, 2010

Daniel Pink on the surprising science of motivation - TEDTalks

A friend of mine recommended this video and I liked it so much I shared it immediately with other friends. As some of them were very busy to watch the whole thing, they asked for a summary. And since the topic was still active on my mind, I decided to do it for them and later I decided to make it available to everybody on this blog.

I have to emphasize that this text is based entirely and solely on this great talk. It is a mere attempt to provide a text version and summarize the main ideas, and I have to say it doesn't come close to the quality of the talk or the performance of the speaker.



A case for rethinking how we run our businesses.

Scientists of human behavior questioned the power of incentives. These contingent motivators: If-you-do-this then you-get-that, work in some circumstances but for a lot of tasks they actually either don't work or often do harm. This is one of the most robust findings in social science and also one of the most ignored.

There's a mismatch between what science knows and what business does.

Our business operating systems, the set of assumptions and protocols beneath our businesses, how we motivate people, how we apply our human resources. It's built entirely on these extrinsic motivators. Around carrots and sticks. This is actually fine for many 20th century's tasks. But for 21st century's tasks, that mechanistic, reward-and-punishment approach often doesn't work, and often does harm.

If-then rewards work really well for unsophisticated tasks, where there's a simple set of rules and a clear destination. Rewards by their very nature, narrow our focus. Concentrate the mind. That's why they work in so many cases. But for real problems, we don't want to narrow our focus and restrict our possibilities. For example, to overcome what is called functional fixedness.

Routine, rule-based, left-brained work, certain kinds of accounting, certain kinds of computer programming has become fairly easy to outsource, fairly easy to automate. Software can do it faster, low-cost providers around the world can do it cheaper. So what really matters, are the more right-brained, creative, conceptual kinds of abilities.

Think about your work. Are the problems you face the kind of problems with a clear set of rules and a single solution? No. The rules are mystifying. The solution, if it exists at all, is surprising and not obvious. If-then rewards, don't work in that case. This is not a feeling. This is not a philosophy. This is a fact, a true fact.

Too many organization are making their decisions, their policies about talents and people., based on assumptions that are out-dated, unexamined and rooted more in folklore than in science. If we really want high performance on the definitional tasks of the 21st century the solution is not to do more of the wrong thing: to entice people with the sweet carrot or threaten them with the sharper stick. We need a whole new approach.

The good news about all this is that the scientists who've been studying human motivation have given us this new approach. It's an approach built much more around intrinsic motivation. Around the desire to do things because they matter, because we like it, because they're interesting, because they're part of something important.

That new operating system for our businesses revolves around 3 elements: autonomy, mastery and purpose.
Autonomy: the urge to direct our own lives.
Mastery: the desire to get better and better at something that matters.
Purpose: the yearning to do what we do in the service of something larger than ourselves.

Today, we're going to talk only about autonomy.

In the 20th century, we came up with this idea about management. Management didn't emanate from nature, it's like: it's not a tree, it's a television set. Somebody invented it. And it doesn't mean it's going to work forever. Traditional notions of management is great if you want compliance. But if you want engagement, self-direction works better.

Let me give you an example of some kinds of radical notions of self-directions. You don't see a lot of it, but you see the first stirrings of something really interesting. Because what it means is: paying people adequately and fairly, absolutely, getting the issue of money out of the table, and then giving people much of autonomy. The 20% time, done famously at Google, where engineers can work 20% of their time on anything they want. They have autonomy over their time, their tasks, their teams, their techniques: radical amounts of autonomy. And at Google, as many of you know, about half the new products in a typical years are burst during that 20% time like: Gmail, Orkut and News.

Another more radical example of autonomy: something called the results only work environment or ROWE. In a ROWE people don't have schedules. They show up when they want, they don't have to be at the office at a certain time or any time. They just have to get the work done. How they do it, when they do it, where they do it is totally up to them. Meetings in these kinds of environments are optional. What happens? almost across the board: productivity goes up, worker-enga worker-satis turn-over goes down.

Autonomy, master, and purpose. These are the building blocks of a new way of doing things.

Now, some of you might look at this and say: mmm, that sounds nice but it's Utopian. And I say nope. I have proof. In mid 1990's Microsoft started an encyclopedia called Encarta. They deployed all the right incentives. They payed professionals to write and edit thousands of articles. Well compensated managers oversaw the whole thing to make sure it came on-budget and on-time. Few years later, a new encyclopedia get started. A different model. Do it for fun. No one gets payed a cent, or a euro or a yen. Do it because you like to do it. Now if you had, just 10 years ago, if you had gone to an economist, any where, and said: hey, I have these 2 different models for creating an encyclopedia, if they went head to head, who'd win? 10 years ago, you couldn't not find a single sober economist on planet earth who'd predicted the Wikipedia model.

Intrinsic motivators vs. extrinsic motivators. Autonomy, mastery and purpose vs. carrots and sticks. And who wins?

To wrap-up (17:20)

There's a mismatch between what science knows and what business does. And here's what science knows:
1) Those 20th century's rewards, those motivators, we think are the natural part of business: Do work, but only in a surprisingly narrow band of circumstances.
2) Those if-then rewards, often destroy creativity.
3) The secret to high performance isn't rewards and punishments, but this unseen intrinsic drive. The drive to do things for their own sake. The drive to do things because they matter.

And here's the best part. We already know this. The science confirms what we know in our hearts. So, if we repair this mismatch between what science knows and what business does. If we bring our notions of motivation into the 21st century. If we get past this lazy, dangerous, ideology of carrots and sticks, we can strengthen our businesses, we can solve a lot of our real problems and maybe, maybe we can change the world.

Thursday, December 31, 2009

Simulation and Kalman filter for a 3rd order kinematic model

Using a Discrete Wiener Process Acceleration (DWPA) model, we illustrate the usage of the Java implementation of the Kalman filter we presented in the previous post. The model we employ here is taken from Estimation with Applications to Tracking and Navigation.

We start by building the Kalman filter using this method:

public static KalmanFilter buildKF(double dt, double processNoisePSD, double measurementNoiseVariance) {
KalmanFilter KF = new KalmanFilter();

//state vector
KF.setX(new Matrix(new double[][]{{0, 0, 0}}).transpose());

//error covariance matrix
KF.setP(Matrix.identity(3, 3));

//transition matrix
KF.setF(new Matrix(new double[][]{
{1, dt, pow(dt, 2)/2},
{0, 1, dt},
{0, 0, 1}}));

//input gain matrix
KF.setB(new Matrix(new double[][]{{0, 0, 0}}).transpose());

//input vector
KF.setU(new Matrix(new double[][]{{0}}));

//process noise covariance matrix
KF.setQ(new Matrix(new double[][]{
{ pow(dt, 5) / 4, pow(dt, 4) / 2, pow(dt, 3) / 2},
{ pow(dt, 4) / 2, pow(dt, 3) / 1, pow(dt, 2) / 1},
{ pow(dt, 3) / 1, pow(dt, 2) / 1, pow(dt, 1) / 1}}
).times(processNoisePSD));

//measurement matrix
KF.setH(new Matrix(new double[][]{{1, 0, 0}}));

//measurement noise covariance matrix
KF.setR(Matrix.identity(1, 1).times(measurementNoiseVariance));

return KF;
}
Then, we simulate the accelerating target by generating random acceleration increments and updating the velocity and displacement accordingly. We also simulate the noisy measurements and feed them to the filter. We repeat this step many times to challenge the performance of the filter. Finally, we compare the state estimate provided by the filter to the true simulated state and the last measurement.
import static java.lang.Math.pow;
import java.util.Random;
import Jama.Matrix;

/**
* This work is licensed under a Creative Commons Attribution 3.0 License.
*
* @author Ahmed Abdelkader
*/

public static void main(String[] args) {
//model parameters
double x = Math.random(), vx = Math.random(), ax = Math.random();

//process parameters
double dt = 1.0 / 100.0;
double processNoiseStdev = 3;
double measurementNoiseStdev = 5;
double m = 0;

//noise generators
Random jerk = new Random();
Random sensorNoise = new Random();

//init filter
KalmanFilter KF = buildKF(dt, pow(processNoiseStdev, 2)/2, pow(measurementNoiseStdev, 2));
KF.setX(new Matrix(new double[][]{{x}, {vx}, {ax}}));

//simulation
for(int i = 0; i < 1000; i++) {
//model update
ax += jerk.nextGaussian() * processNoiseStdev;
vx += dt * ax;
x += dt * vx + 0.5 * pow(dt, 2) * ax;

//measurement realization
m = x + sensorNoise.nextGaussian() * measurementNoiseStdev;

//filter update
KF.predict();
KF.correct(new Matrix(new double[][]{{m}}));
}

//results
System.out.println("True:"); new Matrix(new double[][]{{x}, {vx}, {ax}}).print(3, 1);
System.out.println("Last measurement:\n\n " + m + "\n");
System.out.println("Estimate:"); KF.getX().print(3, 1);
System.out.println("Estimate Error Cov:"); KF.getP().print(3, 3);
}
Depending on how familiar you are with target tracking and Kalman filters, you may find it interesting to consider the following:
  • The error covariance reaches a steady state after a certain number of steps. By studying the steady state error, we can obtain a good idea about the performance of the filter. In addition, this can be used to precalculate the steady state Kalman gain to avoid performing many calculations at each step.
  • If you were to run this simulation yourself, you should experiment with differnet values of processNoiseStdev and measurementNoiseStdev and observe the steady state covariance and absolute error.
  • The simulation uses a position sensor to measure the current location of the target. This may not be available for you, specially in the case of inertial navigation. We will try to look into that in a later post.

Sunday, December 6, 2009

Java implementation of the Kalman Filter using JAMA

This is a very clear and straight forward implementation of the Discrete Kalman Filter Algorithm in the Java language using the JAMA package. I wrote this code for testing and simulation purposes.

import Jama.Matrix;

/**
* This work is licensed under a Creative Commons Attribution 3.0 License.
*
* @author Ahmed Abdelkader
*/

public class KalmanFilter {
protected Matrix X, X0;
protected Matrix F, B, U, Q;
protected Matrix H, R;
protected Matrix P, P0;

public void predict() {
X0 = F.times(X).plus(B.times(U));

P0 = F.times(P).times(F.transpose()).plus(Q);
}

public void correct(Matrix Z) {
Matrix S = H.times(P0).times(H.transpose()).plus(R);

Matrix K = P0.times(H.transpose()).times(S.inverse());

X = X0.plus(K.times(Z.minus(H.times(X0))));

Matrix I = Matrix.identity(P0.getRowDimension(), P0.getColumnDimension());
P = (I.minus(K.times(H))).times(P0);
}

//getters and setters go here
}
To use this class you will need to create a new instance, set the system matrices (F, B, U, Q, H, R) and initialize the state and error covariance matrices (X, P). When you're done building your filter, you can start using it right away as you might expect. You will need to do the following for each step: 1) project ahead your state by calling predict. 2) update your prediction by creating a measurement matrix with the measurements you received at that step and passing it to the filter through a correct call. I am planning to post a tutorial of this in the next few days. (Update: tutorial posted here)

For more information about the Kalman Filter algorithm, I highly recommend you refer to the webpage maintained by Greg Welch and Gary Bishop. In particular, check their excellent introduction to this interesting topic.

Thursday, December 3, 2009

Parsing a simple XML node on iPhone

I wanted to parse some simple XML response from a server and I really wouldn't bother to use NSXMLParser or libxml2 as discussed here on stackoverflow. I get a single node and I just wanted to get the content. I ended up with this utility method:

+ (NSString *)xmlNodeContent:(NSData *)xmlData {
NSString *node = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
NSString *content = @"";
if([node length] > 0) {
int start = [node rangeOfString:@">"].location + 1;
int end = [node rangeOfString:@"<" options:NSBackwardsSearch].location;
content = [node substringWithRange:NSMakeRange(start, end - start)];
}
[node release];
return content;
}

Monday, November 23, 2009

Multi-line UITableViewCell using UILabel

No need for UITextViews or custom UITableViewCells. You can use standard UITableViewCellStyles and make the detailTextLabel accept multiple lines and specify its line break mode. The code would be:

static NSString *CellIdentifier = @"MyCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Label';
cell.detailTextLabel.text = @"Multi-Line\nText";
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
You will also need to return a suitable height for the multi-line cell. A height of (44.0 + (numberOfLines - 1) * 19.0) should work fine.

Update: As Vaibhav mentions in the comments, you can use variants of sizeWithFont from the NSString UIKit Additions to get the required height. I guess sizeWithFont:forWidth:lineBreakMode is the one to use here. Thanks for your input!

Thursday, November 12, 2009

Moving UITextFields over the keyboard without a UIScrollView

I had a UIViewController with a couple of UITextFields attached to its default UIView and everything was fine. Later on, I added a couple more text fields, so I had to make sure all text fields are displayed properly when the keyboard shows up. I thought about using a UIScrollView or maybe a UITableView which scrolls naturally, but I thought I didn't have to change my controller just for that. I found these 2 posts [1, 2] on stackoverflow pretty useful, but I still had to tweak the code a little and here's what I got:

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[activeField resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeField = textField;
[self setViewMovedUp:YES];
return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[activeField resignFirstResponder];
[self setViewMovedUp:NO];
activeField = nil;
return YES;
}

- (void)setViewMovedUp:(BOOL)movedUp {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

CGRect viewFrame = self.view.frame;
CGRect textFieldFrame = [activeField convertRect:[activeField bounds] toView:self.view];

if (movedUp) {
viewFrame.origin.y = -textFieldFrame.origin.y/1.8;
} else {
viewFrame.origin.y = 0;
}
self.view.frame = viewFrame;

[UIView commitAnimations];
}

Let me explain a couple of things here, first: activeField is a UIControl* instance variable I added to my UIVeiwController. second: you can replace (textFieldFrame.origin.y/1.8) with any function of (textFieldFrame.origin.y) that would do the job. Maybe you can try assigning certain offsets for each range of y values, but I preferred this neat form and the result was neat too.

Tuesday, May 5, 2009

Tracking swine flu with Google Maps

FluTracker is a website that tracks the progress of swine flu using data from official sources, news reports and user-contributions*. The site was built using technology provided by Rhiza Labs and Google. This post on Mashable also has great information about tracking swine flu online.

Tracking epidemics was one of the first applications of geospatial information. In 1854, John Snow depicted a cholera outbreak in London using points to represent the locations of some individual cases, as mentioned on Wikipedia.

It also looks like someone is trying to make business out of this site. Check the bold section just above the map. Seems like everything can be exploited to make money.

I hope we don't see more circles, at least in our region. But I can't just stop here. I think that people, we, are very worried about pandemics and death. I mean, what if we track every sin all around the world?

(*) The main sources of information [on the web] today. Watch this great video of Eric Schmidt, Chairman and CEO of Google Inc., at the Newspaper Association of America on April 7, 2009.