1

I want a distribution density curve that rises steeply from zero to its peak and then falls shallowly. At StatDist, a website where statistical distributions may be plotted online, I achieved this with a Gamma distribution, for example by specifying 2 for the shape (k on the website) and also 2 for the inverse scale / rate (theta on the website). The larger the inverse scale (theta), the shallower the declining slope after the peak.

Can I achieve the same with a Gamma distribution in MathNet.Numerics? If so, how? I've tried a few different ways. But I find that values after the peak always decline steeply to near zero. Varying the inverse scale merely makes the whole curve a bit smoother or more jagged in the following example, where I've specified a fixed number of samples

To see what I mean, run the code, varying the inverse scale. Then load the resulting CSV file into a spreadsheet and represent the data as a line graph.

using System.Collections.Generic;
using System.IO;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics;

// [...]

// Put the path of the folder in which the CSV file is to be saved here    
const string chartFolderPath = @"C:\Insert\folder\path\here";
const double shape = 2;
const double inverseScale = 2;
const int sampleCount = 10000;
const int bucketCount = 100;
var gamma = new Gamma(shape, inverseScale);
double[] samples = new double[sampleCount];
gamma.Samples(samples);
var histogram = new Histogram(samples, bucketCount); 
var dictionary = new Dictionary<int, double>();
for (int i = 0; i < bucketCount; i++) {
  dictionary.Add(i, histogram[i].Count);
}
string csvPath = Path.Combine(
  chartFolderPath, 
  $"Gamma Densities, Shape {shape}, Inverse Scale {inverseScale}, " + 
  $"Samples {sampleCount}, Buckets {bucketCount}.csv");
using var writer = new StreamWriter(csvPath);
foreach ((int key, double value) in dictionary) {
  writer.WriteLine($"{key},{value}");
}
SimonOR
  • 151
  • 6

1 Answers1

1

What I was missing was that I need to choose an x-axis range of densities where the inverse scale gives the curve in the line chart the shape I want. In the shape 2, inverse scale 2 example, the x-axis range of the density chart in StatDist suggested to me that I try 0 to 16. For reasons I don't understand, I actually had to use 0 to 3 to get a curve in Excel that was visually much the same as in the StatDist chart. The following code shows how I did it.

using System.Collections.Generic;
using System.IO;
using MathNet.Numerics.Distributions;

// ...

// Put the path of the folder in which the CSV file is to be saved here    
const string chartFolderPath = @"C:\Insert\folder\path\here";
const double shape = 2;
const double inverseScale = 2;
const double xMin = 0;
const double xMax = 3;
var gamma = new Gamma(shape, inverseScale);
var densities = new Dictionary<double, double>();
for (double x = xMin; x <= xMax; x += 0.1) {
  densities.Add(x, gamma.Density(x));
}
string csvPath = Path.Combine(
  chartFolderPath, 
  $"Gamma Densities, Shape {shape}, Inverse Scale {inverseScale}, " + 
  $"Range {xMin} to {xMax}.csv");
using var writer = new StreamWriter(csvPath);
foreach ((double key, double value) in densities) {
  writer.WriteLine($"{key},{value}");
}
SimonOR
  • 151
  • 6