From 286d03b8c4f5df52082a88f9edbc0779a9ee32ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Old=C5=99ich=20Ko=C5=BEelsk=C3=BD?= Date: Sat, 7 Sep 2019 19:50:28 +0200 Subject: [PATCH] New random distributions and small enhancements --- Demo/DemoConsoleApp/DemoSettings.xml | 240 +++++++-- Demo/DemoConsoleApp/Research.cs | 60 ++- Demo/DemoConsoleApp/TimeSeriesGenerator.cs | 2 +- RCNet/Docs/Imgs/Components.png | Bin 0 -> 27352 bytes ...yClassExtensions.cs => ArrayExtensions.cs} | 4 +- ...ClassExtensions.cs => DoubleExtensions.cs} | 4 +- .../RNGCryptoServiceProviderClassExtension.cs | 127 ----- RCNet/Extensions/RandomClassExtensions.cs | 260 ---------- RCNet/Extensions/RandomExtensions.cs | 467 ++++++++++++++++++ ...ClassExtensions.cs => StringExtensions.cs} | 17 +- RCNet/Neural/Activation/ActivationFactory.cs | 6 +- .../Neural/Data/Generators/PulseGenerator.cs | 59 ++- .../Data/Generators/PulseGeneratorSettings.cs | 75 ++- RCNet/Neural/Network/FF/FeedForwardNetwork.cs | 9 +- RCNet/Neural/Network/FF/QRDRegrTrainer.cs | 14 +- RCNet/Neural/Network/PP/ParallelPerceptron.cs | 7 +- .../SM/Neuron/HiddenNeuronPredictors.cs | 127 ++--- .../Network/SM/Preprocessing/Reservoir.cs | 6 +- RCNet/RCNet.csproj | 10 +- RCNet/RCNetTypes.xsd | 84 +++- RCNet/RandomValue/IDistrSettings.cs | 18 + RCNet/RandomValue/RandomValueSettings.cs | 348 ++++++++++++- Readme.md | 4 +- 23 files changed, 1348 insertions(+), 600 deletions(-) create mode 100644 RCNet/Docs/Imgs/Components.png rename RCNet/Extensions/{ArrayClassExtensions.cs => ArrayExtensions.cs} (98%) rename RCNet/Extensions/{DoubleClassExtensions.cs => DoubleExtensions.cs} (96%) delete mode 100644 RCNet/Extensions/RNGCryptoServiceProviderClassExtension.cs delete mode 100644 RCNet/Extensions/RandomClassExtensions.cs create mode 100644 RCNet/Extensions/RandomExtensions.cs rename RCNet/Extensions/{StringClassExtensions.cs => StringExtensions.cs} (85%) create mode 100644 RCNet/RandomValue/IDistrSettings.cs diff --git a/Demo/DemoConsoleApp/DemoSettings.xml b/Demo/DemoConsoleApp/DemoSettings.xml index 27ac388..d9e34d9 100644 --- a/Demo/DemoConsoleApp/DemoSettings.xml +++ b/Demo/DemoConsoleApp/DemoSettings.xml @@ -1,6 +1,186 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -116,6 +128,13 @@ + + + + + + + @@ -123,6 +142,11 @@ + + + + + @@ -148,6 +172,13 @@ + + + + + + + @@ -169,16 +200,34 @@ + + + + + + + + + + + + + + + + + + @@ -187,11 +236,6 @@ Default value is false - - - Default value is Uniform - - @@ -200,32 +244,21 @@ + + + + + + + + - - - Default value is Uniform - - - - - - - - - - - - - - Default value is Uniform - - @@ -240,7 +273,8 @@ - + + diff --git a/RCNet/RandomValue/IDistrSettings.cs b/RCNet/RandomValue/IDistrSettings.cs new file mode 100644 index 0000000..8ec863c --- /dev/null +++ b/RCNet/RandomValue/IDistrSettings.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCNet.RandomValue +{ + public interface IDistrSettings + { + /// + /// Creates deep clone + /// + /// + IDistrSettings DeepClone(); + + }//IDistrSettings +} diff --git a/RCNet/RandomValue/RandomValueSettings.cs b/RCNet/RandomValue/RandomValueSettings.cs index 5b238a7..5c98612 100644 --- a/RCNet/RandomValue/RandomValueSettings.cs +++ b/RCNet/RandomValue/RandomValueSettings.cs @@ -36,12 +36,12 @@ public class RandomValueSettings /// /// Specifies what distribution to use /// - public RandomClassExtensions.DistributionType DistrType { get; } + public RandomExtensions.DistributionType DistrType { get; } /// - /// Gaussian distribution parameters + /// Distribution parameters /// - public GaussianDistrSettings GaussianDistrCfg { get; set; } + public IDistrSettings DistrCfg { get; set; } //Constructors /// @@ -50,20 +50,41 @@ public class RandomValueSettings /// Min random value /// Max random value /// Specifies whether to randomize value sign - /// Specifies what distribution to use - /// Specifies gaussian distribution parameters + /// Specific parameters of the distribution public RandomValueSettings(double min = -1, double max = 1, bool randomSign = false, - RandomClassExtensions.DistributionType distrType = RandomClassExtensions.DistributionType.Uniform, - GaussianDistrSettings gaussianDistrCfg = null + IDistrSettings distrCfg = null ) { Min = min; Max = max; RandomSign = randomSign; - DistrType = distrType; - GaussianDistrCfg = gaussianDistrCfg; + DistrCfg = distrCfg; + if(DistrCfg == null) + { + DistrType = RandomExtensions.DistributionType.Uniform; + } + else + { + Type dcType = DistrCfg.GetType(); + if(dcType == typeof(GaussianDistrSettings)) + { + DistrType = RandomExtensions.DistributionType.Gaussian; + } + else if(dcType == typeof(ExponentialDistrSettings)) + { + DistrType = RandomExtensions.DistributionType.Exponential; + } + else if (dcType == typeof(GammaDistrSettings)) + { + DistrType = RandomExtensions.DistributionType.Gamma; + } + else + { + throw new Exception($"Unexpected distribution configuration"); + } + } Check(); return; } @@ -78,11 +99,7 @@ public RandomValueSettings(RandomValueSettings source) Max = source.Max; RandomSign = source.RandomSign; DistrType = source.DistrType; - GaussianDistrCfg = null; - if (source.GaussianDistrCfg != null) - { - GaussianDistrCfg = source.GaussianDistrCfg.DeepClone(); - } + DistrCfg = source.DistrCfg?.DeepClone(); return; } @@ -105,13 +122,35 @@ public RandomValueSettings(XElement elem) Min = double.Parse(randomValueSettingsElem.Attribute("min").Value, CultureInfo.InvariantCulture); Max = double.Parse(randomValueSettingsElem.Attribute("max").Value, CultureInfo.InvariantCulture); RandomSign = bool.Parse(randomValueSettingsElem.Attribute("randomSign").Value); - DistrType = RandomClassExtensions.ParseDistributionType(randomValueSettingsElem.Attribute("distribution").Value); - //Gaussian parameters - GaussianDistrCfg = null; - XElement gaussianParamsElem = randomValueSettingsElem.Descendants("gaussianDistr").FirstOrDefault(); - if(gaussianParamsElem != null) + XElement distrParamsElem = randomValueSettingsElem.Elements().FirstOrDefault(); + if (distrParamsElem == null) + { + DistrType = RandomExtensions.DistributionType.Uniform; + DistrCfg = new UniformDistrSettings(); + } + else { - GaussianDistrCfg = new GaussianDistrSettings(gaussianParamsElem); + switch (distrParamsElem.Name.LocalName) + { + case "uniformDistr": + DistrType = RandomExtensions.DistributionType.Uniform; + DistrCfg = new UniformDistrSettings(distrParamsElem); + break; + case "gaussianDistr": + DistrType = RandomExtensions.DistributionType.Gaussian; + DistrCfg = new GaussianDistrSettings(distrParamsElem); + break; + case "exponentialDistr": + DistrType = RandomExtensions.DistributionType.Exponential; + DistrCfg = new ExponentialDistrSettings(distrParamsElem); + break; + case "gammaDistr": + DistrType = RandomExtensions.DistributionType.Gamma; + DistrCfg = new GammaDistrSettings(distrParamsElem); + break; + default: + throw new Exception($"Unexpected element {distrParamsElem.Name.LocalName}"); + } } Check(); return; @@ -189,7 +228,7 @@ public override bool Equals(object obj) Max != cmpSettings.Max || RandomSign != cmpSettings.RandomSign || DistrType != cmpSettings.DistrType || - !Equals(GaussianDistrCfg, cmpSettings.GaussianDistrCfg) + !Equals(DistrCfg, cmpSettings.DistrCfg) ) { return false; @@ -214,11 +253,83 @@ public RandomValueSettings DeepClone() } //Inner classes + /// + /// Uniform distribution parameters + /// + [Serializable] + public class UniformDistrSettings : IDistrSettings + { + //Attributes + + //Constructors + /// + /// Creates an initialized instance + /// + public UniformDistrSettings() + { + Check(); + return; + } + + /// + /// Copy constructor + /// + /// Source instance + public UniformDistrSettings(UniformDistrSettings source) + { + return; + } + + /// + /// Creates an instance and initializes it from given xml element. + /// + /// Xml data containing settings. + public UniformDistrSettings(XElement elem) + { + //Parsing + //Nothing to do + Check(); + return; + } + + //Methods + private void Check() + { + return; + } + + /// + /// See the base. + /// + public override bool Equals(object obj) + { + if (obj == null) return false; + return true; + } + + /// + /// See the base. + /// + public override int GetHashCode() + { + return base.GetHashCode(); + } + + /// + /// Creates the deep copy instance of this instance + /// + public IDistrSettings DeepClone() + { + return new UniformDistrSettings(this); + } + + }//UniformDistrSettings + /// /// Gaussian distribution parameters /// [Serializable] - public class GaussianDistrSettings + public class GaussianDistrSettings : IDistrSettings { //Attributes /// @@ -304,12 +415,201 @@ public override int GetHashCode() /// /// Creates the deep copy instance of this instance /// - public GaussianDistrSettings DeepClone() + public IDistrSettings DeepClone() { return new GaussianDistrSettings(this); } - }//GaussianSettings + }//GaussianDistrSettings + + /// + /// Exponential distribution parameters + /// + [Serializable] + public class ExponentialDistrSettings : IDistrSettings + { + //Attributes + /// + /// Mean + /// + public double Mean { get; } + + //Constructors + /// + /// Creates an initialized instance + /// + /// Mean + public ExponentialDistrSettings(double mean) + { + Mean = mean; + Check(); + return; + } + + /// + /// Copy constructor + /// + /// Source instance + public ExponentialDistrSettings(ExponentialDistrSettings source) + { + Mean = source.Mean; + return; + } + + /// + /// Creates an instance and initializes it from given xml element. + /// + /// Xml data containing settings. + public ExponentialDistrSettings(XElement elem) + { + //Parsing + Mean = double.Parse(elem.Attribute("mean").Value, CultureInfo.InvariantCulture); + Check(); + return; + } + + //Methods + private void Check() + { + if (Mean == 0) + { + throw new Exception($"Incorrect Mean ({Mean.ToString(CultureInfo.InvariantCulture)}) value. Mean must not be EQ to 0."); + } + return; + } + + /// + /// See the base. + /// + public override bool Equals(object obj) + { + if (obj == null) return false; + ExponentialDistrSettings cmpSettings = obj as ExponentialDistrSettings; + if (Mean != cmpSettings.Mean) + { + return false; + } + return true; + } + + /// + /// See the base. + /// + public override int GetHashCode() + { + return base.GetHashCode(); + } + + /// + /// Creates the deep copy instance of this instance + /// + public IDistrSettings DeepClone() + { + return new ExponentialDistrSettings(this); + } + + }//ExponentialDistrSettings + + /// + /// Gamma distribution parameters + /// + [Serializable] + public class GammaDistrSettings : IDistrSettings + { + //Attributes + /// + /// Alpha, the shape parameter + /// + public double Alpha { get; } + + /// + /// Beta, the rate parameter + /// + public double Beta { get; } + + //Constructors + /// + /// Creates an initialized instance + /// + /// Shape parameter (alpha) + /// Rate parameter (beta) + public GammaDistrSettings(double alpha, double beta) + { + Alpha = alpha; + Beta = beta; + Check(); + return; + } + + /// + /// Copy constructor + /// + /// Source instance + public GammaDistrSettings(GammaDistrSettings source) + { + Alpha = source.Alpha; + Beta = source.Beta; + return; + } + + /// + /// Creates an instance and initializes it from given xml element. + /// + /// Xml data containing settings. + public GammaDistrSettings(XElement elem) + { + //Parsing + Alpha = double.Parse(elem.Attribute("alpha").Value, CultureInfo.InvariantCulture); + Beta = double.Parse(elem.Attribute("beta").Value, CultureInfo.InvariantCulture); + Check(); + return; + } + + //Methods + private void Check() + { + if (Alpha <= 0) + { + throw new Exception($"Incorrect Alpha ({Alpha.ToString(CultureInfo.InvariantCulture)}) value. Alpha must be GT 0."); + } + if (Beta <= 0) + { + throw new Exception($"Incorrect Beta ({Beta.ToString(CultureInfo.InvariantCulture)}) value. Beta must be GT 0."); + } + return; + } + + /// + /// See the base. + /// + public override bool Equals(object obj) + { + if (obj == null) return false; + GammaDistrSettings cmpSettings = obj as GammaDistrSettings; + if (Alpha != cmpSettings.Alpha || Beta != cmpSettings.Beta) + { + return false; + } + return true; + } + + /// + /// See the base. + /// + public override int GetHashCode() + { + return base.GetHashCode(); + } + + /// + /// Creates the deep copy instance of this instance + /// + public IDistrSettings DeepClone() + { + return new GammaDistrSettings(this); + } + + }//GammaDistrSettings }//RandomValueSettings diff --git a/Readme.md b/Readme.md index 305ee58..c2d2fd8 100644 --- a/Readme.md +++ b/Readme.md @@ -36,13 +36,13 @@ Main functionality and possibilities are demonstrated in a simple demo applicati |LUD|LU decomposition of a squared matrix| |ParamSeeker|Implements an error driven iterative search for the best value of a given parameter| |HurstExpEstim|Implements Hurst exponent estimator. It can be used to evaluate level of data randomness| -|"RandomValue"|Supports Uniform and Gaussian distributions| +|"RandomValue"|Supports Uniform, Gaussian, Exponential and Gamma distributions| |Others|Set of small additional helper components like PhysUnit, Interval, Bitwise, Combinatorics, Factorial, WeightedAvg, ...| ### Signal generators |Component|Description| |--|--| -|PulseGenerator|Generates constant pulses at a specified frequency| +|PulseGenerator|Generates constant pulses having specified average period. Pulse leaks follow specified random distribution or can be constant| |MackeyGlassGenerator|Generates Mackey-Glass chaotic signal| |RandomGenerator|Generates random signal| |SinusoidalGenerator|Generates sinusoidal signal|