By Tim McLaughlin

BOSTON (Reuters) - Nearly everyone, from London gamblers to U.S. money managers got it wrong. Britain's vote to leave the European Union shocked pundits, investors and politicians alike, underscoring the inherent difficulty of forecasting such rare events.

   On PredictIt, an online political events betting site operated by Victoria University in Wellington, New Zealand and U.S.-based partners, bettors had the probability of a “leave” camp win at just 16 percent on Thursday as British polls closed. Within four hours of the vote count, that had shot to 90 percent.

   The dramatic reversal caught many investors flat footed and showed how they have trouble hedging against such shocks even with the help of such tools as exchange-traded funds or computer algorithms designed to capture an electorate's social media vibe, economists, pollsters and fund managers said on Friday.

Predicting the outcome of Thursday's referendum was harder than that of a national election because there was virtually no historical data to draw on, said David Rothschild, an economist at Microsoft Research. He said pollsters also did not pay enough attention to working class and less educated voters.

The city of Sunderland, for example, home to Britain's largest car factory and considered a bellwether of the sentiment among blue-collar voters, surprised with the strength of its support for EU departure and when the result came among the first that night it sent the pound reeling.

    Rothschild, who forecast Britain would vote to remain in the bloc it joined in 1973, had a lot of company.

A person familiar with the campaign's monitoring by the U.S. intelligence agencies admitted that they were befuddled by the vote's outcome." We're all scratching our heads."

By mid-week, a rise in demand for S&P 500 put options was indicating that more and more investors were either betting on a market drop or hedging themselves against such a scenario, said David Jilek, chief investment strategist at Gateway Investment Advisers.

   Yet while opinion polls went back and forth and kept indicating a close vote, betting odds consistently favored the "remain" camp. And since odds makers fared better than pollsters in predicting the results of the last general election and the 2014 Scottish independence referendum, the 52-48 percent victory for the "leave" campaign to leave was such a shock.

THROUGH THE ROOF

Global stock markets and the British pound tumbled on Friday after a wild election night ride.

    "All through Thursday the big bets had come in favor of "remain" but as the clock ticked toward midnight the most extraordinary reversal got underway," said David Williams, a spokesman for betting firm Ladbrokes. "In the end we've managed our book well enough to make a few quid but the real story for the bookies was just how enormous the turnover was," he said.

"Never before in our 130-year history have we known a night when betting went through the roof like this."

    Betfair, a betting exchange, said the implied probability of a remain vote was 94 percent right up until the polls closed, but that changed dramatically in the wee hours of Friday when early results started coming in.

“At one point after the Sunderland result had been announced, there was more than £5-10k being traded per second on the market, which is absolutely huge,” said Naomi Totten, a spokeswoman for Betfair. (graphic: http://tmsnrt.rs/28SKhzZ)

One theory is that some of the more affluent punters were staunch supporters of the "remain" campaign and bet on it to win, according to two gambling analysts. Since EU supporters bet more money than those from the "leave" camp, that skewed the odds and public perceptions about the likely outcome, said the analysts, who did not want to be named because they were not authorized to speak about betting trends.

    Money managers, meanwhile, may be kicking themselves for failing to deploy simple hedging strategies. Charles Reinhard, head of portfolio strategy at MainStay Investments, said building up cash or buying more stable, dividend-paying stocks could have dampened Friday's stock market shock wave.

    But Don Steinbrugge, managing partner at Agecroft Partners, said hedge fund managers are rewarded for beating the market and face heavy redemptions from clients if they are cautious and take large cash positions.

    "They want you to beat the market through stock selection," he said.

    Rothschild, who also is a fellow at Columbia University’s Applied Statistics Center, said he expected forecasting to improve with a transition from polls using small, random representative samples to large Internet-based ones with rich demographic data.

    "If I have one million respondents with a large amount demographic data, I should be able to predict outcomes better, or I'm not a very good statistician," he said.    

(Reporting By Tim McLaughlin; Additional reporting by Mark Hosenball; Editing by Tomasz Janowski)