A Chinese immigrant who stole trade secrets from the Chicago Merc worth an estimated $50 million was spared prison Tuesday by a federal judge who cited his otherwise “exemplary life.”

Chunlai Yang, 50, of Libertyville, was instead sentenced to just four years probation for stealing software that underpinned the CME Group’s Globex trading platform.

Yang, who worked as a high-ranking programmer for the Merc from 2000 until his arrest in 2011, pleaded guilty in 2012 to the theft, admitting he was trying to create a similar product in China when he illegally downloaded more than 10,000 computer source code files.

Prosecutors wanted him locked up for as long as six years — an unusually stiff sentence for a trade secrets case. They argued the code Yang stole was worth as much as $50 million and that a failure to punish Yang would encourage a nation that has “a reputation” for stealing trade secrets to commit more acts of corporate espionage.

But Yang’s attorney, Ed Genson, said that the CME group suffered no actual loss, arguing that the “foolish” Yang changed his mind before he passed the stolen code on and deserved a sentence of probation or house arrest.

Many of those in a courtroom packed with Yang’s relatives and supporters broke down in tears when Yang bowed to all four corners of the courtroom and gave an emotional apology, telling the judge, “I made a terrible mistake — I’m extremely regretful and ashamed for what I did.”

U.S. District Judge John Darrah agreed with Assistant U.S. Attorney Barry Jonas that intellectual property rights are “the cornerstone of American industry” and that the main goal of the sentence should be to deter like-minded criminals.

But the judge said that the case was a classic example of “when good people do bad things,” adding that Yang’s significant charity work in the Chinese immigrant community showed the “exemplary life” he’d led until the theft.

Court papers filed in advance of Tuesday’s hearing show that Yang, who was among a group of Chinese engineers who met with then Chinese President Hu Jintao when he visited Chicago in 2011, won an award from Secretary of State Jesse White for his community work in 2010.

And among the dozens of letters of support sent to the judge testifying to Yang’s good character and community service was one from Congressman Danny Davis.

Though the letter is sealed, Davis acknowledged in it that he had thrown a party for Yang’s daughter when she was accepted as a student at Harvard, Genson wrote in court papers.