Crude oil and gasoline prices have taken us on a wild and crazy ride over the past two years. Crude oil prices rose from $51 a barrel in January 2007 to $145 in July 2008, a jump of 184 percent. Then prices plunged to $30 a barrel in December 2008, just five months later.

Gasoline prices have followed suit. They too rose rapidly between January 2007 and July 2008, from $2.21 a gallon to a high of $4.17, a rise of 89 percent. Then they headed straight down, hitting a low of $1.67 in December 2008.

How can we explain this crazy roller-coaster ride?

Oil prices soared in 2007-08 because massive increases in demand bumped up against an unchanged supply, according to James Hamilton, an economist at the University of California, San Diego.

The biggest jump in demand was in China, where oil consumption rose by 1.2 million barrels a day between 2005 and 2008. Meanwhile, the global supply of oil stagnated — production barely changed. Saudi Arabia, the most important oil exporter, was a big part of this story. In the past, the Saudis opened the spigot when prices rose rapidly, but not this time: Saudi production actually fell in 2007.

Because the Chinese usage of crude oil was taking off, while supply did not change, usage had to fall in the rest of the world. And it did, especially in Europe, Japan and the U.S. To persuade consumers of oil in these areas to decrease their consumption, prices simply had to rise. And indeed they did — quite steeply.

What led to the amazing collapse in the international oil market after July 2008, when crude oil prices plummeted 79 percent in just five months? The worldwide recession worsened dramatically in the last half of 2008, leading to a big drop in personal incomes. Faced with lower incomes and very high prices for oil and gasoline, people reduced their demand for oil products. They drove less and bought fewer new cars. And as you would guess, sales of gas-guzzling SUVs dropped more than sales of small fuel-efficient cars.

The roller-coaster ride up and the ride down may also have been fueled by speculation, Hamilton suggests. Traders in the oil market saw that between 2001 and 2008 crude oil prices moved steadily up. By increasing their purchases of oil contracts in the futures market, they made larger and larger bets that prices would keep on rising. These very bets drove up oil prices, both the futures price and the price of oil for immediate delivery.

Oil producing countries, such as Saudi Arabia, also probably expected the 2001-08 price increases to continue. To take advantage of expected higher prices, they may have decided not to raise oil production immediately, but to leave oil in the ground and sell it in the future. This would explain the Saudi decision to decrease pro-duction in 2007 despite a rise in prices.

This speculative price bubble eventually burst, driving oil prices down after July 2008 at record-breaking speed. The bursting of the bubble reinforced the drop in consumer demand, and together these factors explain the price collapse of late 2008.

What about oil prices in the coming years? While we will surely see continuing fluctuations in oil and gasoline prices, the dominant trend likely will be upward. Demand for oil products in China and elsewhere in the world is likely to keep on rising. Oil exporting countries, knowing that their reserves are gradually falling, and expecting oil prices to climb, will not rush to increase production. To persuade them to boost production, we will have to pay higher gasoline prices.

Since December, gasoline prices have already risen substantially — from $1.67 a gallon to $2.52. This short-term trend fits in nicely with the prediction that prices will rise over the long run.

This story leads to two important lessons. First, buyers should think hard before buying a low-mileage truck or car. Further, as a nation we should boost research on cheap, renewable sources of energy. Protection of our fragile environment is by no means the only powerful motive for doing this.

Edwin Dean, an economist and seasonal resident of Vinalhaven, writes monthly about economic issues.