@@ -90,6 +90,12 @@ public static PlotModel DisconnectedBins()
9090 return CreateDisconnectedBins ( ) ;
9191 }
9292
93+ [ Example ( "Normal Distribution Three Colors" ) ]
94+ public static PlotModel NormalDistribution ( )
95+ {
96+ return CreateNormalDistribution ( ) ;
97+ }
98+
9399 public static PlotModel CreateExponentialDistribution ( double mean = 1 , int n = 10000 )
94100 {
95101 var model = new PlotModel { Title = "Exponential Distribution" , Subtitle = "Uniformly distributed bins (" + n + " samples)" } ;
@@ -128,6 +134,43 @@ public static PlotModel CreateExponentialDistributionCustomBins(double mean = 1,
128134 return model ;
129135 }
130136
137+ public static PlotModel CreateNormalDistribution ( double mean = 0 , double std = 1 , int n = 1000000 )
138+ {
139+ var model = new PlotModel { Title = $ "Normal Distribution (μ={ mean } , σ={ std } )", Subtitle = "95% of the distribution (" + n + " samples)" } ;
140+ model . Axes . Add ( new LinearAxis { Position = AxisPosition . Left , Title = "Frequency" } ) ;
141+ model . Axes . Add ( new LinearAxis { Position = AxisPosition . Bottom , Title = "x" } ) ;
142+
143+ Random rnd = new Random ( ) ;
144+
145+ HistogramSeries chs = new HistogramSeries ( ) ;
146+ var binningOptions = new BinningOptions ( BinningOutlierMode . CountOutliers , BinningIntervalType . InclusiveLowerBound , BinningExtremeValueMode . ExcludeExtremeValues ) ;
147+ var binBreaks = HistogramHelpers . CreateUniformBins ( - std * 4 , std * 4 , 100 ) ;
148+ chs . Items . AddRange ( HistogramHelpers . Collect ( SampleNormal ( rnd , mean , std , n ) , binBreaks , binningOptions ) ) ;
149+ chs . StrokeThickness = 1 ;
150+
151+ double LimitHi = mean + 1.96 * std ;
152+ double LimitLo = mean - 1.96 * std ;
153+ OxyColor ColorHi = OxyColors . DarkRed ;
154+ OxyColor ColorLo = OxyColors . DarkRed ;
155+
156+ chs . ColorMapping = ( item ) =>
157+ {
158+ if ( item . RangeCenter > LimitHi )
159+ {
160+ return ColorHi ;
161+ }
162+ else if ( item . RangeCenter < LimitLo )
163+ {
164+ return ColorLo ;
165+ }
166+ return chs . ActualFillColor ;
167+ } ;
168+
169+ model . Series . Add ( chs ) ;
170+
171+ return model ;
172+ }
173+
131174 public static PlotModel CreateDisconnectedBins ( )
132175 {
133176 var model = new PlotModel { Title = "Disconnected Bins" } ;
@@ -155,5 +198,21 @@ private static double SampleExp(Random rnd, double mean)
155198 {
156199 return Math . Log ( 1.0 - rnd . NextDouble ( ) ) / - mean ;
157200 }
201+
202+ private static IEnumerable < double > SampleNormal ( Random rnd , double mean , double std , int count )
203+ {
204+ for ( int i = 0 ; i < count ; i ++ )
205+ {
206+ yield return SampleNormal ( rnd , mean , std ) ;
207+ }
208+ }
209+
210+ private static double SampleNormal ( Random rnd , double mean , double std )
211+ {
212+ // http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
213+ var u1 = rnd . NextDouble ( ) ;
214+ var u2 = rnd . NextDouble ( ) ;
215+ return Math . Sqrt ( - 2 * Math . Log ( u1 ) ) * Math . Cos ( 2 * Math . PI * u2 ) ;
216+ }
158217 }
159218}
0 commit comments