/// <summary> /// Run example /// </summary> public void Run() { // 1. Get 10 random samples of f(x) = (x * x) / 2 using continuous uniform distribution on [-10, 10] var uniform = new ContinuousUniform(-10, 10); var result = Generate.RandomMap(10, uniform, Function); Console.WriteLine(@" 1. Get 10 random samples of f(x) = (x * x) / 2 using continuous uniform distribution on [-10, 10]"); for (var i = 0; i < result.Length; i++) { Console.Write(result[i].ToString("N") + @" "); } Console.WriteLine(); Console.WriteLine(); // 2. Get 10 random samples of f(x) = (x * x) / 2 using Exponential(1) distribution and retrieve sample points var exponential = new Exponential(1); double[] samplePoints = Generate.Random(10, exponential); result = Generate.Map(samplePoints, Function); Console.WriteLine(@"2. Get 10 random samples of f(x) = (x * x) / 2 using Exponential(1) distribution and retrieve sample points"); Console.Write(@"Points: "); for (var i = 0; i < samplePoints.Length; i++) { Console.Write(samplePoints[i].ToString("N") + @" "); } Console.WriteLine(); Console.Write(@"Values: "); for (var i = 0; i < result.Length; i++) { Console.Write(result[i].ToString("N") + @" "); } Console.WriteLine(); Console.WriteLine(); // 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution var chiSquare = new ChiSquared(10); result = Generate.RandomMap2(10, chiSquare, TwoDomainFunction); Console.WriteLine(@" 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution"); for (var i = 0; i < result.Length; i++) { Console.Write(result[i].ToString("N") + @" "); } Console.WriteLine(); }
static ZergCharacter() { GOAL_PRIORITIES = new Dictionary<State, byte>(){ {State.AttackGoal, 16}, {State.FeedGoal, 8}, {State.RetreatGoal, 1}, {State.ServeGoal, 2} }; var goals = new Stack<State>(); var goalEnums = (State[])Enum.GetValues(typeof(State)); foreach (var enumValue in goalEnums) { for (int i = 0; i < GOAL_PRIORITIES[enumValue]; i++) { goals.Push(enumValue); } } int count = goals.Count; RND = new DiscreteUniform(0, count - 1); GOALS_LOOKUP_TABLE = new State[count]; var rnd = new ContinuousUniform(0.0, 1.0); while (goals.Any()) { int idx = (int)Math.Round(rnd.Sample() * (--count), 0); GOALS_LOOKUP_TABLE[idx] = goals.Pop(); } }
/// <summary> /// Build linear samples. /// </summary> /// <param name="x">X sample values.</param> /// <param name="y">Y samples values.</param> /// <param name="xtest">X test values.</param> /// <param name="ytest">Y test values.</param> /// <param name="samples">Sample values.</param> /// <param name="sampleOffset">Sample offset.</param> /// <param name="slope">Slope number.</param> /// <param name="intercept">Intercept criteria.</param> public static void Build(out double[] x, out double[] y, out double[] xtest, out double[] ytest, int samples = 3, double sampleOffset = -0.5, double slope = 2.0, double intercept = -1.0) { // Fixed-seed "random" distribution to ensure we always test with the same data var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(42)); // build linear samples x = new double[samples]; y = new double[samples]; for (int i = 0; i < x.Length; i++) { x[i] = i + sampleOffset; y[i] = (x[i]*slope) + intercept; } // build linear test vectors randomly between the sample points xtest = new double[samples + 1]; ytest = new double[samples + 1]; if (samples == 1) { // y = const xtest[0] = sampleOffset - uniform.Sample(); xtest[1] = sampleOffset + uniform.Sample(); ytest[0] = ytest[1] = (sampleOffset*slope) + intercept; } else { for (int i = 0; i < xtest.Length; i++) { xtest[i] = (i - 1) + sampleOffset + uniform.Sample(); ytest[i] = (xtest[i]*slope) + intercept; } } }
public static List<PatternClass> Create2DimTeachingVectors(double p1, double p2, IContinuousDistribution generatorFirstFeature1, IContinuousDistribution generatorFirstFeature2, IContinuousDistribution generatorSecondFeature1, IContinuousDistribution generatorSecondFeature2, int nrOfTeachingVectors) { List<PatternClass> createdTeachingVectors = new List<PatternClass>(); if (probabilityGenerator == null) { probabilityGenerator = new ContinuousUniform(0, 1); probabilityGenerator.RandomSource = new Random(DateTime.Now.Millisecond + 10); } for (int i = 0; i < nrOfTeachingVectors; i++) { double value = 0; double value1 = 0; int classNumber = CreateClass(p1, p2); if (classNumber == 1) { value = generatorFirstFeature1.Sample(); value1 = generatorSecondFeature1.Sample(); } else if (classNumber == 2) { value = generatorFirstFeature2.Sample(); value1 = generatorSecondFeature2.Sample(); } createdTeachingVectors.Add(new PatternClass(new FeatureVector(value,value1), classNumber)); } return createdTeachingVectors; }
public DropOutLayer( int in_size, int out_size, double drop_prob = 0.5, string layer_name = "", Vector <double> weights = null, Vector <double> biases = null) : base(in_size, out_size, layer_name, weights, biases) { // 擬似乱数器生成 rand = new ContinuousUniform(); // DropOut する確率 if (drop_prob > 1.0 || drop_prob < 0) { Console.WriteLine("0 < drop_prob < 1.0. drop_prob is set 0.5"); _drop_prob = 0.5; } else { _drop_prob = drop_prob; } // マスク初期化 _drop_mask = new int[out_size]; CreateDropMask(); LayerType = "DropOutLayer"; GenericsType = _activation.Type(); }
public void RejectTest() { var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(1)); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => 0.021, uniform.Sample); Assert.IsNotNull(rs.RandomSource); rs.RandomSource = uniform.RandomSource; Assert.IsNotNull(rs.RandomSource); }
public void NullRandomNumberGenerator() { var uniform = new ContinuousUniform(0.0, 1.0); uniform.RandomSource = new MersenneTwister(); var rs = new RejectionSampler<double>(x => System.Math.Pow(x, 1.7) * System.Math.Pow(1.0 - x, 5.3), x => System.Double.NegativeInfinity, uniform.Sample); rs.RandomSource = null; }
public void NoUpperBound() { var uniform = new ContinuousUniform(0.0, 1.0); uniform.RandomSource = new MersenneTwister(); var rs = new RejectionSampler<double>(x => System.Math.Pow(x, 1.7) * System.Math.Pow(1.0 - x, 5.3), x => System.Double.NegativeInfinity, uniform.Sample); double s = rs.Sample(); }
public void SampleArrayTest() { var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(1)); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => 0.021, uniform.Sample) { RandomSource = uniform.RandomSource }; rs.Sample(5); }
public void SetupDistributions() { dists = new IDistribution[8]; dists[0] = new Beta(1.0, 1.0); dists[1] = new ContinuousUniform(0.0, 1.0); dists[2] = new Gamma(1.0, 1.0); dists[3] = new Normal(0.0, 1.0); dists[4] = new Bernoulli(0.6); dists[5] = new Weibull(1.0, 1.0); dists[6] = new DiscreteUniform(1, 10); dists[7] = new LogNormal(1.0, 1.0); }
public static List<PatternClass> Create2dimSampleObject(IContinuousDistribution generator, IContinuousDistribution generator1, int count, int classNumber) { List<PatternClass> sampleObjects = new List<PatternClass>(); probabilityGenerator = new ContinuousUniform(0, 1); probabilityGenerator.RandomSource = new Random(DateTime.Now.Millisecond + 10); for (int i = 0; i < count; i++) { sampleObjects.Add(new PatternClass(new FeatureVector(generator.Sample(), generator1.Sample()), classNumber)); } return sampleObjects; }
public void RejectTest() { var uniform = new ContinuousUniform(0.0, 1.0); uniform.RandomSource = new MersenneTwister(); var rs = new RejectionSampler<double>(x => System.Math.Pow(x, 1.7) * System.Math.Pow(1.0 - x, 5.3), x => 0.021, uniform.Sample); Assert.IsNotNull(rs.RandomSource); rs.RandomSource = uniform.RandomSource; Assert.IsNotNull(rs.RandomSource); }
/// <summary> /// Uses Infer3DExactLMS to infer a random world point from a number of different projections of the point. /// It writes out the squared errors corresponding to different number of projections into a file. /// </summary> /// <param name="numProjections">the max number of projections to use.</param> /// <param name="gaussianNoiseSigma">the standard deviation of the gaussian noise to be added to the projected points.</param> public static void ShowErrorInfer3DExactLMS(int numProjections, double gaussianNoiseSigma, string fileName) { ContinuousUniform dist = new ContinuousUniform(0, 1); Normal gaussianNoise = new Normal(0, gaussianNoiseSigma); DenseMatrix worldPoint = new DenseMatrix(4,1); worldPoint = (DenseMatrix) worldPoint.Random(4,1, dist); ProjectedPoint[] projections = new ProjectedPoint[numProjections]; for (int i = 0; i < projections.Length; i++) { projections[i] = new ProjectedPoint(); projections[i].worldToImage = new DenseMatrix(3, 4); projections[i].worldToImage = (DenseMatrix)projections[i].worldToImage.Random(3, 4, dist); projections[i].projectedPoint = (projections[i].worldToImage * worldPoint); projections[i].projectedPoint += (DenseMatrix)projections[i].projectedPoint.Random(3, 1, gaussianNoise); } File.WriteAllLines(fileName, Enumerable.Range(2, numProjections) .Select(i => String.Format("{0}\t{1}", i, (worldPoint - Infer3DExactLMS(projections.Take(i))).L2Norm()))); }
static void SamplesUnchecked(System.Random rnd, double[] values, double alpha, double beta, double scale, double location) { var randThetas = new double[values.Length]; var randWs = new double[values.Length]; ContinuousUniform.SamplesUnchecked(rnd, randThetas, -Constants.PiOver2, Constants.PiOver2); Exponential.SamplesUnchecked(rnd, randWs, 1.0); if (!1.0.AlmostEqual(alpha)) { for (int i = 0; i < values.Length; i++) { var randTheta = randThetas[i]; var theta = (1.0 / alpha) * Math.Atan(beta * Math.Tan(Constants.PiOver2 * alpha)); var angle = alpha * (randTheta + theta); var part1 = beta * Math.Tan(Constants.PiOver2 * alpha); var factor = Math.Pow(1.0 + (part1 * part1), 1.0 / (2.0 * alpha)); var factor1 = Math.Sin(angle) / Math.Pow(Math.Cos(randTheta), 1.0 / alpha); var factor2 = Math.Pow(Math.Cos(randTheta - angle) / randWs[i], (1 - alpha) / alpha); values[i] = location + scale * (factor * factor1 * factor2); } } else { for (int i = 0; i < values.Length; i++) { var randTheta = randThetas[i]; var part1 = Constants.PiOver2 + (beta * randTheta); var summand = part1 * Math.Tan(randTheta); var subtrahend = beta * Math.Log(Constants.PiOver2 * randWs[i] * Math.Cos(randTheta) / part1); values[i] = location + scale * Constants.TwoInvPi * (summand - subtrahend); } } }
public void ValidateInverseCumulativeDistribution(double lower, double upper) { var n = new ContinuousUniform(lower, upper); for (var i = 0; i < 11; i++) { var x = i - 5.0; if (x <= lower) { Assert.AreEqual(lower, n.InverseCumulativeDistribution(0.0), 1e-12); Assert.AreEqual(lower, ContinuousUniform.InvCDF(lower, upper, 0.0), 1e-12); } else if (x >= upper) { Assert.AreEqual(upper, n.InverseCumulativeDistribution(1.0), 1e-12); Assert.AreEqual(upper, ContinuousUniform.InvCDF(lower, upper, 1.0), 1e-12); } else { Assert.AreEqual(x, n.InverseCumulativeDistribution((x - lower)/(upper - lower)), 1e-12); Assert.AreEqual(x, ContinuousUniform.InvCDF(lower, upper, (x - lower)/(upper - lower)), 1e-12); } } }
public void NoUpperBound() { var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(1)); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => Double.NegativeInfinity, uniform.Sample); Assert.That(() => rs.Sample(), Throws.ArgumentException); }
public void ValidateMinimum(double lower, double upper) { var n = new ContinuousUniform(lower, upper); Assert.AreEqual(lower, n.Minimum); }
public void ValidateMode(double lower, double upper) { var n = new ContinuousUniform(lower, upper); Assert.AreEqual((lower + upper) / 2.0, n.Mode); }
public void CanCreateContinuousUniform() { var n = new ContinuousUniform(); Assert.AreEqual(0.0, n.LowerBound); Assert.AreEqual(1.0, n.UpperBound); }
public void DiagonalDenseMatrixTransposeThisAndMultiply() { var dist = new ContinuousUniform(-1.0, 1.0, new SystemRandomSource(1)); Assert.IsInstanceOf<DiagonalMatrix>(Matrix<Complex>.Build.DiagonalIdentity(3, 3)); var wide = Matrix<Complex>.Build.Random(3, 8, dist); Assert.IsTrue((Matrix<Complex>.Build.DiagonalIdentity(3).Multiply(2d).TransposeThisAndMultiply(wide)).Equals(wide.Multiply(2d))); Assert.IsTrue((Matrix<Complex>.Build.Diagonal(3, 5, 2d).TransposeThisAndMultiply(wide)).Equals(wide.Multiply(2d).Stack(Matrix<Complex>.Build.Dense(2, 8)))); Assert.IsTrue((Matrix<Complex>.Build.Diagonal(3, 2, 2d).TransposeThisAndMultiply(wide)).Equals(wide.Multiply(2d).SubMatrix(0, 2, 0, 8))); var tall = Matrix<Complex>.Build.Random(8, 3, dist); Assert.IsTrue((Matrix<Complex>.Build.DiagonalIdentity(8).Multiply(2d).TransposeThisAndMultiply(tall)).Equals(tall.Multiply(2d))); Assert.IsTrue((Matrix<Complex>.Build.Diagonal(8, 10, 2d).TransposeThisAndMultiply(tall)).Equals(tall.Multiply(2d).Stack(Matrix<Complex>.Build.Dense(2, 3)))); Assert.IsTrue((Matrix<Complex>.Build.Diagonal(8, 2, 2d).TransposeThisAndMultiply(tall)).Equals(tall.Multiply(2d).SubMatrix(0, 2, 0, 3))); }
/// <summary> /// Creates the list of objects with specified class /// </summary> /// <param name="generator"></param> /// <param name="count"></param> /// <param name="classNumber"></param> /// <returns></returns> public static List<PatternClass> CreateSampleObject(int p1,int p2 ,IContinuousDistribution generator1,IContinuousDistribution generator2,int count) { List<PatternClass> sampleObjects = new List<PatternClass>(); probabilityGenerator = new ContinuousUniform(0, 1); probabilityGenerator.RandomSource = new Random(DateTime.Now.Millisecond + 10); int counter = 0; ; for (; counter < count; ) { //sampleObjects.Add(new PatternClass(new FeatureVector(generator.Sample()), classNumber)); } return sampleObjects; }
public void ValidateSkewness(double lower, double upper) { var n = new ContinuousUniform(lower, upper); Assert.AreEqual(0.0, n.Skewness); }
public void DiagonalDenseMatrixMultiply() { var dist = new ContinuousUniform(-1.0, 1.0, new SystemRandomSource(1)); Assert.IsInstanceOf<DiagonalMatrix>(Matrix<Complex32>.Build.DiagonalIdentity(3, 3)); var wide = Matrix<Complex32>.Build.Random(3, 8, dist); Assert.IsTrue((Matrix<Complex32>.Build.DiagonalIdentity(3).Multiply(2f)*wide).Equals(wide.Multiply(2f))); Assert.IsTrue((Matrix<Complex32>.Build.Diagonal(5, 3, 2f)*wide).Equals(wide.Multiply(2f).Stack(Matrix<Complex32>.Build.Dense(2, 8)))); Assert.IsTrue((Matrix<Complex32>.Build.Diagonal(2, 3, 2f)*wide).Equals(wide.Multiply(2f).SubMatrix(0, 2, 0, 8))); var tall = Matrix<Complex32>.Build.Random(8, 3, dist); Assert.IsTrue((Matrix<Complex32>.Build.DiagonalIdentity(8).Multiply(2f)*tall).Equals(tall.Multiply(2f))); Assert.IsTrue((Matrix<Complex32>.Build.Diagonal(10, 8, 2f)*tall).Equals(tall.Multiply(2f).Stack(Matrix<Complex32>.Build.Dense(2, 3)))); Assert.IsTrue((Matrix<Complex32>.Build.Diagonal(2, 8, 2f)*tall).Equals(tall.Multiply(2f).SubMatrix(0, 2, 0, 3))); }
public void DenseDiagonalMatrixMultiply() { var dist = new ContinuousUniform(-1.0, 1.0, new SystemRandomSource(1)); Assert.IsInstanceOf<DiagonalMatrix>(Matrix<Complex32>.Build.DiagonalIdentity(3, 3)); var tall = Matrix<Complex32>.Build.Random(8, 3, dist); Assert.IsTrue((tall*Matrix<Complex32>.Build.DiagonalIdentity(3).Multiply(2f)).Equals(tall.Multiply(2f))); Assert.IsTrue((tall*Matrix<Complex32>.Build.Diagonal(3, 5, 2f)).Equals(tall.Multiply(2f).Append(Matrix<Complex32>.Build.Dense(8, 2)))); Assert.IsTrue((tall*Matrix<Complex32>.Build.Diagonal(3, 2, 2f)).Equals(tall.Multiply(2f).SubMatrix(0, 8, 0, 2))); var wide = Matrix<Complex32>.Build.Random(3, 8, dist); Assert.IsTrue((wide*Matrix<Complex32>.Build.DiagonalIdentity(8).Multiply(2f)).Equals(wide.Multiply(2f))); Assert.IsTrue((wide*Matrix<Complex32>.Build.Diagonal(8, 10, 2f)).Equals(wide.Multiply(2f).Append(Matrix<Complex32>.Build.Dense(3, 2)))); Assert.IsTrue((wide*Matrix<Complex32>.Build.Diagonal(8, 2, 2f)).Equals(wide.Multiply(2f).SubMatrix(0, 3, 0, 2))); }
static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p) { var u = ContinuousUniform.Sample(rnd, 0, 1); return(InvCDF(location, scale, skew, p, u)); }
public void CanSample() { var n = new ContinuousUniform(); n.Sample(); }
public void CanCreateContinuousUniform(double lower, double upper) { var n = new ContinuousUniform(lower, upper); Assert.AreEqual(lower, n.LowerBound); Assert.AreEqual(upper, n.UpperBound); }
public void SampleTest() { var uniform = new ContinuousUniform(0.0, 1.0, new MersenneTwister()); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => 0.021, uniform.Sample) { RandomSource = uniform.RandomSource }; rs.Sample(); }
public ContGen() { _uniform = new MathNet.Numerics.Distributions.ContinuousUniform(); }
public void NoUpperBound() { var uniform = new ContinuousUniform(0.0, 1.0, new MersenneTwister()); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => Double.NegativeInfinity, uniform.Sample); Assert.Throws<ArgumentOutOfRangeException>(() => rs.Sample()); }
public void DenseDiagonalMatrixTransposeAndMultiply() { var dist = new ContinuousUniform(-1.0, 1.0, new SystemRandomSource(1)); Assert.IsInstanceOf<DiagonalMatrix>(Matrix<Complex>.Build.DiagonalIdentity(3, 3)); var tall = Matrix<Complex>.Build.Random(8, 3, dist); Assert.IsTrue(tall.TransposeAndMultiply(Matrix<Complex>.Build.DiagonalIdentity(3).Multiply(2d)).Equals(tall.Multiply(2d))); Assert.IsTrue(tall.TransposeAndMultiply(Matrix<Complex>.Build.Diagonal(5, 3, 2d)).Equals(tall.Multiply(2d).Append(Matrix<Complex>.Build.Dense(8, 2)))); Assert.IsTrue(tall.TransposeAndMultiply(Matrix<Complex>.Build.Diagonal(2, 3, 2d)).Equals(tall.Multiply(2d).SubMatrix(0, 8, 0, 2))); var wide = Matrix<Complex>.Build.Random(3, 8, dist); Assert.IsTrue(wide.TransposeAndMultiply(Matrix<Complex>.Build.DiagonalIdentity(8).Multiply(2d)).Equals(wide.Multiply(2d))); Assert.IsTrue(wide.TransposeAndMultiply(Matrix<Complex>.Build.Diagonal(10, 8, 2d)).Equals(wide.Multiply(2d).Append(Matrix<Complex>.Build.Dense(3, 2)))); Assert.IsTrue(wide.TransposeAndMultiply(Matrix<Complex>.Build.Diagonal(2, 8, 2d)).Equals(wide.Multiply(2d).SubMatrix(0, 3, 0, 2))); }
public void NullRandomNumberGenerator() { var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(1)); var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => Double.NegativeInfinity, uniform.Sample); Assert.That(() => rs.RandomSource = null, Throws.TypeOf<ArgumentNullException>()); }
public void CanSampleSequence() { var n = new ContinuousUniform(); var ied = n.Samples(); ied.Take(5).ToArray(); }
public void ValidateToString() { var n = new ContinuousUniform(1.0, 2.0); Assert.AreEqual("ContinuousUniform(Lower = 1, Upper = 2)", n.ToString()); }