# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/nxshi/IDS131F21/blob/main/ProblemSets/3a.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="eMRDpElwlqFf"
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# + id="RrPZ5m-lpk1j" colab={"base_uri": "https://localhost:8080/"} outputId="39f46b67-82c7-4384-fcb7-9a1699477ce9"
import os
from google.colab import drive
drive.mount('/content/gdrive')
DATADIR = os.path.join('/content/gdrive/My Drive/ps3')
os.chdir(DATADIR)

# + id="22Pz3DvAp0tk"
df = pd.read_csv('CO2Data.csv', usecols = ['  Yr', ' Mn', '      Date', 'CO2', 'seasonally'])
df = df.set_axis(["Yr", "Mn", "Date", "CO2", 'seasonally'], axis=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="WO0NxadQvWIx" outputId="a506288b-758a-4a60-e765-975b3d9dd029"
df = df[df.CO2 != -99.99]
df.head()

# + id="Cyr4T4zzqGxj" colab={"base_uri": "https://localhost:8080/", "height": 330} outputId="262664e7-da4d-4307-9ddc-f4c2127a8a9a"
x = df["Date"]-1958.2027
y = df["CO2"]

a2, a1 = np.polyfit(x, y, 1)

print('a2 = ' + str(a2))
print('a1 = ' + str(a1))


plt.plot(x, y,'.', markersize=1, label = 'CO2 Data')
plt.plot(x, a2*x + a1, label = 'linear fit')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + id="k2tVK8iHt-Bu" colab={"base_uri": "https://localhost:8080/", "height": 296} outputId="675b4ce7-17dc-46dc-84ac-f237e20a8519"
plt.plot(x, y-(a2*x + a1),'.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + colab={"base_uri": "https://localhost:8080/", "height": 347} id="2yxVotXMxK48" outputId="d927093a-e81c-4175-a3ba-e43fbb7a8344"
b3, b2, b1 = np.polyfit(x, y, 2)

print('b3 = ' + str(b3))
print('b2 = ' + str(b2))
print('b1 = ' + str(b1))

plt.plot(x, y,'.', markersize=1, label = 'CO2 Data')
plt.plot(x, b1 + b2*x + b3*(x**2), markersize=1, label = 'quadratic fit')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="XTURuLpM0ZIC" outputId="3794d575-61b5-4a71-c422-06732f7012a6"
plt.plot(x, y-(b1 + b2*x + b3*(x**2)),'.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + colab={"base_uri": "https://localhost:8080/", "height": 381} id="ZtfRyuV1yR_B" outputId="99782904-c4e9-463b-9494-cf4bf5951351"
g5, g4, g3, g2, g1 = np.polyfit(x, y, 4)

print('g5 = ' + str(g5))
print('g4 = ' + str(g4))
print('g3 = ' + str(g3))
print('g2 = ' + str(g2))
print('g1 = ' + str(g1))

plt.plot(x, y,'.', markersize=1, label = 'CO2 Data')
plt.plot(x, g1 + g2*x + g3*(x**2) + g4*(x**3) + g5*(x**4), markersize=1, label = 'quartic fit')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="K3FMlwov0e4H" outputId="2ad1bf88-b3ef-4ee7-c27c-e7261ecec10b"
plt.plot(x, y-(g1 + g2*x + g3*(x**2) + g4*(x**3) + g5*(x**4)),'.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + id="GPrmx7Kasz1A"
df['F2'] = (b1 + b2*x + b3*(x**2))

# + id="4aX4OIf63d34"
df['rF2'] = y-(b1 + b2*x + b3*(x**2))

# + id="QSENcoYa20Us"
month_means = df.groupby('Mn').mean()

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="CmzY4xc-2y9g" outputId="bc06a5de-59bc-4123-9eac-3e937afea9bf"
plt.plot(month_means.rF2)
plt.ylabel('Periodic Signal')
plt.xlabel('Month')

# + colab={"base_uri": "https://localhost:8080/"} id="Qm9GZDTS5j-m" outputId="19a083c8-ac7f-4ab5-89dd-5167bd2309ca"
month_means.rF2

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="G985CyrquBia" outputId="404603ea-81ef-42a8-c7b9-bd1df5696c98"
df.head()

# + id="y1JY06lu7HQc"
df['Seasonality'] = 0

# + colab={"base_uri": "https://localhost:8080/"} id="yC5OO1Oz4IDc" outputId="0b945389-7b46-4497-f561-44362ed12ae1"
for i in range(1,month_means.shape[0]+1):
  print(i)
  print(month_means.rF2[i])
  df.loc[df.Mn == i, 'Seasonality'] = df.loc[df.Mn == i, 'F2']+ month_means.rF2[i]

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="BfItA9O44Ew1" outputId="7bba2180-83ae-4086-d032-4cc765131f70"
df.head()

# + id="R9TjurjC4GBk" colab={"base_uri": "https://localhost:8080/", "height": 296} outputId="ec0d3ac1-2aed-4f19-b60a-bb1f97081b82"
plt.plot(x, df.CO2, markersize=2, label = 'CO2 data')
plt.plot(x, df.Seasonality, markersize=2, label = 'Seasonality Adjusted fit: F2(ti) + Pi')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="l41zW8DDvXcb" outputId="7ed511a8-dbe9-4ffe-cdb8-6df7000a893a"
plt.plot(x, df.CO2-df.Seasonality, '.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="xw7xO-0lvrM5" outputId="5a3c0ff3-f367-4d66-f4c6-59c50fa8e475"
plt.plot(x, df.seasonally, markersize=2, label = 'Seasonally Adjusted Data')
plt.plot(x, df.F2, markersize=2, label = 'F2(ti) fit')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + colab={"base_uri": "https://localhost:8080/", "height": 296} id="vwimxWOKvy60" outputId="3edcaf3c-ff0c-49a8-923a-f7fe083d91b7"
plt.plot(x, df.seasonally-df.F2, '.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + id="h5jC11G3teFk" colab={"base_uri": "https://localhost:8080/", "height": 296} outputId="36f7c7ab-3a76-4b8e-92dc-f63e0a22cbc0"
plt.plot(x, df.seasonally, markersize=2, label = 'Seasonally Adjusted Data')
plt.plot(x, df.Seasonality, markersize=2, label = 'Seasonality Adjusted fit: F2(ti) + Pi')
leg = plt.legend(loc='upper center')
plt.xlabel('Time (1958 = 0th year)')
plt.ylabel('CO2 concentration [ppm]')

# + id="-T4W32tpztx1" colab={"base_uri": "https://localhost:8080/", "height": 296} outputId="a4c43537-dfd2-4e45-aa05-ccf8d4ef9e5d"
plt.plot(x, df.seasonally-df.Seasonality, '.', markersize=2)
plt.ylabel('Residuals Error')
plt.xlabel('Time (1958 = 0th year)')

# + id="GXIniYHM0xkg"


StarcoderdataJupyterScriptsDedupFiltered