@Phil: Awesome post. I totally forgot about the test cases in the src providing a good look at how to implement things. Thanks for reminding me where I need to look and providing some insightful instruction.
@Joe, Ted: Thanks for pointing me to some other good platforms. I'm looking into R and SciPy/NumPy. Best regards, Michael -----Original Message----- From: Phil Steitz [mailto:[email protected]] Sent: Wednesday, December 08, 2010 11:35 PM To: Commons Users List Subject: Re: [MATH] Looking for example code for the math.random and math.optimization libraries On Wed, Dec 8, 2010 at 12:57 PM, Rothenberg, Michael < [email protected]> wrote: > Thanks for the quick response, Joe. I am trying to pull random data based > on a distribution to start with. > > I have two variables with historical data and a correlation between them: > power and gas prices. I am now looking to simulate some relationships. 1st > step is to pull random power and gas pairs for each time period based on a > distribution (historical mean/stddev) and correlation. Lognormal is > preferred, but I can make normal into log normal if needed. > > I was browsing through math.random and came across the > CorrelatedRandomVectorGenerator class. Sounds like that is right up my > alley in terms of fitting my needs... but I have no idea how to > implement/use it. > > You are right that you can use the class above. Here is a code sample from the setUp() method of CorrelatedRandomVectorGeneratorTest showing you how to create a CorrelatedRandomVectorGenerator: RandomGenerator rg = new JDKRandomGenerator(); // Could use any RandomGenerator here rg.setSeed(17399225432l); // Fixed seed means same results every time GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg); generator = new CorrelatedRandomVectorGenerator(mean, covariance, 1.0e-12 * covariance.getNorm(), rawGenerator); The mean argument is a doulbe[] array holding the means of the random vector components. In your case, this will have length 2. The covariance argument is a RealMatrix, which in your case needs to be 2 x 2. The main diagonal elements should be the variances of the vector components and the off-diagonal elements should be the covariance. For example, if the means are 1 and 2 respectively, the desired standard deviations are 3 and 4, respectively, then you can use double[] mean = {1, 2}; double[][] cov = {{9, c}, {c, 16}}; RealMatrix covariance = MatrixUtils.createRealMatrix(cov); where c is the desired covariance. If you are starting with a desired correlation, you need to translate this to a covariance by multiplying it by the product of the standard deviations. For example, if you want to generate data that will give Pearson's R of 0.5, you would use c = 3 * 4 * .5 = 6. Phil > Thanks in advance for your help! > > Best regards, > > Michael > > w: 561.304.5921 > m: 772.263.8343 > > > -----Original Message----- > From: Haswell, Joe [mailto:[email protected]] > Sent: Wednesday, December 08, 2010 12:41 PM > To: Commons Users List > Subject: RE: [MATH] Looking for example code for the math.random and > math.optimization libraries > > Do you have any specific questions? Hard to point you in the right > direction or provide help if I don't know what you need. > > Joe H. | HP Software. > > -----Original Message----- > From: Rothenberg, Michael [mailto:[email protected]] > Sent: Wednesday, December 08, 2010 10:38 AM > To: [email protected] > Subject: [MATH] Looking for example code for the math.random and > math.optimization libraries > > Hi all, > > I have been reading the JavaDoc and everything else I can find on the > Commons.Math.Random and .Optimization libraries, but have not figured out > how to use them. Does anyone have any code examples/web sites/forums/etc.. > that I can use? > > Best regards, > > Michael > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
