简体   繁体   English

如何在 MathNet.Numerics 中使用 Gamma 分布

[英]How to use a Gamma distribution in MathNet.Numerics

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).StatDist ,一个可以在线绘制统计分布的网站,我通过 Gamma 分布实现了这一点,例如,通过为形状指定 2(网站上的 k)和反向比例/比率(网站上的 theta)指定 2 . The larger the inverse scale (theta), the shallower the declining slope after the peak.反比例(theta)越大,峰值后的下降斜率越浅。

Can I achieve the same with a Gamma distribution in MathNet.Numerics?我可以通过 MathNet.Numerics 中的 Gamma 分布实现同样的效果吗? 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.然后将生成的 CSV 文件加载到电子表格中,并将数据表示为折线图。

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}");
}

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.我缺少的是我需要选择一个 x 轴的密度范围,其中反比例为折线图中的曲线提供我想要的形状。 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.在形状 2,反比例尺 2 示例中, StatDist中密度图的 x 轴范围建议我尝试 0 到 16。由于我不明白的原因,我实际上不得不使用 0 到 3 来获得一个Excel 中的曲线在视觉上与 StatDist 图表中的大致相同。 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}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM