Add your own alternative version
Stats
258.8K views 13.1K downloads 227 bookmarked
Posted
8 Aug 2006

Comments and Discussions



Actually, the workaround I gave above is not robust in all circumstances. For uniform distributions the following code will work, irrespective of the original and new values of Alpha and Beta.
private static void AlphaAndBeta(DiscreteUniformDistribution distribution, int alpha, int beta)
{
distribution.Alpha = Math.Min(alpha, distribution.Alpha);
distribution.Beta = Math.Max(beta, distribution.Beta);
distribution.Alpha = alpha;
distribution.Beta = beta;
Debug.Assert(distribution.Alpha == alpha);
Debug.Assert(distribution.Beta == beta);
}





I'm having no problems resetting a Standard Generator, but resetting a distribution doesn't seem to be working for me:
Troschuetz.Random.NormalDistribution n = new Troschuetz.Random.NormalDistribution ();
string s1="First Try of 5 Random Numbers: ";
for (int i=1; i<=5; i++)
{
s1+=n.NextDouble() + " ";
}
n.Reset();
string s2= "Second Try of 5 Random Numbers:";
for (int i=1; i<=5; i++)
{
s2+=n.NextDouble () + " ";
}
MessageBox.Show (s1 + "\r\n" + s2);
The above code ought to produce the exact same set of 5 numbers, and it doesn't. What am I doing wrong?
PS. Thank you very much for thisit looks well put together! Sorry if I'm missing something obvious...





Hi,
thanks for the input and finding this error. The problem is that I've once more forgotten to consider the random number generation algorithm of the normal distribution. It computes two random numbers at a time, buffers one and returns it when the NextDouble method is called for the next time. When I started developing this library I thought it will be sufficient for all distributions to reset the underlying random number generator, but obviously it is not since for example the NormalDistribution has to discard an already computed random number. I've corrected the whole mess by declaring the Distribution.Reset virtual and overriding it in derived classes when necessary. The new version of the library is uploaded.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





This is awesome, thank you a million!





Hi, my name is Todd and I am a working on a simulation model in VB.NET. I'm a novice programmer and trying to figure things out as I go, but need some help. I need to be able to choose a random number from a normal distribution with a mean and standard deviation (both need to be user defined). Would this library help me? Any suggestions would be extremely helpful. Thanks.
Take care
T





Hi Todd!
You can customize the normal distribution (and all other distributions too) with help of its (their) parameters exposed as properties. In case of the normal distribution the mean directly maps to the μ parameter and the standard deviation directly maps to the σ parameter. In general, the relation of the parameters to the statistics of a distribution is shown in the respective Wikipedia articles to which the article links.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Thanks a lot for the work. Cheers





A wonderful bit of work.
I wanted to use your tester program but it gives a zillion problems with respect to the ZedGraph stuff. For example it says that it can't tell if it's using a ZedGraph or a System.Windows Label. If I disambiguate those then I get a bunch of other serious errors.
As I'm sure it worked on your system, I suspect that the problem arises from the fact that I have a recent ZedGraph installation on my PC and for some reason it's clashing with yours. However, my version of the .dll is identical to yours.
Have you any idea of how I can get your Tester working?
Thanks
Steve





SteveAbbott wrote: As I'm sure it worked on your system
You're right Nevertheless I checked again and found something strange. Though I copied ZedGraph.dll version 5.0.1.41097 into the RandomTester project and referenced it, Visual Studio tells me in the properties that it references version 4.2.1.35091 at a path that no longer exists and copied it to the output directory (don't know from where). I've experimented a bit, but now everything is messed up and I'm no longer able to let the RandomTester project reference the Random project which is quite essential I will try to fix this mess on weekend and update the downloads if I succeed.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Thanks for trying! I still get confused about what magic DotNet is doing with control dlls. DotNet is often that sad mixture of Microsoft being so smart that it's dumb.
Good luck with your search for the magic fix. I look forward to experimenting with your excellent code.
Steve





Got some time tonight and think I fixed it. It seems it was some weird problem with the VS project. I copied all resources to a new project where I could reference my Random project again and also the ZedGraph.dll version I want. When referencing the version of ZedGraph which I thought using the whole time, I faced the same ambiguity problems as you and they seem to have changed some other things too. So obviously on your computer the VS project didn't show the weird behaviour as on mine. Cause I didn't want to rewrite the code for the newer ZedGraph version and the RandomTester runs well with the older one, I stick to it and updated the project so it hopefully uses this one correctly on every computer.
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





That's brilliant. It all works fine now, thanks for repackaging everything.
I wish I knew a foolproof way of moving a .Net project from one folder to another without all sorts of collateral damage!
Steve





Hi there, first of all *great* article. Very useful for me. I was wondering if in your work you have came accross an FInv funtion (return the quantile according to the F distribution  avaialble in Excel FInv).
Thanks a lot, Busi





Sorry, but that method never crossed my way.
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Thanks Stephan. If you will, drop me a line...
Thanks, Busi





I'll keep my eyes open
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Hello Stefan (again), Just asking if during your recent work you have come accross the naughty FInv function...
Thanks, Busi





Sorry to disappoint you but it still didn't cross my way.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Almost ten years ago I took a class in Simulations and Testing for my CE Masters. In that class I had to hand code a half dozen different random number generators in order to to use them simultaneously for different variables/inputs to guarantee they were related. Boy could I have used something like this back then ... though C# wasn't even on the radar then. Worth a 5 to me even though I don't have a current need to RNG functions.
One thing though, my text book back then had many examples of bad RNG functions and charts/graphs to show why. They were often time series plots in 3D that would show patterns, etc. If I recall correctly, often it wasn't the algortithm, but the parameters that could make or break an RNG. I no longer have the book, but I was wondering how hard it would be to add such tests to your test app so that you could experiement with the parameters to the different RNG functions &/or create more and be able to spot a bad one.





Scott S. wrote: how hard it would be to add such tests to your test app
It's hard to say without having/knowing the book you refer to and therefor not knowing how complex these test are.
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Hello
I am trying Random Tester and the DescreteUniformDistribution. And it gives me quite strange results. The histogram is almost OK: the numbers are uniformly distributed, except for 3 points in it. First is the number something less then 1/3 of the range, then the number less then 2/3 of the range and then the upper bound.
If I generate numbers between 0  100, than the histogram is OK except for number(s) a bit less than 30 and 60 and at the number 100.
If I generate numbers between 0  5000, than the histogram is OK except for number(s) about 1400 and 2900 and 5000. To see this anomaly, you have to increase number of samples (I used 10 000 000)
To see it cleary is goot to not use large range (say 0  1000), and use many samples (it's visible in 1 000 000 samples, but much better in 10 000 000 samples).
Can anybody explain this to me?
Thanks a lot, Stano
PS: Great work and thanks a lot for this library.





Hi,
the described behaviour results from the used computation of the displayed histogram. In case you generate discrete random numbers between 0 and 100 there are 101 discrete numbers that occur. At the same time the histogram uses by default only 100 steps (intervals), so it cannot differentiate all generated numbers. Therefor increase the number of histogram steps, so it equals at least the number of generated discrete numbers.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Wonderful article on Codeproject. Thanks heaps.
I was looking for a way to generate randomised stockmarket data for testing puposes. Random generation of close signal is relatively straight forward. The problem is generating open/high/low/close/volume data which is stochastic i.e. some pattern is present related to CLV (Close Location Volume), ATR(average true range) , OBV(On Balance Volume) etc.
At least with your set of random generators I can now simulate stochastics, by applying various distributions to OHLC prices and volumes.
I,m just beginning tonight with your classes and already I have created a better simulator using your classes than I had been able to create previously. Cheers Anton





Anton,
How are you accounting for the infamous 'fattails' of the stock market? And do you know of a way to take a distribution based upon historical data and test based on this distribution?
BW





Variation in the distribution used (or parameters or both) whilst travelling between price points gives adequate results.







General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

