What are some common methods for trend analysis in PHP when dealing with temperature data?

When dealing with temperature data in PHP, common methods for trend analysis include calculating moving averages, linear regression, and exponential smoothing. These techniques can help identify patterns and trends in temperature data over time, allowing for better forecasting and analysis.

// Calculate moving average
function moving_average($data, $period) {
    $result = [];
    $count = count($data);
    
    for ($i = 0; $i < $count - $period + 1; $i++) {
        $result[] = array_sum(array_slice($data, $i, $period)) / $period;
    }
    
    return $result;
}

// Calculate linear regression
function linear_regression($x, $y) {
    $n = count($x);
    $sum_x = array_sum($x);
    $sum_y = array_sum($y);
    $sum_xy = 0;
    $sum_x_squared = 0;
    
    for ($i = 0; $i < $n; $i++) {
        $sum_xy += $x[$i] * $y[$i];
        $sum_x_squared += $x[$i] * $x[$i];
    }
    
    $slope = ($n * $sum_xy - $sum_x * $sum_y) / ($n * $sum_x_squared - $sum_x * $sum_x);
    $intercept = ($sum_y - $slope * $sum_x) / $n;
    
    return ['slope' => $slope, 'intercept' => $intercept];
}

// Calculate exponential smoothing
function exponential_smoothing($data, $alpha) {
    $result = [];
    $result[0] = $data[0];
    
    for ($i = 1; $i < count($data); $i++) {
        $result[$i] = $alpha * $data[$i] + (1 - $alpha) * $result[$i - 1];
    }
    
    return $result;
}