/// <summary> /// Reads from the provided file name all parameters and data for a /// heightmap. If the data for the heightmap does not exist, then /// no data is written to the provided texture. /// </summary> /// <param name='fileName'> /// The file name. This can be relative or fully-qualified. /// </param> /// <param name='no'> /// The <see cref="NormalOptions" /> that will store read-in parameters. /// </param> /// <param name='co'> /// The <see cref="CloudOptions" /> that will store read-in parameters for /// <see cref="CloudFractal" />. /// </param> /// <param name='wo'> /// The <see cref="WorleyOptions" /> that will store read-in parameters for /// <see cref="WorleyNoise" />. /// </param> /// <param name='tex'> /// The <code>Texture2D</code> containing the heightmap data. /// </param> public static void Read(string fileName, ref NormalOptions no, ref CloudOptions co, ref VoronoiOptions vo, ref Texture2D tex) { using(BinaryReader r = new BinaryReader(File.OpenRead(fileName))) { no.size = r.ReadInt32(); no.seed = r.ReadInt32(); no.cloudInf = r.ReadSingle(); no.voronoiInf = r.ReadSingle(); no.useThermalErosion = r.ReadBoolean(); no.useHydroErosion = r.ReadBoolean(); no.showSeams = r.ReadBoolean(); co.upperLeftStart = r.ReadSingle(); co.lowerLeftStart = r.ReadSingle(); co.lowerRightStart = r.ReadSingle(); co.upperRightStart = r.ReadSingle(); vo.metric = (DistanceFunctions.DistanceMetric)r.ReadInt32(); vo.combiner = (CombinerFunctions.CombineFunction)r.ReadInt32(); vo.numberOfFeaturePoints = r.ReadInt32(); vo.numberOfSubregions = r.ReadInt32(); vo.multiplier = r.ReadSingle(); tex.Resize(no.size, no.size); int bLeft = (int)(r.BaseStream.Length - r.BaseStream.Position); if(bLeft > 0) tex.LoadImage(r.ReadBytes(bLeft)); } }
public void FitTest5() { double[][] observations = { new double[] { 1, 2 }, new double[] { 1, 2 }, new double[] { 0, 1 }, new double[] { 5, 7 } }; double[] weights = { 1, 1, 0, 0 }; var target = new MultivariateNormalDistribution(2); bool thrown = false; try { target.Fit(observations, weights); } catch (NonPositiveDefiniteMatrixException) { thrown = true; } Assert.IsTrue(thrown); NormalOptions options = new NormalOptions() { Robust = true }; // No exception thrown target.Fit(observations, weights, options); checkDegenerate(target); }
public void FitTest2() { double[][] observations = { new double[] { 1, 2 }, new double[] { 1, 2 }, new double[] { 1, 2 }, new double[] { 1, 2 } }; var target = new MultivariateNormalDistribution(2); bool thrown = false; try { target.Fit(observations); } catch (NonPositiveDefiniteMatrixException) { thrown = true; } Assert.IsTrue(thrown); NormalOptions options = new NormalOptions() { Regularization = double.Epsilon }; // No exception thrown target.Fit(observations, options); }
public void FitTest3() { double[][] observations = { new double[] { 1, 2 }, new double[] { 2, 4 }, new double[] { 3, 6 }, new double[] { 4, 8 } }; var target = new MultivariateNormalDistribution(2); NormalOptions options = new NormalOptions() { Robust = true }; target.Fit(observations, options); double pdf = target.ProbabilityDensityFunction(4, 2); double cdf = target.DistributionFunction(4, 2); bool psd = target.Covariance.IsPositiveDefinite(); Assert.AreEqual(0.043239154739844896, pdf); Assert.AreEqual(0.12263905840338646, cdf); Assert.IsFalse(psd); }
/// <summary> /// Fits the underlying distribution to a given set of observations. /// </summary> /// /// <param name="observations">The array of observations to fit the model against. The array /// elements can be either of type double (for univariate data) or /// type double[] (for multivariate data).</param> /// <param name="weights">The weight vector containing the weight for each of the samples.</param> /// <param name="options">Optional arguments which may be used during fitting, such /// as regularization constants and additional parameters.</param> /// /// <remarks> /// Although both double[] and double[][] arrays are supported, /// providing a double[] for a multivariate distribution or a /// double[][] for a univariate distribution may have a negative /// impact in performance. /// </remarks> /// public override void Fit(double[] observations, double[] weights, IFittingOptions options) { if (immutable) { throw new InvalidOperationException(); } double mu, var; if (weights != null) { #if DEBUG for (int i = 0; i < weights.Length; i++) { if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) { throw new Exception("Invalid numbers in the weight vector."); } } #endif // Compute weighted mean mu = Statistics.Tools.WeightedMean(observations, weights); // Compute weighted variance var = Statistics.Tools.WeightedVariance(observations, weights, mu); } else { // Compute weighted mean mu = Statistics.Tools.Mean(observations); // Compute weighted variance var = Statistics.Tools.Variance(observations, mu); } if (options != null) { // Parse optional estimation options NormalOptions o = (NormalOptions)options; double regularization = o.Regularization; if (var == 0 || Double.IsNaN(var) || Double.IsInfinity(var)) { var = regularization; } } if (var <= 0) { throw new ArgumentException("Variance is zero. Try specifying " + "a regularization constant in the fitting options."); } initialize(mu, Math.Sqrt(var), var); }
public void GetFittingOptionsTest() { NormalOptions options = (NormalOptions)DistributionInfo.GetFittingOptions <NormalDistribution>(); Assert.AreEqual(0, options.Regularization); Assert.IsNull(options.Postprocessing); Assert.AreEqual(false, options.Diagonal); Assert.AreEqual(false, options.Robust); Assert.AreEqual(false, options.Shared); }
/// <summary> /// Fits the underlying distribution to a given set of observations. /// </summary> /// /// <param name="observations">The array of observations to fit the model against. The array /// elements can be either of type double (for univariate data) or /// type double[] (for multivariate data).</param> /// <param name="weights">The weight vector containing the weight for each of the samples.</param> /// <param name="options">Optional arguments which may be used during fitting, such /// as regularization constants and additional parameters.</param> /// /// <remarks> /// Although both double[] and double[][] arrays are supported, /// providing a double[] for a multivariate distribution or a /// double[][] for a univariate distribution may have a negative /// impact in performance. /// </remarks> /// public override void Fit(double[][] observations, double[] weights, IFittingOptions options) { double[] means; double[,] cov; if (weights != null) { // Compute weighted mean vector means = Statistics.Tools.Mean(observations, weights); // Compute weighted covariance matrix cov = Statistics.Tools.WeightedCovariance(observations, weights, means); } else { // Compute mean vector means = Statistics.Tools.Mean(observations); // Compute covariance matrix cov = Statistics.Tools.Covariance(observations, means); } CholeskyDecomposition chol = new CholeskyDecomposition(cov, false, true); if (options != null) { // Parse optional estimation options NormalOptions o = (NormalOptions)options; double regularization = o.Regularization; if (regularization > 0) { int dimension = observations[0].Length; while (!chol.PositiveDefinite) { for (int i = 0; i < dimension; i++) { cov[i, i] += regularization; } chol = new CholeskyDecomposition(cov, false, true); } } } if (!chol.PositiveDefinite) { throw new NonPositiveDefiniteMatrixException("Covariance matrix is not positive " + "definite. Try specifying a regularization constant in the fitting options."); } // Become the newly fitted distribution. initialize(means, cov, chol); }
/// <summary> /// Fits the underlying distribution to a given set of observations. /// </summary> /// <param name="observations">The array of observations to fit the model against. The array /// elements can be either of type double (for univariate data) or /// type double[] (for multivariate data).</param> /// <param name="weights">The weight vector containing the weight for each of the samples.</param> /// <param name="options">Optional arguments which may be used during fitting, such /// as regularization constants and additional parameters.</param> /// <remarks> /// Although both double[] and double[][] arrays are supported, /// providing a double[] for a multivariate distribution or a /// double[][] for a univariate distribution may have a negative /// impact in performance. /// </remarks> public override void Fit(double[][] observations, double[] weights, IFittingOptions options) { double[] means; double[,] cov; if (weights != null) { #if DEBUG for (int i = 0; i < weights.Length; i++) { if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) { throw new Exception("Invalid numbers in the weight vector."); } } #endif // Compute weighted mean vector means = GABIZ.Base.Statistics.Tools.Mean(observations, weights); // Compute weighted covariance matrix cov = GABIZ.Base.Statistics.Tools.WeightedCovariance(observations, weights, means); } else { // Compute mean vector means = GABIZ.Base.Statistics.Tools.Mean(observations); // Compute covariance matrix cov = GABIZ.Base.Statistics.Tools.Covariance(observations, means); } if (options != null) { // Parse optional estimation options NormalOptions o = (NormalOptions)options; double regularization = o.Regularization; while (!cov.IsPositiveDefinite()) { int dimension = observations[0].Length; for (int i = 0; i < dimension; i++) { cov[i, i] += regularization; } } } // Become the newly fitted distribution. initialize(means, cov); }
public void EstimateTest() { double[] observations = { 2, 2, 2, 2, 2 }; NormalOptions options = new NormalOptions() { Regularization = 0.1 }; LognormalDistribution actual = LognormalDistribution.Estimate(observations, options); Assert.AreEqual(System.Math.Log(2), actual.Location); Assert.AreEqual(System.Math.Sqrt(0.1), actual.Shape); }
/// <summary> /// Creates a Baum-Welch with default configurations for /// hidden Markov models with normal mixture densities. /// </summary> /// public static BaumWelchLearning <Mixture <NormalDistribution> > FromMixtureModel( HiddenMarkovModel <Mixture <NormalDistribution> > model, NormalOptions options) { MixtureOptions mixOptions = new MixtureOptions() { Iterations = 1, InnerOptions = options }; return(new BaumWelchLearning <Mixture <NormalDistribution> >(model) { FittingOptions = mixOptions }); }
public void FitTest() { IDistribution target = CreateMultivariateContinuousDistribution(); double[][] observations = { new double[] { 1, 1 }, new double[] { 1, 1 }, }; double[] weights = { 0.50, 0.50 }; bool thrown; IFittingOptions options = new NormalOptions() { Regularization = 0.1 }; thrown = false; try { target.Fit(observations, weights); } catch { thrown = true; } Assert.AreEqual(true, thrown); thrown = false; try { target.Fit(observations, weights, options); } catch { thrown = true; } Assert.AreEqual(false, thrown); }
public void FitTest1() { IDistribution target = CreateMultivariateContinuousDistribution(); // TODO: Initialize to an appropriate value Array observations = new double[][] { new double[] { 1, 1 }, new double[] { 1, 1 }, }; bool thrown; IFittingOptions options = new NormalOptions() { Regularization = 0.1 }; thrown = false; try { target.Fit(observations); } catch { thrown = true; } Assert.AreEqual(true, thrown); thrown = false; try { target.Fit(observations, options); } catch { thrown = true; } Assert.AreEqual(false, thrown); }
/// <summary> /// 载入设置 /// </summary> /// <param name="options">相关设置</param> public static void Load(NormalOptions options) { if (File.Exists(options.Conf) == false) { Log.Warn(string.Format("没有找到配置文件\"{0}\"!", options.Conf)); Environment.Exit(1); return; } string content; using (StreamReader reader = new StreamReader(options.Conf, Encoding.UTF8)) { content = reader.ReadToEnd(); } Log.Info("成功获取配置文件,正在加载……"); List <SingleConfiguration> configurations = JsonConvert.DeserializeObject <List <SingleConfiguration> >(content, new ConfigurationConverter()); Log.Info(string.Format("成功加载配置文件,共有{0}条配置。", configurations.Count)); if (configurations.Count == 0) { Environment.Exit(0); } WorkScheduler scheduler = new WorkScheduler(); foreach (var i in configurations) { scheduler.StartWork(i); Log.Info(string.Format("成功启动任务\"{0}\"。", i.name)); } Log.Info("成功启动所有备份任务。"); #if !DEBUG Thread.Sleep(Timeout.Infinite);//加载完成后主线程休眠。 #endif }
/// <summary> /// Fits the underlying distribution to a given set of observations. /// </summary> /// <param name="observations">The array of observations to fit the model against. The array /// elements can be either of type double (for univariate data) or /// type double[] (for multivariate data).</param> /// <param name="weights">The weight vector containing the weight for each of the samples.</param> /// <param name="options">Optional arguments which may be used during fitting, such /// as regularization constants and additional parameters.</param> /// <remarks> /// Although both double[] and double[][] arrays are supported, /// providing a double[] for a multivariate distribution or a /// double[][] for a univariate distribution may have a negative /// impact in performance. /// </remarks> public override void Fit(double[] observations, double[] weights, IFittingOptions options) { if (immutable) { throw new InvalidOperationException(); } #if DEBUG for (int i = 0; i < weights.Length; i++) { if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) { throw new Exception("Invalid numbers in the weight vector."); } } #endif // Compute weighted mean double m = GABIZ.Base.Statistics.Tools.WeightedMean(observations, weights); // Compute weighted variance double v = GABIZ.Base.Statistics.Tools.WeightedVariance(observations, weights, m); if (options != null) { // Parse optional estimation options NormalOptions o = (NormalOptions)options; double regularization = o.Regularization; if (v == 0 || Double.IsNaN(v) || Double.IsInfinity(v)) { v = regularization; } } initialize(m, v); }
public TerrainGenerator(NormalOptions normalOpt, CloudOptions cloudOpt, VoronoiOptions voronoiOpt) { Init(normalOpt, cloudOpt, voronoiOpt); }
/// <summary> /// Estimates a new Normal distribution from a given set of observations. /// </summary> /// public static LognormalDistribution Estimate(double[] observations, double[] weights, NormalOptions options = null) { LognormalDistribution n = new LognormalDistribution(); n.Fit(observations, weights, options); return(n); }
private void Init(NormalOptions normalOpt, CloudOptions cloudOpt, VoronoiOptions voronoiOpt) { _normalOpt = normalOpt; _cloudOpt = cloudOpt; _voronoiOpt = voronoiOpt; _cfGen = new CloudFractal(_normalOpt.size, _normalOpt.seed, _cloudOpt); _vnGen = new VoronoiDiagram(_normalOpt.size, _normalOpt.seed, _voronoiOpt); _heightMap = new float[_normalOpt.size * _normalOpt.size]; }
/// <summary> /// Overload for <see cref="Write" /> that does not have texture data. /// </summary> /// <param name='fileName'> /// The file name. This can be relative or fully-qualified. /// </param> /// <param name='no'> /// The <see cref="NormalOptions" /> to write. /// </param> /// <param name='co'> /// The <see cref="CloudOptions" /> to write. /// </param> /// <param name='wo'> /// The <see cref="WorleyOptions" /> to write. /// </param> /// <param name='tex'> /// The <code>Texture2D</code> containing the heightmap data. /// </param> public static void Write(string fileName, NormalOptions no, CloudOptions co, VoronoiOptions vo) { Write(fileName, no, co, vo, null); }
/// <summary> /// Write the specified the heightmap and all paramaters to the provided /// file name. /// </summary> /// <param name='fileName'> /// The file name. This can be relative or fully-qualified. /// </param> /// <param name='no'> /// The <see cref="NormalOptions" /> to write. /// </param> /// <param name='co'> /// The <see cref="CloudOptions" /> to write. /// </param> /// <param name='wo'> /// The <see cref="WorleyOptions" /> to write. /// </param> /// <param name='tex'> /// The <code>Texture2D</code> containing the heightmap data. /// </param> /// <description> /// This uses a <code>BinaryWriter</code> to convert all data into binary /// data for writing. /// </description> public static void Write(string fileName, NormalOptions no, CloudOptions co, VoronoiOptions vo, Texture2D tex) { //if file already exists, will overwrite without warning using(BinaryWriter w = new BinaryWriter(File.Open(fileName+".emb", FileMode.Create))) { w.Write(no.size); w.Write(no.seed); w.Write(no.cloudInf); w.Write(no.voronoiInf); w.Write(no.useThermalErosion); w.Write(no.useHydroErosion); w.Write(no.showSeams); w.Write(co.upperLeftStart); w.Write(co.lowerLeftStart); w.Write(co.lowerRightStart); w.Write(co.upperRightStart); w.Write((int)vo.metric); w.Write((int)vo.combiner); w.Write(vo.numberOfFeaturePoints); w.Write(vo.numberOfSubregions); w.Write(vo.multiplier); if(tex != null) w.Write(tex.EncodeToPNG()); } }
/// <summary> /// Estimates a new Normal distribution from a given set of observations. /// </summary> /// public static NormalDistribution Estimate(double[][] observations, NormalOptions options) { return(Estimate(observations, null, options)); }
/// <summary> /// Estimates a new Normal distribution from a given set of observations. /// </summary> /// public static NormalDistribution Estimate(double[][] observations, double[] weights, NormalOptions options) { NormalDistribution n = new NormalDistribution(observations[0].Length); n.Fit(observations, weights, options); return(n); }
/// <summary> /// Fits the underlying distribution to a given set of observations. /// </summary> /// /// <param name="observations">The array of observations to fit the model against. The array /// elements can be either of type double (for univariate data) or /// type double[] (for multivariate data).</param> /// <param name="weights">The weight vector containing the weight for each of the samples.</param> /// <param name="options">Optional arguments which may be used during fitting, such /// as regularization constants and additional parameters.</param> /// /// <remarks> /// Although both double[] and double[][] arrays are supported, /// providing a double[] for a multivariate distribution or a /// double[][] for a univariate distribution may have a negative /// impact in performance. /// </remarks> /// public override void Fit(double[][] observations, double[] weights, IFittingOptions options) { double[] means; double[,] cov; NormalOptions opt = options as NormalOptions; if (weights != null) { #if DEBUG double sum = 0; for (int i = 0; i < weights.Length; i++) { if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) { throw new Exception("Invalid numbers in the weight vector."); } sum += weights[i]; } if (Math.Abs(sum - 1.0) > 1e-10) { throw new Exception("Weights do not sum to one."); } #endif // Compute weighted mean vector means = Statistics.Tools.Mean(observations, weights); // Compute weighted covariance matrix if (opt != null && opt.Diagonal) { cov = Matrix.Diagonal(Statistics.Tools.WeightedVariance(observations, weights, means)); } else { cov = Statistics.Tools.WeightedCovariance(observations, weights, means); } } else { // Compute mean vector means = Statistics.Tools.Mean(observations); // Compute covariance matrix if (opt != null && opt.Diagonal) { cov = Matrix.Diagonal(Statistics.Tools.Variance(observations, means)); } cov = Statistics.Tools.Covariance(observations, means); } CholeskyDecomposition chol = new CholeskyDecomposition(cov, false, true); if (opt != null) { // Parse optional estimation options double regularization = opt.Regularization; if (regularization > 0) { int dimension = observations[0].Length; while (!chol.PositiveDefinite) { for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { if (Double.IsNaN(cov[i, j]) || Double.IsInfinity(cov[i, j])) { cov[i, j] = 0.0; } } cov[i, i] += regularization; } chol = new CholeskyDecomposition(cov, false, true); } } } if (!chol.PositiveDefinite) { throw new NonPositiveDefiniteMatrixException("Covariance matrix is not positive " + "definite. Try specifying a regularization constant in the fitting options."); } // Become the newly fitted distribution. initialize(means, cov, chol); }