Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Inject other Random.Generator (like mersenne twister) #628

Closed
nwiman opened this issue Jun 2, 2017 · 9 comments
Closed

Inject other Random.Generator (like mersenne twister) #628

nwiman opened this issue Jun 2, 2017 · 9 comments

Comments

@nwiman
Copy link

nwiman commented Jun 2, 2017

For now you can only control the seed of the Accord.Math.Random.Generator, but i want to change the Generator to a MersenneTwister... is there a reason for having the Generator readonly, if you could inject a generator and know the limitations wouldn't this be a good thing?

@cesarsouza
Copy link
Member

Hi there,

Well, the reason for having it read-only is that changing this generator might affect the behavior of the entire framework, and not only the code that you need.

However, I agree that the framework shouldn't prevent much what users can do with it, so probably this could be changed.

May I ask which components/classes are you mostly interested in using with a different generator?

@nwiman
Copy link
Author

nwiman commented Jun 5, 2017

Yes, I understand that this is a central piece but I think the implementation is a bit problematic as it (as you say) prevent one from changing the random generator.

The class for now that I want to use a Mersenne twister generator in is the MultivariateNormalDistribution.
It would be nice to have a container that you could bootstrap with a generator or an override where you pass a System.Random, I haven't looked too much in the source code but you maybe have an idea of how to do this in a clean way...

Thanks for a great lib and a quick reply (as always :))

Cheers

@fsteveb
Copy link

fsteveb commented Jun 5, 2017 via email

@nwiman
Copy link
Author

nwiman commented Jun 5, 2017

Yes I see what you mean, but in my case I need to get hold of the actual reference to the Random as well.
As I need to continue to get my random numbers in sequence after my call to Generate my samples from the Accord.Net MultivariateNormalDistribution, if you see where I am getting at?

@cesarsouza
Copy link
Member

@fsteveb Thanks a lot for the comment and offer to help! The framework currently offers an IRandomNumberGenerator<T> interface that can be used in the implementation of random generators. Currently there are already some generators specified there, such as the Uniform Ziggurat but the list is not extensive right now. It would be nice to have new ones, if someone would be willing to contribute some new implementations :-)

However, I still think that replacing the central generator is not a very good idea as it can be quite hard to control it since it can be called from anywhere in and outside the framework, including multiple threads.

@nwiman I had the suspicion you would like to use it from inside the probability distributions :-) In this case, what maybe could easily be done without breaking too much code is to add a new property or virtual method to the base class of all distributions where a IRandomNumberGenerator<T> could be specified. Then the probability distributions could be changed to take the generator from this property/method instead of Accord.Math.Random.Generator.Random.

@nwiman
Copy link
Author

nwiman commented Jun 5, 2017

I like the Idea of the interface option, it seems simple for a starter... :)

@cesarsouza
Copy link
Member

cesarsouza commented Jun 7, 2017

Where exactly would you like to replace the random generation? Is it in the Generate function, when generating/sampling new observations from the distribution, or is it during learning/fitting the GMM model to the data?

Actually I've realized that adding this extra property would require every single .Clone() method of every distribution to be modified to cascade their internal generator to the cloned instances. This would also have some problems with distributions that are actually containers to other distributions such as Mixture or Independent. So I have therefore changed the interface for the .Generate() methods to accept a Random generator as the last parameter instead, as this was simpler to accomplish.

In order to use your own generator, please inherit from .NET's Random class and override its appropriate methods, then pass it at the last parameter of the .Generate() method to generate new samples with your own generator.

If you would also need this functionality in other methods than .Generate(), please let me know!

Regards,
Cesar

@nwiman
Copy link
Author

nwiman commented Jun 7, 2017

For now I'm only using the Generate function but I suspected that you would have to pass the "random" in the ctor or the generate function as it in the MultivariateNormal case uses a static Random function on the NormalDistribution.
The MersenneTwister is inherited from .NETs Random so this will hopefully work like a charm...

Thanks again for all support!

@cesarsouza
Copy link
Member

Added in release 3.6.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants