Sunday, December 27, 2009

How to use OpenForecast to a add forecasting features to you web applications...

Forecasting is one of a nice feature you can add to your data related web application.Assume we have to predict a measurement of a temperature for a given place and date. For that we required a data mining tool. So we achieved this task by using an open source forecasting library called OpenForecast. The advantage of the library is it provides lot of scalability our application. So we can add any data field for our prediction. Below I mentioned the approach we used to achieve the forecasting using OpenForecast.
• First you have to define the test data set for the forecast. For that library provides two class called DataSet and Observations.
• Using Observations class we can define the past observations we made. Then we have to assign the each Observation objects to the Data set. A sample code is shown below

// Create Observation for temperature measured on 2009/12/20
Observation observation1 = new Observation(32.0);
observation1.setIndependentValue("year",2009);
observation1.setIndependentValue("month", 12);
observation1.setIndependentValue("date", 20);

// Create Observation for temperature measured on 2009/12/21
Observation observation2 = new Observation(30.0);
observation2.setIndependentValue("year", 2009);
observation2.setIndependentValue("month", 12);
observation2.setIndependentValue("date", 21);

// Create Observation for temperature measured on 2009/12/22
Observation observation3 = new Observation(27.0);
observation3.setIndependentValue("year", 2009);
observation3.setIndependentValue("month", 12);
observation3.setIndependentValue("date", 22);

DataSet dataSet = new DataSet();

// Add Observations to the DataSet
dataSet.add(observation1);
dataSet.add(observation2);
dataSet.add(observation3);


• After we define the test data set as above we have to use a forecast model to test the data set. For that the library provide a functionality called getBestForecastModel(DataSet dataSet). So we pass our data set and get the best forecast model that can use to train our data set. Library has set of forecasting models such as Double Exponential Smoothing Model, Multiple Linear Regression Model, Naïve Forecasting Model, Polynominal Regression Model, Simple Exponential Smoothing Model, Triple Exponential Smoothing Model, Weighted Moving Average Model. To train the dataset we call the model.init() method. Following code shows this mechanism more clearly.


ForecastingModel model = Forecaster.getBestForecast(dataSet);
model.init(dataSet);


• Then we have to create another Observation/DataPoint for our prediction. In there we set the value to zero and add other independent variables. After that we have to create a new data set and we add above Observation/DataPoint object to our new data set. Sample code shown below.

DataPoint fcDataPoint4 = new Observation(0.0);
fcDataPoint4.setIndependentValue("year", 2009);
fcDataPoint4.setIndependentValue("month", 12);
fcDataPoint4.setIndependentValue("date", 23);

// Create forecast data set and add these DataPoints
DataSet fcDataSet = new DataSet();
fcDataSet.add(fcDataPoint4);


• Now we have created the data set which we want to forecast. Next we forecast the data set using our model and we get the dependent value from above fcDataSet like below. Our forecast result is equal to the value return by the getDependent() method.

Iterator itt = fcDataSet.iterator();
Double value=0.0;
while (itt.hasNext()) {
DataPoint dp = (DataPoint) itt.next();
double forecastValue = dp.getDependentValue();
value = forecastValue;
}

OpenForecast Site:http://openforecast.sourceforge.net/
Download OpenForecast:http://sourceforge.net/projects/openforecast/files/OpenForecast/OpenForecast%200.4.0/OpenForecast-0.4.0.jar/download
Custom Search

My Video Bar

Loading...