Class Prediction

java.lang.Object
Prediction

public class Prediction extends Object
The Prediction class provides statistical tools for financial forecasting based on historical budget data (2021-2026).

It implements Simple Linear Regression using the Ordinary Least Squares (OLS) method to estimate future trends and calculate the time required to reach specific financial targets.

Key Features:

  • Calculation of future budget values for a given year.
  • Estimation of the timeline (Month/Year) to achieve a desired budget goal.
  • Contains legacy console-based prediction methods for testing.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    getValueForGivenYear(double[] x, double[] y, int targetYear)
    Calculates the predicted financial value for a specific future year.
    static String
    getYearandMonthforGivenValue(double[] x, double[] y, double desiredY)
    Estimates the exact time (Month and Year) when a specific financial target will be reached.
    static void
    main(String[] args)
    Entry point of the program.
    static void
    predict(double[] x, double[] y, double desiredY)
    Estimates the independent variable corresponding to a given dependent value using linear regression (least squares method) and prints it in month/year format.
    static void
    predict(double[] x, double[] y, int targetYear)
    Predicts the dependent value for a given target year using linear regression.
    static void
    predict(double[] x, double[] y, int targetYear, double desiredY)
    Performs a linear prediction using least squares regression.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Prediction

      public Prediction()
  • Method Details

    • main

      public static void main(String[] args)
      Entry point of the program.

      This method initializes an array of government expenditures for various entities from 2021 to 2026 and prompts the user for input to make predictions. It demonstrates the use of the predict(double[], double[], double) method by asking the user for:

      • a year to predict values for,
      • a target value for the Republic Presidency to make a prediction.
      The method reads user input using a Scanner and then calls the predict method with the provided inputs.

      Parameters:
      args - command-line arguments (not used)
      See Also:
    • predict

      public static void predict(double[] x, double[] y, int targetYear, double desiredY)
      Performs a linear prediction using least squares regression.

      Calculates the linear equation y = a + b·x from the given data, prints the equation, predicts y for a target year, and estimates the time period corresponding to a given y-value.

      Parameters:
      x - array of independent values (e.g., years)
      y - array of dependent values (e.g., expenditures)
      targetYear - year to predict the value for
      desiredY - target y-value to find the corresponding time period
    • predict

      public static void predict(double[] x, double[] y, double desiredY)
      Estimates the independent variable corresponding to a given dependent value using linear regression (least squares method) and prints it in month/year format. Calculates the linear equation y = a + b·x from the provided data arrays, prints the equation, and computes the x-value corresponding to desiredY.
      Parameters:
      x - array of independent values (e.g., years)
      y - array of dependent values (e.g., expenditures)
      desiredY - the y-value for which the corresponding time period is estimated
    • predict

      public static void predict(double[] x, double[] y, int targetYear)
      Predicts the dependent value for a given target year using linear regression. Calculates the linear equation y = a + b·x from the provided data arrays, prints the equation, and predicts the y-value for the specified target year.
      Parameters:
      x - array of independent values (e.g., years)
      y - array of dependent values (e.g., expenditures)
      targetYear - the year for which the prediction is made
    • getValueForGivenYear

      public static double getValueForGivenYear(double[] x, double[] y, int targetYear)
      Calculates the predicted financial value for a specific future year.

      This method uses the Least Squares Linear Regression algorithm ($y = a + bx$) to find the trend line of the provided historical data.

      Parameters:
      x - An array of historical years (independent variable).
      y - An array of historical financial values (dependent variable).
      targetYear - The year for which the prediction is requested.
      Returns:
      The predicted amount for the target year, rounded down to the nearest integer.
    • getYearandMonthforGivenValue

      public static String getYearandMonthforGivenValue(double[] x, double[] y, double desiredY)
      Estimates the exact time (Month and Year) when a specific financial target will be reached.

      The method normalizes the year data to increase calculation accuracy and then solves the linear regression equation for $x$ given a value $y$.

      Parameters:
      x - An array of historical years.
      y - An array of historical financial values.
      desiredY - The target financial value (budget amount) to reach.
      Returns:
      A string representation of the estimated date in MM/YYYY format.