“Poisson Regression Using C#” in Visual Studio Magazine

I wrote an article titled “Poisson Regression Using C#” in the March 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/Articles/2025/03/03/Poisson-Regression-Using-Csharp.aspx.

Poisson regression is a specific technique to predict a single numeric value. The technique can be used when the problem data is approximately Poisson distributed. The most common scenario where Poisson distributed data arises is when the variable to predict is a count of “things arriving,” for example, the number of cars that arrive at a fast-food restaurant drive-through window, every Tuesday between 9:00 a.m. and 9:10 a.m.

My article presents a complete demo of Poisson regression using the C# language. The demo program begins by loading a set of synthetic data into memory. The data looks like:

-0.8153, -0.6275, -0.3089, -0.2065, 7
 0.3409, -0.1654,  0.1174, -0.7192, 1
 0.7892, -0.8299, -0.9219, -0.6603, 1
-0.8033, -0.1578,  0.9158,  0.0663, 2
-0.3690,  0.3730,  0.6693, -0.9634, 0
. . .

The first four values on each line are the predictors. The last value is a target count to predict. The synthetic data was generated programmatically.

The output of the demo is:

learn rate = 0.0010
maxEpochs = 150
weight decay alpha = 0.000010
Start
epoch =      0 RMSE = 2.8405 acc = 0.2650
epoch =     30 RMSE = 1.0541 acc = 0.4000
epoch =     60 RMSE = 0.5313 acc = 0.7050
epoch =     90 RMSE = 0.3459 acc = 0.8700
epoch =    120 RMSE = 0.2940 acc = 0.9400
Done

Model constant and coefficients:
   1.9900
  -0.9760   1.4856  -1.9587   2.4644

Computing model accuracy
Accuracy on train = 0.9750
Accuracy on test = 1.0000

Predicting for x =
  -0.8153  -0.6275  -0.3089  -0.2065
y = 7.0263
y = 7

It’s important to realize that the Poisson regression technique only works for data that is approximately Poisson distributed. No real-life data is exactly Poisson distributed. It’s surprisingly difficult to determine how close real-life data is to being mathematically Poisson distributed, mostly because it’s difficult to define how close is close enough.



I’m not entirely sure, but I’ll bet that the counts of the number of runs scored in a baseball game are approximately Poisson distributed.

In the 1930s through the 1970s, amusement arcades were an important venue for entertainment. Some of the most popular games were baseball style games. Many hundreds of different games were produced. The Williams Company produced a new baseball game almost every year from 1947 through 1973, and sometimes more than one per year. I really liked laying these old games.

Left: Williams “Official Baseball” (1960) was one of the first to feature an upper area where baseball runners would circle the bases corresponding to the results on the playing surface. These electro-mechanical machines had absolutely beautiful designs and remarkably sophisticated inner workings.

Right: Williams “Upper Deck” (1973) featured a ramp which added a lot of excitement. Both games allowed two players, where one would pitch and one would hit. There was a lot of skill involved.


This entry was posted in Machine Learning. Bookmark the permalink.

Leave a Reply