public override void ShakeLocalParameters(IRandom random, double shakingFactor) { // 50% additive & 50% multiplicative (override of functionality of base class because of a BUG) if (random.NextDouble() < 0.5) { double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma); Weight = Weight + x * shakingFactor; } else { double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma); Weight = Weight * x; } if (random.NextDouble() < Symbol.VariableChangeProbability) { var oldName = VariableName; VariableName = Symbol.VariableNames.SampleRandom(random); // reinitialize weights if variable has changed (similar to FactorVariableTreeNode) if (oldName != VariableName) { Weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma); } } variableValue = Symbol.GetVariableValues(VariableName).SampleRandom(random); }
/// <summary> /// Performs an adaptive normally distributed all position manipulation on the given /// <paramref name="vector"/> and rounding the results to the next feasible value. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not /// as long as the vector to get manipulated.</exception> /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param> /// <param name="random">A random number generator.</param> /// <param name="vector">The integer vector to manipulate.</param> /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param> public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray strategyParameters) { if (strategyParameters == null || strategyParameters.Length == 0) { throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Vector containing the standard deviations is not defined.", "sigma"); } if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) { throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds"); } var N = new NormalDistributedRandom(random, 0.0, 1.0); if (strategyParameters != null) { for (int i = 0; i < vector.Length; i++) { int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1; if (bounds.Columns > 2) { step = bounds[i % bounds.Rows, 2]; } int value = (vector[i] + (int)Math.Round((N.NextDouble() * strategyParameters[i % strategyParameters.Length])) - min) / step; max = FloorFeasible(min, max, step, max - 1); vector[i] = RoundFeasible(min, max, step, value); } } }
protected override List <List <double> > GenerateValues() { List <List <double> > data = new List <List <double> >(); for (int i = 0; i < AllowedInputVariables.Count(); i++) { data.Add(ValueGenerator.GenerateUniformDistributedValues(10000, 0, 1).ToList()); } double x1, x2, x3, x4, x5; double f; List <double> results = new List <double>(); for (int i = 0; i < data[0].Count; i++) { x1 = data[0][i]; x2 = data[1][i]; x3 = data[2][i]; x4 = data[3][i]; x5 = data[4][i]; f = 10 * Math.Sin(Math.PI * x1 * x2) + 20 * Math.Pow(x3 - 0.5, 2) + 10 * x4 + 5 * x5; results.Add(f + NormalDistributedRandom.NextDouble(rand, 0, 1)); } data.Add(results); return(data); }
/// <summary> /// Generates a new random real vector normally distributed around the given mean with the given <paramref name="length"/> and in the interval [min,max). /// </summary> /// <exception cref="ArgumentException"> /// Thrown when <paramref name="random"/> is null.<br /> /// Thrown when <paramref name="mean"/> is null or of length 0.<br /> /// Thrown when <paramref name="sigma"/> is null or of length 0.<br /> /// </exception> /// <remarks> /// If no bounds are given the bounds will be set to (double.MinValue;double.MaxValue). /// /// If dimensions of the mean do not lie within the given bounds they're set to either to the min or max of the bounds depending on whether the given dimension /// for the mean is smaller or larger than the bounds. If min and max for a certain dimension are almost the same the resulting value will be set to min. /// /// However, please consider that such static bounds are not really meaningful to optimize. /// /// The sigma vector can contain 0 values in which case the dimension will be exactly the same as the given mean. /// </remarks> /// <param name="random">The random number generator.</param> /// <param name="means">The mean vector around which the resulting vector is sampled.</param> /// <param name="sigmas">The vector of standard deviations, must have at least one row.</param> /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> /// <param name="maximumTries">The maximum number of tries to sample a value inside the bounds for each dimension. If a valid value cannot be obtained, the mean will be used.</param> /// <returns>The newly created real vector.</returns> public static RealVector Apply(IntValue lengthValue, IRandom random, RealVector means, DoubleArray sigmas, DoubleMatrix bounds, int maximumTries = 1000) { if (lengthValue == null || lengthValue.Value == 0) { throw new ArgumentException("Length is not defined or zero"); } if (random == null) { throw new ArgumentNullException("Random is not defined", "random"); } if (means == null || means.Length == 0) { throw new ArgumentNullException("Mean is not defined", "mean"); } if (sigmas == null || sigmas.Length == 0) { throw new ArgumentNullException("Sigma is not defined.", "sigma"); } if (bounds == null || bounds.Rows == 0) { bounds = new DoubleMatrix(new[, ] { { double.MinValue, double.MaxValue } }); } var length = lengthValue.Value; var nd = new NormalDistributedRandom(random, 0, 1); var result = new RealVector(length); for (int i = 0; i < result.Length; i++) { var min = bounds[i % bounds.Rows, 0]; var max = bounds[i % bounds.Rows, 1]; var mean = means[i % means.Length]; var sigma = sigmas[i % sigmas.Length]; if (min.IsAlmost(max) || mean < min) { result[i] = min; } else if (mean > max) { result[i] = max; } else { int count = 0; bool inRange; do { result[i] = mean + sigma * nd.NextDouble(); inRange = result[i] >= min && result[i] < max; count++; } while (count < maximumTries && !inRange); if (count == maximumTries && !inRange) { result[i] = mean; } } } return(result); }
protected override List <List <double> > GenerateValues() { List <List <double> > data = new List <List <double> >(); for (int i = 0; i < AllowedInputVariables.Count(); i++) { data.Add(ValueGenerator.GenerateUniformDistributedValues(10000, 0, 1).ToList()); } double x1, x2, x3, x4, x5; double f; List <double> results = new List <double>(); for (int i = 0; i < data[0].Count; i++) { x1 = data[0][i]; x2 = data[1][i]; x3 = data[2][i]; x4 = data[3][i]; x5 = data[4][i]; f = 0.1 * Math.Exp(4 * x1) + 4 / (1 + Math.Exp(-20 * (x2 - 0.5))) + 3 * x3 + 2 * x4 + x5; results.Add(f + NormalDistributedRandom.NextDouble(rand, 0, 1)); } data.Add(results); return(data); }
/// <summary> /// <para>Sample a value from a gamma distribution.</para> /// <para>Implementation of "A Simple Method for Generating Gamma Variables" - Marsaglia & Tsang /// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, Pages 363–372.</para> /// </summary> /// <param name="uniformRandom">A uniformly-distributed random number generator.</param> /// <param name="shape">The shape (k, α) of the Gamma distribution. Range: α ≥ 0.</param> /// <param name="rate">The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0.</param> /// <returns>A sample from a Gamma distributed random variable.</returns> public static double NextDouble(IRandom uniformRandom, double shape, double rate) { if (double.IsPositiveInfinity(rate)) { return(shape); } var a = 1d; if (shape < 1) { a = Math.Pow(uniformRandom.NextDouble(), 1 / shape); shape += 1; } var d = shape - 1d / 3d; var c = 1 / Math.Sqrt(9 * d); for (;;) { double v, x; do { x = NormalDistributedRandom.NextDouble(uniformRandom, 0, 1); v = 1 + c * x; } while (v <= 0); v = v * v * v; x = x * x; // save a multiplication below var u = uniformRandom.NextDouble(); if (u < 1 - 0.0331 * x * x || Math.Log(u) < 0.5 * x + d * (1 - v + Math.Log(v))) { return(a * d * v / rate); } } }
public override void ShakeLocalParameters(IRandom random, double shakingFactor) { // mutate only one randomly selected weight var idx = random.Next(weights.Length); // 50% additive & 50% multiplicative if (random.NextDouble() < 0.5) { double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma); weights[idx] = weights[idx] + x * shakingFactor; } else { double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma); weights[idx] = weights[idx] * x; } if (random.NextDouble() < Symbol.VariableChangeProbability) { VariableName = Symbol.VariableNames.SampleRandom(random); if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) { // if the length of the weight array does not match => re-initialize weights weights = Symbol.GetVariableValues(variableName) .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)) .ToArray(); } } }
private List <List <double> > CreateVariables(List <List <double> > allowedInputs, int numVars, List <string[]> inputVarNames, List <string> description, List <double[]> relevances) { var newVariables = new List <List <double> >(); for (int c = 0; c < numVars; c++) { string[] selectedVarNames; double[] relevance; var x = GenerateRandomFunction(random, allowedInputs, out selectedVarNames, out relevance).ToArray(); // standardize x var sigma = x.StandardDeviation(); var mean = x.Average(); for (int i = 0; i < x.Length; i++) { x[i] = (x[i] - mean) / sigma; } var noisePrng = new NormalDistributedRandom(random, 0, Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); newVariables.Add(x.Select(t => t + noisePrng.NextDouble()).ToList()); Array.Sort(selectedVarNames, relevance); inputVarNames.Add(selectedVarNames); relevances.Add(relevance); var desc = string.Format("f({0})", string.Join(",", selectedVarNames)); // for the relevance information order variables by decreasing relevance var relevanceStr = string.Join(", ", selectedVarNames.Zip(relevance, Tuple.Create) .OrderByDescending(t => t.Item2) .Select(t => string.Format(CultureInfo.InvariantCulture, "{0}: {1:N3}", t.Item1, t.Item2))); description.Add(string.Format(" ~ N({0}, {1:N3}) [Relevances: {2}]", desc, noisePrng.Sigma, relevanceStr)); } return(newVariables); }
/// <summary> /// Mutates the endogenous strategy parameters. /// </summary> /// <param name="random">The random number generator to use.</param> /// <param name="vector">The strategy vector to manipulate.</param> /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param> /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param> /// <param name="bounds">The minimal and maximal value for each component, bounds are cycled if the length of bounds is smaller than the length of vector</param> public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) { NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0); double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble()); for (int i = 0; i < vector.Length; i++) { double change = vector[i] * generalMultiplier *Math.Exp(learningRate *N.NextDouble()); if (bounds != null) { double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1]; if (min == max) { vector[i] = min; } else { while (change < min || change > max) { change = vector[i] * generalMultiplier *Math.Exp(learningRate *N.NextDouble()); } vector[i] = change; } } } }
public void Mutate(NormalDistributedRandom gauss) { //sampling a random z from N(0,I) where I is the Identity matrix; lastZ = new RealVector(Mean.Length); var n = lastZ.Length; for (var i = 0; i < n; i++) { lastZ[i] = gauss.NextDouble(); } //Matrixmultiplication: lastStep = lowerCholesky * lastZ; lastStep = new RealVector(Mean.Length); for (var i = 0; i < n; i++) { double sum = 0; for (var j = 0; j <= i; j++) { sum += lowerCholesky[i, j] * lastZ[j]; } lastStep[i] = sum; } //add the step to x weighted by stepsize; for (var i = 0; i < Mean.Length; i++) { Mean[i] += sigma * lastStep[i]; } }
public override void ResetLocalParameters(IRandom random) { base.ResetLocalParameters(random); variableName = Symbol.VariableNames.SampleRandom(random); weights = Symbol.GetVariableValues(variableName) .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray(); }
public override void ResetLocalParameters(IRandom random) { base.ResetLocalParameters(random); weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma); #pragma warning disable 612, 618 variableName = Symbol.VariableNames.SelectRandom(random); #pragma warning restore 612, 618 }
/// <summary> /// Generates normally distributed values sampling from N(mu, sigma) /// </summary> /// <param name="seed">The seed for the random number generator</param> /// <param name="n">Number of values to generate.</param> /// <param name="mu">The mu parameter of the normal distribution</param> /// <param name="sigma">The sigma parameter of the normal distribution</param> /// <returns>An enumerable including n values ~ N(mu, sigma)</returns> public static IEnumerable <double> GenerateNormalDistributedValues(int seed, int n, double mu, double sigma) { var rand = new FastRandom(seed); for (int i = 0; i < n; i++) { yield return(NormalDistributedRandom.NextDouble(rand, mu, sigma)); } }
public override void ResetLocalParameters(IRandom random) { base.ResetLocalParameters(random); threshold = NormalDistributedRandom.NextDouble(random, Symbol.ThresholdInitializerMu, Symbol.ThresholdInitializerSigma); #pragma warning disable 612, 618 variableName = Symbol.VariableNames.SelectRandom(random); #pragma warning restore 612, 618 slope = NormalDistributedRandom.NextDouble(random, Symbol.SlopeInitializerMu, Symbol.SlopeInitializerSigma); }
private static IEnumerable <double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable <int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum.Median) { var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList(); double replacementValue; List <double> replacementValues; IRandom rand; switch (replacement) { case ReplacementMethodEnum.Median: replacementValue = rows.Select(r => originalValues[r]).Median(); replacementValues = Enumerable.Repeat(replacementValue, dataset.Rows).ToList(); break; case ReplacementMethodEnum.Average: replacementValue = rows.Select(r => originalValues[r]).Average(); replacementValues = Enumerable.Repeat(replacementValue, dataset.Rows).ToList(); break; case ReplacementMethodEnum.Shuffle: // new var has same empirical distribution but the relation to y is broken rand = new FastRandom(31415); // prepare a complete column for the dataset replacementValues = Enumerable.Repeat(double.NaN, dataset.Rows).ToList(); // shuffle only the selected rows var shuffledValues = rows.Select(r => originalValues[r]).Shuffle(rand).ToList(); int i = 0; // update column values foreach (var r in rows) { replacementValues[r] = shuffledValues[i++]; } break; case ReplacementMethodEnum.Noise: var avg = rows.Select(r => originalValues[r]).Average(); var stdDev = rows.Select(r => originalValues[r]).StandardDeviation(); rand = new FastRandom(31415); // prepare a complete column for the dataset replacementValues = Enumerable.Repeat(double.NaN, dataset.Rows).ToList(); // update column values foreach (var r in rows) { replacementValues[r] = NormalDistributedRandom.NextDouble(rand, avg, stdDev); } break; default: throw new ArgumentException(string.Format("ReplacementMethod {0} cannot be handled.", replacement)); } return(EvaluateModelWithReplacedVariable(model, variable, dataset, rows, replacementValues)); }
private static IList GetReplacementValuesForDouble(ModifiableDataset modifiableDataset, IEnumerable <int> rows, List <double> originalValues, ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle) { IRandom random = new FastRandom(31415); List <double> replacementValues; double replacementValue; switch (replacementMethod) { case ReplacementMethodEnum.Median: replacementValue = rows.Select(r => originalValues[r]).Median(); replacementValues = Enumerable.Repeat(replacementValue, modifiableDataset.Rows).ToList(); break; case ReplacementMethodEnum.Average: replacementValue = rows.Select(r => originalValues[r]).Average(); replacementValues = Enumerable.Repeat(replacementValue, modifiableDataset.Rows).ToList(); break; case ReplacementMethodEnum.Shuffle: // new var has same empirical distribution but the relation to y is broken // prepare a complete column for the dataset replacementValues = Enumerable.Repeat(double.NaN, modifiableDataset.Rows).ToList(); // shuffle only the selected rows var shuffledValues = rows.Select(r => originalValues[r]).Shuffle(random).ToList(); int i = 0; // update column values foreach (var r in rows) { replacementValues[r] = shuffledValues[i++]; } break; case ReplacementMethodEnum.Noise: var avg = rows.Select(r => originalValues[r]).Average(); var stdDev = rows.Select(r => originalValues[r]).StandardDeviation(); // prepare a complete column for the dataset replacementValues = Enumerable.Repeat(double.NaN, modifiableDataset.Rows).ToList(); // update column values foreach (var r in rows) { replacementValues[r] = NormalDistributedRandom.NextDouble(random, avg, stdDev); } break; default: throw new ArgumentException(string.Format("ReplacementMethod {0} cannot be handled.", replacementMethod)); } return(replacementValues); }
/// <summary> /// Performs an adaptive normally distributed all position manipulation on the given /// <paramref name="vector"/>. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not /// as long as the vector to get manipulated.</exception> /// <param name="sigma">The strategy vector determining the strength of the mutation.</param> /// <param name="random">A random number generator.</param> /// <param name="vector">The real vector to manipulate.</param> /// <returns>The manipulated real vector.</returns> public static void Apply(IRandom random, RealVector vector, RealVector sigma) { if (sigma == null || sigma.Length == 0) { throw new ArgumentException("ERROR: Vector containing the standard deviations is not defined.", "sigma"); } NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0); for (int i = 0; i < vector.Length; i++) { vector[i] = vector[i] + (N.NextDouble() * sigma[i % sigma.Length]); } }
public override void ShakeLocalParameters(IRandom random, double shakingFactor) { base.ShakeLocalParameters(random, shakingFactor); double x = NormalDistributedRandom.NextDouble(random, Symbol.ThresholdManipulatorMu, Symbol.ThresholdManipulatorSigma); threshold = threshold + x * shakingFactor; #pragma warning disable 612, 618 variableName = Symbol.VariableNames.SelectRandom(random); #pragma warning restore 612, 618 x = NormalDistributedRandom.NextDouble(random, Symbol.SlopeManipulatorMu, Symbol.SlopeManipulatorSigma); slope = slope + x * shakingFactor; }
/// <summary> /// Performs an adaptive normally distributed all position manipulation on the given /// <paramref name="vector"/>. /// </summary> /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not /// as long as the vector to get manipulated.</exception> /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param> /// <param name="random">A random number generator.</param> /// <param name="vector">The real vector to manipulate.</param> /// <returns>The manipulated real vector.</returns> public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) { NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0); if (strategyParameters != null) { for (int i = 0; i < vector.Length; i++) { vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]); } } else { // BackwardsCompatibility3.3 #region Backwards compatible region (remove with 3.4) // when upgrading to 3.4 remove the whole condition and just put the if-branch in place (you should then also throw an ArgumentException when strategyParameters is null or has length 0) for (int i = 0; i < vector.Length; i++) { vector[i] = vector[i] + N.NextDouble(); } #endregion } }
public override void ShakeLocalParameters(IRandom random, double shakingFactor) { base.ShakeLocalParameters(random, shakingFactor); // 50% additive & 50% multiplicative if (random.NextDouble() < 0.5) { double x = NormalDistributedRandom.NextDouble(random, Symbol.ManipulatorMu, Symbol.ManipulatorSigma); Value = Value + x * shakingFactor; } else { double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeManipulatorSigma); Value = Value * x; } }
public static AdditiveMove[] Apply(IRandom random, RealVector vector, double sigma, int sampleSize, DoubleMatrix bounds) { AdditiveMove[] moves = new AdditiveMove[sampleSize]; NormalDistributedRandom N = new NormalDistributedRandom(random, 0, sigma); for (int i = 0; i < sampleSize; i++) { int index = random.Next(vector.Length); double strength = 0, min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1]; do { strength = N.NextDouble(); } while (vector[index] + strength <min || vector[index] + strength> max); moves[i] = new AdditiveMove(index, strength); } return(moves); }
private IEnumerable <double> SampleGaussianProcess(IRandom random, List <double>[] xs) { int nl = xs.Length; int nRows = xs.First().Count; double[,] K = new double[nRows, nRows]; // sample length-scales var l = Enumerable.Range(0, nl) .Select(_ => random.NextDouble() * 2 + 0.5) .ToArray(); // calculate covariance matrix for (int r = 0; r < nRows; r++) { double[] xi = xs.Select(x => x[r]).ToArray(); for (int c = 0; c <= r; c++) { double[] xj = xs.Select(x => x[c]).ToArray(); double dSqr = xi.Zip(xj, (xik, xjk) => (xik - xjk)) .Select(dk => dk * dk) .Zip(l, (dk, lk) => dk / lk) .Sum(); K[r, c] = Math.Exp(-dSqr); } } // add a small diagonal matrix for numeric stability for (int i = 0; i < nRows; i++) { K[i, i] += 1.0E-7; } // decompose alglib.trfac.spdmatrixcholesky(ref K, nRows, false); // sample u iid ~ N(0, 1) var u = Enumerable.Range(0, nRows).Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray(); // calc y = Lu var y = new double[u.Length]; alglib.ablas.rmatrixmv(nRows, nRows, K, 0, 0, 0, u, 0, ref y, 0); return(y); }
protected override List <List <double> > GenerateValues() { List <List <double> > data = new List <List <double> >(); List <int> values = new List <int>() { -1, 1 }; var rand = new MersenneTwister((uint)Seed); data.Add(GenerateUniformIntegerDistribution(rand, values, TestPartitionEnd)); values.Add(0); for (int i = 0; i < AllowedInputVariables.Count() - 1; i++) { data.Add(GenerateUniformIntegerDistribution(rand, values, TestPartitionEnd)); } double x1, x2, x3, x4, x5, x6, x7; double f; List <double> results = new List <double>(); double sigma = Math.Sqrt(2); for (int i = 0; i < data[0].Count; i++) { x1 = data[0][i]; x2 = data[1][i]; x3 = data[2][i]; x4 = data[3][i]; x5 = data[4][i]; x6 = data[5][i]; x7 = data[6][i]; if (x1.Equals(1)) { f = 3 + 3 * x2 + 2 * x3 + x4; } else { f = -3 + 3 * x5 + 2 * x6 + x7; } results.Add(f + NormalDistributedRandom.NextDouble(rand, 0, sigma)); } data.Add(results); return(data); }
public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) { var gravity = new double[velocity.Length]; var direction = new RealVector(velocity.Length); var radius = 0.0; var nd = new NormalDistributedRandom(random, 0, 1); for (int i = 0; i < velocity.Length; i++) { var g_id = (personalBestAttraction * personalBest[i] + neighborBestAttraction * neighborBest[i] - position[i] * (neighborBestAttraction + personalBestAttraction)) / 3.0; // center of the hyper-sphere gravity[i] = g_id + position[i]; // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html direction[i] = nd.NextDouble(); radius += g_id * g_id; } // randomly choose a radius within the hyper-sphere radius = random.NextDouble() * Math.Sqrt(radius); // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius var unitscale = Math.Sqrt(direction.DotProduct(direction)); if (unitscale > 0) { for (var i = 0; i < velocity.Length; i++) { var sampledPos = gravity[i] + direction[i] * radius / unitscale; velocity[i] = velocity[i] * inertia + sampledPos - position[i]; } } var speed = Math.Sqrt(velocity.DotProduct(velocity)); if (speed > maxVelocity) { for (var i = 0; i < velocity.Length; i++) { velocity[i] *= maxVelocity / speed; } } }
public override void ShakeLocalParameters(IRandom random, double shakingFactor) { base.ShakeLocalParameters(random, shakingFactor); // 50% additive & 50% multiplicative if (random.NextDouble() < 0) { double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma); weight = weight + x * shakingFactor; } else { double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma); weight = weight * x; } #pragma warning disable 612, 618 variableName = Symbol.VariableNames.SelectRandom(random); #pragma warning restore 612, 618 }
private static IEnumerable <double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable <int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum.Median) { var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList(); double replacementValue; List <double> replacementValues; IRandom rand; switch (replacement) { case ReplacementMethodEnum.Median: replacementValue = rows.Select(r => originalValues[r]).Median(); replacementValues = Enumerable.Repeat(replacementValue, dataset.Rows).ToList(); break; case ReplacementMethodEnum.Average: replacementValue = rows.Select(r => originalValues[r]).Average(); replacementValues = Enumerable.Repeat(replacementValue, dataset.Rows).ToList(); break; case ReplacementMethodEnum.Shuffle: // new var has same empirical distribution but the relation to y is broken rand = new FastRandom(31415); replacementValues = rows.Select(r => originalValues[r]).Shuffle(rand).ToList(); break; case ReplacementMethodEnum.Noise: var avg = rows.Select(r => originalValues[r]).Average(); var stdDev = rows.Select(r => originalValues[r]).StandardDeviation(); rand = new FastRandom(31415); replacementValues = rows.Select(_ => NormalDistributedRandom.NextDouble(rand, avg, stdDev)).ToList(); break; default: throw new ArgumentException(string.Format("ReplacementMethod {0} cannot be handled.", replacement)); } dataset.ReplaceVariable(variable, replacementValues); //mkommend: ToList is used on purpose to avoid lazy evaluation that could result in wrong estimates due to variable replacements var estimates = model.GetEstimatedValues(dataset, rows).ToList(); dataset.ReplaceVariable(variable, originalValues); return(estimates); }
private IEnumerable <double> SampleLinearFunction(IRandom rand, List <double>[] xs, out double[] relevance) { int nl = xs.Length; int nRows = xs.First().Count; // sample standardized coefficients iid ~ N(0, 1) var c = Enumerable.Range(0, nRows).Select(_ => NormalDistributedRandom.NextDouble(rand, 0, 1)).ToArray(); // calculate scaled coefficients (variables with large variance should have smaller coefficients) var scaledC = Enumerable.Range(0, nl) .Select(i => c[i] / xs[i].StandardDeviationPop()) .ToArray(); var y = EvaluteLinearModel(xs, scaledC); relevance = CalculateRelevance(y, xs, scaledC); return(y); }
protected override List <List <double> > GenerateValues() { List <List <double> > data = new List <List <double> >(); var nrand = new NormalDistributedRandom(random, 0, 1); for (int c = 0; c < numberOfFeatures; c++) { var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList(); data.Add(datai); } var y = GenerateRandomFunction(random, data); var targetSigma = y.StandardDeviation(); var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); data.Add(y.Select(t => t + noisePrng.NextDouble()).ToList()); return(data); }
protected override List <List <double> > GenerateValues() { var rand = new MersenneTwister((uint)Seed); List <List <double> > data = new List <List <double> >(); var C_La = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 0.4, 0.8).ToList(); var a = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 5.0, 10.0).ToList(); var C_Ld_e = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 0.4, 0.8).ToList(); var d_e = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 5.0, 10.0).ToList(); var S_HT = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1.0, 1.5).ToList(); var S_ref = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 5.0, 7.0).ToList(); var C_L = new List <double>(); var C_L_noise = new List <double>(); data.Add(C_La); data.Add(a); data.Add(C_Ld_e); data.Add(d_e); data.Add(S_HT); data.Add(S_ref); data.Add(C_L); data.Add(C_L_noise); double a0 = -2.0; for (int i = 0; i < C_La.Count; i++) { double C_Li = C_La[i] * (a[i] - a0) + C_Ld_e[i] * d_e[i] * S_HT[i] / S_ref[i]; C_L.Add(C_Li); } var sigma_noise = 0.05 * C_L.StandardDeviationPop(); C_L_noise.AddRange(C_L.Select(md => md + NormalDistributedRandom.NextDouble(rand, 0, sigma_noise))); return(data); }
protected override List <List <double> > GenerateValues() { List <List <double> > data = new List <List <double> >(); for (int i = 0; i < AllowedInputVariables.Count(); i++) { data.Add(Enumerable.Range(0, TestPartitionEnd) .Select(_ => xRandom.NextDouble()) .ToList()); } var random = new MersenneTwister(); var selectedFeatures = Enumerable.Range(0, AllowedInputVariables.Count()) .Where(_ => random.NextDouble() < selectionProbability) .ToArray(); w = selectedFeatures.Select(_ => weightRandom.NextDouble()).ToArray(); var target = new List <double>(); for (int i = 0; i < data[0].Count; i++) { var s = selectedFeatures .Select(index => data[index][i]) .ToArray(); target.Add(ScalarProd(s, w)); } var targetSigma = target.StandardDeviation(); var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); data.Add(target.Select(t => t + noisePrng.NextDouble()).ToList()); // set property listing the selected features as string[] this.selectedFeatures = selectedFeatures.Select(i => AllowedInputVariables[i]).ToArray(); optimalRSquared = 1 - noiseRatio; return(data); }