Global Temperature Anomaly November 2023

 Given the current OpenAI turmoil, it's hard to say how ChatGPT will develop in the near term. I, for one, welcome our new Microsoft OpenAI overlords, for today at least. I have no hopes for a transparent ChatGPT.



Predicting how temperature will rise over the next few years is difficult. Its seems that the world keeps being surprised that things are worse than we suspected. There may be reasons other than anthropogenic climate effects, but humanity is the main culprit. We're well on our way for 2023 being the hottest year on record.

On 2023-11-14, I downloaded the global monthly temperature anomaly data from the Met Office Hadley Centre observations datasets. The CSV data file contains monthly estimates of the global temperature anomaly from January 1850 to September 2023. The temperature anomalies (deg C) are relative to 1961-1990.

> library(tidyverse)
> df_temp_hadcrut <- read_csv('ChatGPT/data/HadCRUT.5.0.1.0.analysis.summary_series.global.monthly.csv', name_repair = 'universal')
> names(df_temp_hadcrut)
[1] "Time"                           "Anomaly..deg.C."                "Lower.confidence.limit..2.5.." 
[4] "Upper.confidence.limit..97.5.."
> names(df_temp_hadcrut)
[1] "Time"                           "Anomaly..deg.C."                "Lower.confidence.limit..2.5.." 
[4] "Upper.confidence.limit..97.5.."
> df_temp_hadcrut <- df_temp_hadcrut %>% rename(`Anomaly (deg C)` = `Anomaly..deg.C.` )
> df_temp_hadcrut <- df_temp_hadcrut %>% rename(`Lower confidence limit (2.5%)` = `Lower.confidence.limit..2.5..` ) %>% rename(`Upper confidence limit (97.5%)` = `Upper.confidence.limit..97.5..`)
> write_csv(df_temp_hadcrut, 'ChatGPT/data/HadCRUT.5.0.1.0.analysis.summary_series.global.monthly.csv')
> ggplot(df_temp_hadcrut, aes(x = Time, y = `Anomaly (deg C)`)) + ylab('Temperature Anomaly \u00B0C') + ggtitle('Temperature Anomaly') + scale_x_discrete(breaks = c(df_temp_hadcrut$Time[seq(1, nrow(df_temp_hadcrut), by = 120)], df_temp_hadcrut$Time[nrow(df_temp_hadcrut)])) + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
> 

You can see that 2023 is shaping up to be very warm.

ChatGPT helps out


I wanted to try some sort of deep learning to fit the data and make predictions. I decided to see if ChatGPT could give me a start.

Query:
write Pytorch code to perform a smooth fit of the temperature anomaly data from 1850 to 2020 and predict temperature anomaly values for 2021 and 2022

This a pretty terrible query, but surprisingly ChatGPT came up with reasonable start code.


To perform a smooth fit of the temperature anomaly data from 1850 to 2020, you can use a smoothing algorithm such as **moving average smoothing** or **exponential smoothing**. Here is an example of how to perform moving average smoothing on the temperature anomaly data using Python:

```python
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
df = pd.read_csv('global_temperature_anomaly_1850_2023.csv')
df = df[df['Year'] >= 1850]
df = df[df['Year'] <= 2020]

# Compute the moving average
window_size = 12  # Use a window size of 12 months
df['MA'] = df['J-D'].rolling(window_size).mean()

# Plot the data
plt.plot(df['Year'], df['J-D'], label='Temperature Anomaly')
plt.plot(df['Year'], df['MA'], label=f'{window_size}-Month Moving Average')
plt.title('Global Temperature Anomaly (1850-2020)')
plt.xlabel('Year')
plt.ylabel('Temperature Anomaly (°C)')
plt.legend()
plt.show()
```

This code reads the CSV file `global_temperature_anomaly_1850_2023.csv` into a `pandas` DataFrame and computes the 12-month moving average of the `J-D` column (which represents the annual temperature anomaly) for the years 1850 to 2020. It then plots the original data and the moving average on the same graph using `matplotlib`.

To predict temperature anomaly values for 2021 and 2022, you can use a machine learning algorithm such as **LSTM**. Here is an example of how to use PyTorch to train an LSTM model on the temperature anomaly data and predict future values:

```python
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader

# Define the LSTM model
class LSTM(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(LSTM, self).__init__()
        self.hidden_size = hidden_size
        self.lstm = nn.LSTM(input_size, hidden_size)
        self.linear = nn.Linear(hidden_size, output_size)

    def forward(self, input):
        output, _ = self.lstm(input.view(len(input), 1, -1))
        output = self.linear(output.view(len(input), -1))
        return output[-1]

# Load the data
df = pd.read_csv('global_temperature_anomaly_1850_2023.csv')
df = df[df['Year'] >= 1850]
df = df[df['Year'] <= 2020]
data = torch.tensor(df['J-D'].values, dtype=torch.float32)

# Define the dataset and dataloader
class TemperatureDataset(Dataset):
    def __init__(self, data, seq_len):
        self.data = data
        self.seq_len = seq_len

    def __len__(self):
        return len(self.data) - self.seq_len

    def __getitem__(self, idx):
        return self.data[idx:idx+self.seq_len], self.data[idx+self.seq_len]

seq_len = 12  # Use a sequence length of 12 months
dataset = TemperatureDataset(data, seq_len)
dataloader = DataLoader(dataset, batch_size=1)

# Train the LSTM model
input_size = seq_len
hidden_size = 32
output_size = 1
lr = 0.001
num_epochs = 100

model = LSTM(input_size, hidden_size, output_size)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=lr)

for epoch in range(num_epochs):
    for X, y in dataloader:
        optimizer.zero_grad()
        y_pred = model(X)
        loss = criterion(y_pred, y)
        loss.backward()
        optimizer.step()

# Predict future values
future = 24  # Predict 24 months into the future
predictions = []
with torch.no_grad():
    model.eval()
    for i in range(future):
        X = data[-seq_len:].view(1, -1)
        y_pred = model(X)
        predictions.append(y_pred.item())
        data = torch.cat([data, y_pred.view(-1)])

# Plot the data and predictions
plt.plot(df['Year'], df['J-D'], label='Temperature Anomaly')
plt.plot(range(2021, 2023+1), predictions, label='Predictions')
plt.title('Global Temperature Anomaly (1850-2023)')
plt.xlabel('Year')
plt.ylabel('Temperature Anomaly (°C)')
plt.legend()
plt.show()
```

This code reads the CSV file `global_temperature_anomaly_1850_2023.csv` into a `pandas` DataFrame and trains an LSTM model on the `J-D` column (which represents the annual temperature anomaly) for

Source: Conversation with Bing, 11/14/2023
(1) Time Series Anomaly Detection using LSTM Autoencoders. https://github.com/curiousily/Getting-Things-Done-with-Pytorch/blob/master/manuscript/06.time-series-anomaly-ecg.md.
(2) Automatic differentiation package - torch.autograd — PyTorch 2.1 .... https://pytorch.org/docs/stable/autograd.html.
(3) Learning PyTorch with Examples. https://pytorch.org/tutorials/beginner/pytorch_with_examples.html.
(4) Performance Tuning Guide — PyTorch Tutorials 2.1.0+cu121 documentation. https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html.
(5) PyTorch: is there a definitive training loop similar to Keras' fit()?. https://stackoverflow.com/questions/59584457/pytorch-is-there-a-definitive-training-loop-similar-to-keras-fit.
(6) PyTorch: Predicting future values with LSTM - Stack Overflow. https://stackoverflow.com/questions/66048406/pytorch-predicting-future-values-with-lstm.
(7) Predicting future values with LSTM - PyTorch Forums. https://discuss.pytorch.org/t/predicting-future-values-with-lstm/110916.
(8) python - PyTorch : predict single example - Stack Overflow. https://stackoverflow.com/questions/51041128/pytorch-predict-single-example.
(9) Weather Forecasting | Papers With Code. https://paperswithcode.com/task/weather-forecasting.
(10) undefined. https://avatars.githubusercontent.com/u/150327?v=4.
(11) undefined. https://github.com/curiousily/Getting-Things-Done-with-Pytorch/blob/master/manuscript/06.time-series-anomaly-ecg.md?raw=true.
(12) undefined. https://desktop.github.com.
(13) undefined. https://docs.github.com/articles/about-issue-and-pull-request-templates.
(14) undefined. https://github.com/curiousily/Getting-Things-Done-with-Pytorch/raw/master/manuscript/06.time-series-anomaly-ecg.md.

{ending was cut off}

I used this code as a start. It's a very simple NN using an LSTM layer followed by a linear layer. I kept the LSTM model and the data loader as well as much of the main code. The code isn't fast because I didn't use a GPU.

You can find the full code on GitHub.

I used the data up until the end of 2020 as training and predicted values for 2021 to September 2023. Here's what the fit looks like. 


The predictions are particularly accurate. The rapid temperature rise over the last few years confounds the training data. In general, this sort of noisy data with shifting trends is difficult to predict.





No comments:

Post a Comment