Developed a model from scratch and compared to this #49
Replies: 10 comments 7 replies
-
Hi! Thanks for sharing! Do you have a specific question you'd like to ask or further insights to share? Regarding these examples, could you share a working example (and the time series) of how you generated these forecasts from both models? |
Beta Was this translation helpful? Give feedback.
-
Hey! Here's my whole notebook where i created my model, the plots i had
provided are near the end of the notebook. The algebra behind it all is
documented in my github repo https://github.com/Vin1001/TSA/
…On Thu, 11 Apr 2024 at 14:36, Abdul Fatir ***@***.***> wrote:
Hi! Thanks for sharing! Do you have a specific question you'd like to ask
or further insights to share? Regarding these examples, could you share a
working example (and the time series) of how you generated these forecasts
from both models?
—
Reply to this email directly, view it on GitHub
<#49 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H35OU74UQ6NKKQCZETY4ZHCTAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOBRGIYDQ>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9081208@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
The notebook that i provided through mail is a newer version of the
notebook that's available on my repo in the notebooks folder. I can update
that with this one if needed.
…On Thu, 11 Apr, 2024, 5:42 pm Abdul Fatir, ***@***.***> wrote:
Did you forget to attach the notebook? I can't find something like that in
your repo.
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H4MON2BQKFARNZSFNDY4Z43BAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOBTGMZTI>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9083334@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
Alright I'll update it in my repo. It'll be visible there. It is named
'autoreg'
…On Thu, 11 Apr, 2024, 6:00 pm Abdul Fatir, ***@***.***> wrote:
FYI, if you are attaching any file through email, it's not going through.
I only see your text.
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H4DCFRVB6J5VC7WBGTY4Z67HAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOBTGUZDC>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9083521@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
The last column will the target since it contains the future value, which
we need to predict. Kindly see the attached note for clarification.
…On Fri, 12 Apr, 2024, 12:05 pm Abdul Fatir, ***@***.***> wrote:
I checked the code briefly and I am not completely sure what's going on in
the LinearRegression model. It looks like you're setting your last column
as the target but the last column is actually a lagged value and I
believe the target should be column 0. Can you explain your code?
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66HZIXSTX3WOICFBRMDDY456CRAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOJRHE2TG>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9091953@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
There were some issues with the inference parameters for Chronos in your code. When in doubt, please use the defaults. I played around a bit and here's what I found. Chronos (defaults)import torch
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from chronos import ChronosPipeline
def getData(name, per, intrvl, cols=["Close"]):
df = yf.Ticker(name)
df = pd.DataFrame(df.history(period=per, interval=intrvl))
return df[cols].copy()
df = getData(name="adanient.ns", per="20d", intrvl="15m")
series = df.reset_index()["Close"]
prediction_length = 32
context, actuals = series[:-prediction_length], series[-prediction_length:]
pipeline = ChronosPipeline.from_pretrained(
"amazon/chronos-t5-base", device_map="cuda:2"
)
forecast = pipeline.predict(torch.tensor(context), prediction_length=prediction_length)[
0
].numpy()
forecast = forecast
forecast_index = range(len(context), len(context) + prediction_length)
low, median, high = np.quantile(forecast, [0.25, 0.5, 0.75], axis=0)
plt.figure(figsize=(8, 4))
plt.plot(context, color="royalblue", label="historical data")
plt.plot(forecast_index, actuals, color="forestgreen", label="actuals")
plt.plot(forecast_index, median, color="tomato", label="median forecast")
plt.fill_between(
forecast_index,
low,
high,
color="tomato",
alpha=0.3,
label="50% prediction interval",
)
plt.legend(loc="upper left")
plt.title("Chronos")
plt.grid()
plt.show() OutputYou might see some quantization artifacts in the forecast above. This happens because the absolute scale of the time series is significantly larger than its variation. One quick fix for the quantization artifacts is to use another type of normalization. Let's try standardization. Chronos (with standardization)import torch
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from chronos import ChronosPipeline
def getData(name, per, intrvl, cols=["Close"]):
df = yf.Ticker(name)
df = pd.DataFrame(df.history(period=per, interval=intrvl))
return df[cols].copy()
df = getData(name="adanient.ns", per="20d", intrvl="15m")
series = df.reset_index()["Close"]
prediction_length = 32
context, actuals = series[:-prediction_length], series[-prediction_length:]
mean, scale = context.mean(), context.std()
norm_context = (context - mean) / scale
pipeline = ChronosPipeline.from_pretrained(
"amazon/chronos-t5-base", device_map="cuda:2"
)
forecast = pipeline.predict(
torch.tensor(norm_context), prediction_length=prediction_length
)[0].numpy()
forecast = forecast * scale + mean
forecast_index = range(len(context), len(context) + prediction_length)
low, median, high = np.quantile(forecast, [0.25, 0.5, 0.75], axis=0)
plt.figure(figsize=(8, 4))
plt.plot(context, color="royalblue", label="historical data")
plt.plot(forecast_index, actuals, color="forestgreen", label="actuals")
plt.plot(forecast_index, median, color="tomato", label="median forecast")
plt.fill_between(
forecast_index,
low,
high,
color="tomato",
alpha=0.3,
label="50% prediction interval",
)
plt.legend(loc="upper left")
plt.title("Chronos")
plt.grid()
plt.show() OutputNice! The quantization artifacts are gone. The forecasts are a bit off but do note that we're unrolling into the entire prediction horizon without feeding in the actual lags as in the case of the linear model. Let's try rolling 1-step predictions. Chronos (rolling 1-step)import torch
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from chronos import ChronosPipeline
def getData(name, per, intrvl, cols=["Close"]):
df = yf.Ticker(name)
df = pd.DataFrame(df.history(period=per, interval=intrvl))
return df[cols].copy()
df = getData(name="adanient.ns", per="20d", intrvl="15m")
series = df.reset_index()["Close"]
prediction_length = 32
pipeline = ChronosPipeline.from_pretrained(
"amazon/chronos-t5-base", device_map="cuda:2"
)
# rolling prediction
forecast = []
for j in range(prediction_length):
context = series[: -prediction_length + j]
mean, scale = context.mean(), context.std()
norm_context = (context - mean) / scale
forecast_j = pipeline.predict(
torch.tensor(norm_context),
prediction_length=1
)[0].numpy()
forecast_j = forecast_j * scale + mean
forecast.append(forecast_j)
forecast = np.concatenate(forecast, axis=-1)
# for plotting
context, actuals = series[:-prediction_length], series[-prediction_length:]
forecast_index = range(len(context), len(context) + prediction_length)
low, median, high = np.quantile(forecast, [0.25, 0.5, 0.75], axis=0)
plt.figure(figsize=(8, 4))
plt.plot(context, color="royalblue", label="historical data")
plt.plot(forecast_index, actuals, color="forestgreen", label="actuals")
plt.plot(forecast_index, median, color="tomato", label="rolling median forecast")
plt.fill_between(
forecast_index,
low,
high,
color="tomato",
alpha=0.3,
label="50% prediction interval",
)
plt.legend(loc="upper left")
plt.title("Chronos")
plt.grid()
plt.show() |
Beta Was this translation helpful? Give feedback.
-
Okay I have uploaded a file named TS_concept.pdf in the notebooks directory
…On Fri, 12 Apr, 2024, 2:03 pm Abdul Fatir, ***@***.***> wrote:
As I mentioned before, your attachments are not showing up. Please post
via Github web.
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H5P5WR3HF4GTUTRIEDY46L4BAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOJTGEZTS>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9093139@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
If I wasn't doing it in the code, why would it magically give the expected
result?
…On Fri, 12 Apr, 2024, 3:06 pm Abdul Fatir, ***@***.***> wrote:
I understand what you're trying to do, but please check if you're actually
doing that in your code.
self.y_train[self.new_mat.iloc[:,target-1:target].columns] = np.array(self.new_mat.iloc[:,target-1:target])self.X_train = self.new_mat.drop(self.y_train.columns, axis=1)
In any case, I have answered the issue about Chronos. Let me know if you
any other questions *about Chronos*.
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H54QE5WULNR6T63GYLY46TK7AVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOJTHA4DA>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9093880@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
Its simple data preprocessing. It's not even that i have used high level ML
libraries like sklearn. They're just pandas and numpy modules.
…On Fri, 12 Apr, 2024, 3:45 pm Abdul Fatir, ***@***.***> wrote:
It's not clear to me from your code. Anyway, let me know if you have any
other questions about Chronos. :)
—
Reply to this email directly, view it on GitHub
<#49 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H7TJD3YSQVKZHIQ6UDY46X25AVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TAOJUGIZTI>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/comments/9094234@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
I am still ready to explain the whole code line by line if you want. Let's
setup a meeting.
…On Fri, 12 Apr, 2024, 4:02 pm Abdul Fatir, ***@***.***> wrote:
Closed #49 <runs-on/runs-on#49> as
resolved.
—
Reply to this email directly, view it on GitHub
<#49>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A4U66H44AFHLIEJG33EKEDDY46Z2NAVCNFSM6AAAAABGBTUKRGVHI2DSMVQWIX3LMV45UABFIRUXGY3VONZWS33OIV3GK3TUHI5E433UNFTGSY3BORUW63R3GEZDCOJWG42A>
.
You are receiving this because you authored the thread.Message ID:
<amazon-science/chronos-forecasting/repo-discussions/49/discussion_event/1219674
@github.com>
|
Beta Was this translation helpful? Give feedback.
-
I have developed my own version of OLS and used it in stock price forecasting: https://github.com/Vin1001/TSA
Here's a comparison between the two 👇
Beta Was this translation helpful? Give feedback.
All reactions