JSFiddle - React, Tailwind, and code Playground

by sberube

JavaScript

const awsCloudSpend = [
  120000,  // January
  105000,  // February
  110000,  // March
  95000,   // April
  105000,  // May
  125000,  // June
  135000,  // July
  145000,  // August
  120000,  // September
  105000,  // October
  115000,  // November
  130000   // December
];

// Exponential smoothing forecast parameters
const smoothingFactor = 0.3;  // Adjust this value (0 < alpha < 1) for desired smoothing effect

// Calculate the forecast for the next 6 months
const forecastedData = [];
const initialForecast = awsCloudSpend[awsCloudSpend.length - 1];
forecastedData.push(initialForecast);

for (let i = 1; i <= 6; i++) {
  const previousActual = awsCloudSpend[awsCloudSpend.length - i];
  const forecast = Math.round(smoothingFactor * previousActual + (1 - smoothingFactor) * forecastedData[i - 1]);
  forecastedData.push(forecast);
}

console.log(forecastedData);