Java Code Examples for net.sourceforge.openforecast.DataSet#getIndependentVariables()

The following examples show how to use net.sourceforge.openforecast.DataSet#getIndependentVariables() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTimeBasedModel.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Initializes the time variable from the given data set. If the data set
 * does not have a time variable explicitly defined, then provided there
 * is only one independent variable defined for the data set that is used
 * as the time variable. If more than one independent variable is defined
 * for the data set, then it is not possible to take an educated guess at
 * which one is the time variable. In this case, an
 * IllegalArgumentException will be thrown.
 * @param dataSet the data set to use to initialize the time variable.
 * @throws IllegalArgumentException If more than one independent variable
 * is defined for the data set and no time variable has been specified. To
 * correct this, be sure to explicitly specify the time variable in the
 * data set passed to {@link #init}.
 */
protected void initTimeVariable( DataSet dataSet )
    throws IllegalArgumentException
{
    if ( timeVariable == null )
        {
            // Time variable not set, so look at independent variables
            timeVariable = dataSet.getTimeVariable();
            if ( timeVariable == null )
                {
                    String[] independentVars
                        = dataSet.getIndependentVariables();
                    
                    if ( independentVars.length != 1 )
                        throw new IllegalArgumentException("Unable to determine the independent time variable for the data set passed to init for "+toString()+". Please use DataSet.setTimeVariable before invoking model.init.");
                    
                    timeVariable = independentVars[0];
                }
        }
}
 
Example 2
Source File: DataSetTest.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Tests the correct initialization of a DataSet.
 */
public void testDataSet()
{
    DataSet data = new DataSet( dataSet1 );
    
    // Verify data set contains the correct number of entries
    assertTrue( data.size() == dataSet1.size() );
    
    // Vefify that only one independent variable name is reported
    String[] independentVariables = data.getIndependentVariables();
    assertTrue( independentVariables.length == 1 );
    assertTrue( independentVariables[0].equals("x") );
    
    // Verify the dependent values stored
    Iterator<DataPoint> it = data.iterator();
    while ( it.hasNext() )
        {
            DataPoint dp = it.next();
            double value = dp.getDependentValue();
            double TOLERANCE = 0.001;
            assertTrue( value>-TOLERANCE && value<SIZE+TOLERANCE );
        }
}
 
Example 3
Source File: MultipleLinearRegressionModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Initializes the coefficients to use for this regression model. The
 * coefficients are derived so as to give the best fit hyperplane for the
 * given data set.
 *
 * <p>Additionally, the accuracy indicators are calculated based on this
 * data set.
 * @param dataSet the set of observations to use to derive the regression
 *        coefficients for this model.
 */
public void init( DataSet dataSet )
{
    String varNames[] = dataSet.getIndependentVariables();
    
    // If no coefficients have been defined for this model,
    //  use all that exist in this data set
    if ( coefficient == null )
        setIndependentVariables( varNames );
    
    int n = varNames.length;
    double a[][] = new double[n+1][n+2];
    
    // Iterate through dataSet
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            // Get next data point
            DataPoint dp = it.next();
            
            // For each row in the matrix, a
            for ( int row=0; row<n+1; row++ )
                {
                    double rowMult = 1.0;
                    if ( row != 0 )
                        {
                            // Get value of independent variable, row
                            String rowVarName = varNames[row-1];
                            rowMult = dp.getIndependentValue(rowVarName);
                        }
                    
                    // For each column in the matrix, a
                    for ( int col=0; col<n+2; col++ )
                        {
                            double colMult = 1.0;
                            if ( col == n+1 )
                                {
                                    // Special case, for last column
                                    //  use value of dependent variable
                                    colMult = dp.getDependentValue();
                                }
                            else if ( col > 0 )
                                {
                                    // Get value of independent variable, col
                                    String colVarName = varNames[col-1];
                                    colMult = dp.getIndependentValue(colVarName);
                                }
                            
                            a[row][col] += rowMult * colMult;
                        }
                }
        }
    
    // Solve equations to derive coefficients
    double coeff[] = Utils.GaussElimination( a );
    
    // Assign coefficients to independent variables
    intercept = coeff[0];
    for ( int i=1; i<n+1; i++ )
        coefficient.put( varNames[i-1], new Double(coeff[i]) );
    
    // Calculate the accuracy indicators
    calculateAccuracyIndicators( dataSet );
}
 
Example 4
Source File: TimeSeriesBuilderTest.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the correct input of a DataSet from a TimeSeries by creating a
 * simple TimeSeries object then inputting it using a TimeSeriesBuilder
 * instance.
 */
public void testBuilder()
{
    // Constants used to determine size of test
    int NUMBER_OF_TIME_PERIODS = 100;
    
    // Set up array for expected results
    double expectedValue[] = new double[ NUMBER_OF_TIME_PERIODS ];
    
    // Create test TimeSeries
    TimeSeries timeSeries = new TimeSeries("Simple time series");
    RegularTimePeriod period = new Day();
    
    for ( int d=0; d<NUMBER_OF_TIME_PERIODS; d++ )
        {
            expectedValue[d] = d;
            timeSeries.add(period,d);
            period = period.next();
        }
    
    // Create TimeSeriesBuilder and use it to create the DataSet
    String TIME_VARIABLE = "t";
    TimeSeriesBuilder builder
        = new TimeSeriesBuilder( timeSeries, TIME_VARIABLE );
    DataSet dataSet = builder.build();
    
    // Verify data set contains the correct number of entries
    assertEquals( "DataSet created is of the wrong size",
                  NUMBER_OF_TIME_PERIODS, dataSet.size() );
    
    // Vefify that only two independent variable names are reported
    String[] independentVariables = dataSet.getIndependentVariables();
    assertEquals( "Checking the correct number of independent variables",
                  1, independentVariables.length );
    assertEquals( "Independent variable not set as expected",
                  TIME_VARIABLE, independentVariables[0] );
    
    // Check the data points in the data set. This may not be a good
    //  test since it is dependent on the order of the data points in
    //  the 2-d array
    checkResults( dataSet, expectedValue );
}
 
Example 5
Source File: CSVBuilderTest.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the correct initialization of a DataSet from a CSV file where
 * the input is valid, yet poorly and irregularly formatted. For example,
 * the CSVBuilder is supposed to treat as a zero field two commas following
 * each other. This test will also test naming the columns and the use of
 * blank lines and comments in the input.
 */
public void testExtremeCSVBuilder()
    throws FileNotFoundException, IOException
{
    // Constants used to determine size of test
    double expectedValue[] = { 4,5,6,7,8 };
    int numberOfDataPoints = expectedValue.length;
    
    // Create test CSV file
    File testFile = File.createTempFile( "test", ".csv" );
    PrintStream out = new PrintStream( new FileOutputStream(testFile) );
    out.println("# This is a test CSV file with various 'peculiarities'");
    out.println(" # thrown in to try and trip it up");
    out.println("Field1, Field2, \"Field, 3\", Observation");
    out.println("-1, -2 ,-3,4");
    out.println(",,,5");
    out.println(" 1 , 2 , 3 , 6 ");
    out.println(" 2, 4, 6, 7");
    out.println("3 ,6 ,9 ,8");
    out.close();
    
    // Create CSV builder and use it to create the DataSet
    CSVBuilder builder = new CSVBuilder( testFile, true );
    DataSet dataSet = builder.build();
    
    // Verify data set contains the correct number of entries
    assertEquals( "DataSet created is of the wrong size",
                  numberOfDataPoints, dataSet.size() );
    
    // Vefify that only three independent variable names are reported
    String[] independentVariables = dataSet.getIndependentVariables();
    assertEquals( "Checking the correct number of independent variables",
                  3, independentVariables.length );
    
    // Note these will have been sorted into alphabetical order
    assertTrue( "Checking variable 0 name is as expected",
                independentVariables[0].compareTo("Field, 3")==0 );
    assertTrue( "Checking variable 1 name is as expected",
                independentVariables[1].compareTo("Field1")==0 );
    assertTrue( "Checking variable 2 name is as expected",
                independentVariables[2].compareTo("Field2")==0 );
    
    // Test the data set created by the builder
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            DataPoint dataPoint = it.next();
            double field1 = dataPoint.getIndependentValue("Field1");
            double field2 = dataPoint.getIndependentValue("Field2");
            double field3 = dataPoint.getIndependentValue("Field, 3");
            
            // field2 was set to twice field1
            // field3 was set to three times field1
            assertTrue( "Checking independent values are correct",
                        field2==2*field1 && field3==3*field1 );
            
            // The data was set up with this simple equation
            double expectedResult = 5.0 + field1;
            
            assertEquals("Checking data point "+dataPoint,
                         expectedResult, dataPoint.getDependentValue(),
                         TOLERANCE);
        }
    
    // Clean up - remove test file
    testFile.delete();
}