Nawaz Sharif is the ex-prime minister of Pakistan again. His third stint in the job ended on July 28 after the Supreme Court disqualified him from office. Yet he could justifiably claim that he left Pakistan's economy in a better state than he found it.

When Pakistan last went to the polls, GDP had been growing about 3 percent, a dismal rate for a poor country with a burgeoning population. Inflation was above 10 percent. The budget deficit had ballooned. A crisis loomed.

Four years on, inflation is in the low single digits. The budget deficit has shrunk to a little above 4 percent of GDP. The GDP growth rate is closing in on 6 percent. Investors have taken notice.

Pakistan is not Sweden. It remains at the wrong end of global rankings of security, corruption and human development. At the last count, almost 30 percent of the population were living in poverty. Yet a crisis-prone economy has at least been put on a steadier footing. In the process, Pakistan has become something of an investment darling. It is thus a template for a particular kind of turnaround, one that reflects an upgrade in macroeconomic policy.

Bounce-back stories of this kind are quite rare, because the reforms needed are initially painful. They are typically found in what are called "frontier markets," which lie beyond even emerging markets at the riskiest edge of the investment universe. A more apt description for one that has come back from the near-dead to a tolerable life might be "phoenix economy."

Which places fit the bill? The spectrum runs from disaster zones, such as Zimbabwe (or even Venezuela), which might one day bounce back; through early-stage recovery stories that may yet falter, such as Argentina, Egypt and perhaps Nigeria; to graduates, such as Pakistan or the Philippines.

Not much unites such economies beyond a history of bad management. But there are some common themes. Politics are usually unstable. And phoenixes tend to go through the same three phases: a crisis, or "ashes" stage, as trouble comes to a head and capital flees the country; a "response" stage, where a politician grasps the reform nettle, often with IMF support; and a third "rebirth" stage, as capital is lured back by the prospect of economic recovery.

The triggers for crisis vary. A weak spot in Pakistan, for instance, was its reliance on oil imports to fuel much of its electricity supply. When the price of crude rose above $100 a barrel in 2013, the cost of the government's fuel subsidies blew out its budget deficit.

It is not enough for senior technocrats to argue for such changes. The head of government must back the reforms. Andrew Brudenell, of Ashmore, a fund manager, said that once an example is set from the top, the effect trickles down to other institutions.

A big plus is a high-profile champion for policy changes, such as Mauricio Macri, Argentina's president. Often the IMF will be brought in to lend hard-currency reserves and policy advice. Egypt began a three-year IMF program last November. Pakistan signed up to its most recent one in September 2013.

That is often the cue for the rebirth, during which capital flight goes into reverse. Attracting capital back is "somewhere on the scale between pretty important and absolutely crucial," said Paul McNamara, of GAM, a fund-management firm.

There are risks even for graduates of the phoenix-economy school. Take Pakistan. Since it reached the end of its IMF program last year, there has been a slackening of fiscal and monetary discipline and a re-emergence of old problems in its power companies. The prospects for faster growth now rest on Chinese investment in a 1,875-mile China-Pakistan Economic Corridor, or CPEC.

That puts Pakistan in a familiar spot: a reliance on foreign capital, which can turn out to be fickle and expensive. Trouble would take a while to surface. By then, investors may be talking about the big turnaround in Zimbabwe or Venezuela.