/// <summary> /// 获取多个随机整型 /// </summary> /// <param name="count">整型个数</param> /// <param name="minValue">最大值</param> /// <param name="maxValue">最小值</param> /// <param name="_Distribution">分布规律</param> public static int[] Ints(int count, int minValue, int maxValue, DistributionFunction _Distribution = DistributionFunction.Uniform) { int[] ints = new int[count]; decimal a = 0, b = 0, c = 0; if (_Distribution == DistributionFunction.Parabola) { decimal d = ((decimal)Math.Pow(maxValue, 3) - (decimal)Math.Pow(minValue, 3)) / 3 - (maxValue + minValue) * ((decimal)Math.Pow(maxValue, 2) - (decimal)Math.Pow(minValue, 2)) / 2 + (decimal)Math.Pow(maxValue + minValue, 2) * (maxValue - minValue) / 4; a = 1 / d; b = -a * (minValue + maxValue); c = a * (decimal)Math.Pow(minValue + maxValue, 2) / 4; } for (int i = 0; i < count; i++) { switch (_Distribution) { case DistributionFunction.Uniform: ints[i] = Int(minValue, maxValue + 1); break; case DistributionFunction.Possion: ints[i] = PossionVariable_Int((maxValue + minValue) / 2.0, minValue, maxValue); break; case DistributionFunction.Parabola: ints[i] = ParabolaVariable_Int(a, b, c, minValue, maxValue); break; } } return(ints); }
//For Puasson: 0, 0.220, 0.553, 0.805, 0.932, 0.980, 0.995, 1 private static IEnumerable <double> GetDistributionFunctionValues(DistributionFunction function, int amount) { for (int i = 0; i < amount + 1; i++) { yield return(function((double)i / amount)); } }
public ContinuousDistribution(DistributionFunction pdf, float min, float max, DistributionFunction cdf = null) { this.pdf = pdf; this.cdf = cdf; this.min = min; this.max = max; }
public double[] GenerateSamples(int howMany) { var resArray = new double[howMany]; if (DistributionFunction == null) { return(new double[howMany]); } var res = DistributionFunction.Generate(howMany, resArray); return(res); }
internal WeaponBarrelsBase(BinaryReader binaryReader) { this.flags = (Flags)binaryReader.ReadInt32(); this.roundsPerSecond = binaryReader.ReadRange(); this.accelerationTimeSeconds = binaryReader.ReadSingle(); this.decelerationTimeSeconds = binaryReader.ReadSingle(); this.barrelSpinScale = binaryReader.ReadSingle(); this.blurredRateOfFire = binaryReader.ReadSingle(); this.shotsPerFire = binaryReader.ReadInt32(); this.fireRecoveryTimeSeconds = binaryReader.ReadSingle(); this.softRecoveryFraction = binaryReader.ReadSingle(); this.magazine = binaryReader.ReadShortBlockIndex1(); this.roundsPerShot = binaryReader.ReadInt16(); this.minimumRoundsLoaded = binaryReader.ReadInt16(); this.roundsBetweenTracers = binaryReader.ReadInt16(); this.optionalBarrelMarkerName = binaryReader.ReadStringID(); this.predictionType = (PredictionType)binaryReader.ReadInt16(); this.firingNoise = (FiringNoiseHowLoudThisWeaponAppearsToTheAI)binaryReader.ReadInt16(); this.accelerationTimeSeconds0 = binaryReader.ReadSingle(); this.decelerationTimeSeconds0 = binaryReader.ReadSingle(); this.damageError = binaryReader.ReadRange(); this.accelerationTimeSeconds1 = binaryReader.ReadSingle(); this.decelerationTimeSeconds1 = binaryReader.ReadSingle(); this.invalidName_ = binaryReader.ReadBytes(8); this.minimumErrorDegrees = binaryReader.ReadSingle(); this.errorAngleDegrees = binaryReader.ReadRange(); this.dualWieldDamageScale = binaryReader.ReadSingle(); this.distributionFunction = (DistributionFunction)binaryReader.ReadInt16(); this.projectilesPerShot = binaryReader.ReadInt16(); this.distributionAngleDegrees = binaryReader.ReadSingle(); this.minimumErrorDegrees0 = binaryReader.ReadSingle(); this.errorAngleDegrees0 = binaryReader.ReadRange(); this.firstPersonOffsetWorldUnits = binaryReader.ReadVector3(); this.damageEffectReportingType = (DamageEffectReportingType)binaryReader.ReadByte(); this.invalidName_0 = binaryReader.ReadBytes(3); this.projectile = binaryReader.ReadTagReference(); this.eh = new WeaponBarrelDamageEffectStructBlock(binaryReader); this.ejectionPortRecoveryTime = binaryReader.ReadSingle(); this.illuminationRecoveryTime = binaryReader.ReadSingle(); this.heatGeneratedPerRound01 = binaryReader.ReadSingle(); this.ageGeneratedPerRound01 = binaryReader.ReadSingle(); this.overloadTimeSeconds = binaryReader.ReadSingle(); this.angleChangePerShot = binaryReader.ReadRange(); this.accelerationTimeSeconds2 = binaryReader.ReadSingle(); this.decelerationTimeSeconds2 = binaryReader.ReadSingle(); this.angleChangeFunction = (AngleChangeFunctionFunctionUsedToScaleBetweenInitialAndFinalAngleChangePerShot)binaryReader.ReadInt16(); this.invalidName_1 = binaryReader.ReadBytes(2); this.invalidName_2 = binaryReader.ReadBytes(8); this.invalidName_3 = binaryReader.ReadBytes(24); this.firingEffects = ReadBarrelFiringEffectBlockArray(binaryReader); }
private static void CheckPearsonCriteria(List <double> list, DistributionFunction function) { var distributionFunctionValues = GetDistributionFunctionValues(function, IntervalsAmount).ToList(); var probabilityFunctionValues = GetProbabilityFunctionValues(distributionFunctionValues).ToList(); var intervals = GetAmountByIntervals(list).ToList(); double value = 0; for (int i = 0; i < IntervalsAmount; i++) { value += Math.Pow(intervals[i] - N * probabilityFunctionValues[i], 2) / (N * probabilityFunctionValues[i]); } Console.WriteLine($"Pearson criterion: {DisplayCriteriaResults(value, Pearson49Quantiles[Epsilon])}"); }
//生成无重复的多个整型 public static int[] Ints_Unrepeating(int count, int minValue, int maxValue, DistributionFunction _Distribution = DistributionFunction.Uniform) { int[] ints = new int[count]; //输出的整型数组 int[] ranges = new int[maxValue - minValue + 1]; //将取值范围内所有的数组都排列出来 for (int i = minValue; i <= maxValue; i++) { ranges[i - minValue] = i; } double lambda = 0; if (_Distribution == DistributionFunction.Possion) { lambda = (minValue + maxValue) / 2.0; } for (int i = 0; i < count; i++) { int index = Int(minValue, maxValue) - minValue; //取值范围内的下标 if (_Distribution == DistributionFunction.Uniform) { index = Int(minValue, maxValue) - minValue; //取值范围内的下标 } else if (_Distribution == DistributionFunction.Possion) { index = PossionVariable_Int(lambda, minValue, maxValue); } else { index = ParabolaVariable_Int(minValue, maxValue); } ints[i] = ranges[index]; //赋值给输出的数组 ranges[index] = ranges[maxValue - minValue]; //替换当前位置的值,防止重复 maxValue--; } return(ints); }
public static void CDFSampling() { var cdfSampleTestCount = 2000000; var pdfSizes = new[] { 5, 10, 25, 50, 100, 250 }; var rnd = new RandomSystem(0); foreach (var pz in pdfSizes) { var pdf = new double[pz].SetByIndex(i => rnd.UniformDouble()); var sum = pdf.Sum(); var cdf = new DistributionFunction(pdf); Assert.True(cdf.Norm.ApproximateEquals(sum, 1e-10)); rnd.ReSeed(0); var sc1 = new int[pdf.Length]; Report.BeginTimed("Sample Binary Search"); for (int i = 0; i < cdfSampleTestCount; i++) { var ind = cdf.Sample(rnd.UniformDouble()); sc1[ind]++; } Report.End(); rnd.ReSeed(0); var sc2 = new int[pdf.Length]; Report.BeginTimed("Sample Linear Search"); for (int i = 0; i < cdfSampleTestCount; i++) { var ind = SamplePDFLinear(pdf, sum, rnd.UniformDouble()); sc2[ind]++; } Report.End(); for (int i = 0; i < pdf.Length; i++) { Assert.True(sc1[i] == sc2[i]); } } }
private static void CheckKolmogorovCriteria(List <double> list, DistributionFunction function) { var distributionFunctionValues = GetDistributionFunctionValues(function, list.Count).ToList(); double maxDiff = 0; for (int i = 0; i < list.Count; i++) { if (Math.Abs(list[i] - distributionFunctionValues[i]) > maxDiff) { maxDiff = Math.Abs(list[i] - distributionFunctionValues[i]); } if (Math.Abs(list[i] - distributionFunctionValues[i + 1]) > maxDiff) { maxDiff = Math.Abs(list[i] - distributionFunctionValues[i + 1]); } } var value = maxDiff * Math.Sqrt(list.Count); Console.WriteLine($"Kolmogorov criterion: {DisplayCriteriaResults(value, KolmogorovQuantiles[Epsilon])}"); }
/// <summary> /// Shows a new chart in a default form. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <remarks> /// Equivalent to: /// <code> /// NMathStatsChart.Show( ToChart( dist, function ) ); /// </code> /// </remarks> public static void Show( PoissonDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { Show( ToChart( dist, function ) ); }
/// <summary> /// Shows a new chart in a default form. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <remarks> /// Equivalent to: /// <code> /// NMathStatsChart.Show( ToChart( dist, function ) ); /// </code> /// </remarks> public static void Show( NegativeBinomialDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { Show( ToChart( dist, function ) ); }
/// <summary> /// Shows a new chart in a default form. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <remarks> /// Equivalent to: /// <code> /// NMathStatsChart.Show( ToChart( dist, function ) ); /// </code> /// </remarks> public static void Show( GeometricDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { Show( ToChart( dist, function ) ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, GammaDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "GammaDistribution", String.Format("\u03B1={0}, \u03B2={1}", dist.Alpha, dist.Beta) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, TDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "TDistribution", String.Format("df={0}", dist.DegreesOfFreedom) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, PoissonDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { List<string> titles = new List<string>() { "PoissonDistribution", String.Format("\u03BB={0}", dist.Mean) }; UpdateDiscreteDistribution( ref chart, dist, titles, function ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, NormalDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "NormalDistribution", String.Format("\u03BC={0}, \u03C3\u00B2={1}", dist.Mean, dist.Variance) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, NegativeBinomialDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { List<string> titles = new List<string>() { "NegativeBinomialDistribution", String.Format("n={0}, p={1}", dist.N, dist.P) }; UpdateDiscreteDistribution( ref chart, dist, titles, function ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, JohnsonDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "JohnsonDistribution", String.Format("\u03B3={0}, \u03B4={1}, \u03BE={2}, \u03BB={3}", dist.Gamma, dist.Delta, dist.Xi, dist.Lambda) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, GeometricDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { List<string> titles = new List<string>() { "GeometricDistribution", String.Format("p={0}", dist.P) }; UpdateDiscreteDistribution( ref chart, dist, titles, function ); }
/// <summary> /// Shows a new chart in a default form. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <remarks> /// Equivalent to: /// <code> /// NMathStatsChart.Show( ToChart( dist, function, numInterpolatedValues ) ); /// </code> /// </remarks> public static void Show( WeibullDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { Show( ToChart( dist, function, numInterpolatedValues ) ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, UniformDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "UniformDistribution", String.Format("lower={0}, upper={1}", dist.LowerLimit, dist.UpperLimit) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
public override string ToString() { return($"Distribution: {DistributionFunction?.GetType()}, Mean: {Mean}, STD: {StDeviation}"); }
/// <summary> /// Returns a new line chart plotting the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> public static ChartControl ToChart( WeibullDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { ChartControl chart = GetDefaultChart(); Update( ref chart, dist, function, numInterpolatedValues ); return chart; }
/// <summary></summary> protected static void UpdateContinuousDistribution( ref ChartControl chart, ProbabilityDistribution dist, List<string> titles, DistributionFunction function, int numInterpolatedValues ) { string xTitle = "x"; string yTitle; double xmin = dist.InverseCDF( 0.0001 ); double xmax = dist.InverseCDF( 0.9999 ); OneVariableFunction f; if( function == DistributionFunction.PDF ) { yTitle = "Probability Density Function"; f = new OneVariableFunction( new Func<double, double> ( delegate( double x ) { return dist.PDF( x ); } ) ); } else { yTitle = "Cumulative Distribution Function"; f = new OneVariableFunction( new Func<double, double> ( delegate( double x ) { return dist.CDF( x ); } ) ); } ChartSeries series = BindXY( f, xmin, xmax, numInterpolatedValues, ChartSeriesType.Line, ChartSymbolShape.None ); Update( ref chart, series, titles, xTitle, yTitle ); }
/// <summary> /// Returns a new line chart plotting the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// </summary> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <returns>A new chart.</returns> public static ChartControl ToChart( PoissonDistribution dist, DistributionFunction function = DistributionFunction.PDF ) { ChartControl chart = GetDefaultChart(); Update( ref chart, dist, function ); return chart; }
/// <summary></summary> protected static void UpdateDiscreteDistribution( ref ChartControl chart, ProbabilityDistribution dist, List<string> titles, DistributionFunction function ) { string xTitle = "x"; string yTitle; int xmin = (int)Math.Floor( dist.InverseCDF( 0.0001 ) ); int xmax = (int)Math.Ceiling( dist.InverseCDF( 0.9999 ) ); DoubleVector x = new DoubleVector( xmax - xmin + 1, xmin, 1 ); DoubleVector y; if( function == DistributionFunction.PDF ) { yTitle = "Probability Mass Function"; y = x.Apply( new Func<double, double>( dist.PDF ) ); } else { yTitle = "Cumulative Distribution Function"; y = x.Apply( new Func<double, double>( dist.CDF ) ); } ChartSeries series = BindXY( x, y, ChartSeriesType.Column, ChartSymbolShape.None ); Update( ref chart, series, titles, xTitle, yTitle ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, WeibullDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "WeibullDistribution", String.Format("scale={0}, shape={1}", dist.Scale, dist.Shape) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }
/// <summary> /// Updates the given chart with the specified distribution. /// </summary> /// <param name="chart">A chart.</param> /// <param name="dist">The distribution.</param> /// <param name="function">The distribution function to plot.</param> /// <param name="numInterpolatedValues">The number of interpolated values.</param> /// <returns>A new chart.</returns> /// <remarks> /// Plots the specified function of the given distribution for 0.0001 <= p <= 0.9999. /// <br/> /// Titles are added only if chart does not currently contain any titles. /// <br/> /// chart.Series[0] is replaced, or added if necessary. /// </remarks> public static void Update( ref ChartControl chart, ExponentialDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 ) { List<string> titles = new List<string>() { "ExponentialDistribution", String.Format("\u03BB={0}", dist.Lambda) }; UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues ); }