The issue here is that you’re trying to pass a series (an array-like structure) to the beta_kalman function. However, this series only contains values from one of the columns (asset_1), while your function expects two separate arguments (s1 and s2).
One way to solve this issue is by modifying the rolling function to pass the correct argument to beta_kalman. We can achieve this by using the .apply() method, which passes the series as a single argument.
Here’s an example of how you could modify your code:
df_betas2 = df_returns.copy()
for i in range(len(df_returns.index) - window + 1):
temp_returns = df_returns[['asset_1', 'asset_2']].iloc[i:i + window].copy()
# Use the `.apply()` method to pass the series as a single argument
df_betas2.loc[temp_returns.index[-1], 'BETA'] = beta_kalman(temp_returns['asset_1'].values, temp_returns['asset_2'].values)[-1]
In this modified version, we use temp_returns['asset_1'].values and temp_returns['asset_2'].values to get the values of both columns as arrays. These arrays are then passed to beta_kalman.
Another way to solve this issue is by modifying your function to accept a single series (an array-like structure) with two elements. For example:
def beta_kalman(s):
s1, s2 = s
# Your function implementation here...
pass
In this version, we define beta_kalman to accept a single argument s, which is an array-like structure containing the values of both columns. We then unpack these values into separate variables s1 and s2.
Here’s how you could use this modified function in your code:
df_betas2 = df_returns.copy()
for i in range(len(df_returns.index) - window + 1):
temp_returns = df_returns[['asset_1', 'asset_2']].iloc[i:i + window].copy()
# Use the `.apply()` method to pass the series as a single argument
df_betas2.loc[temp_returns.index[-1], 'BETA'] = beta_kalman(temp_returns[['asset_1', 'asset_2']].values)[0]
In this version, we use temp_returns[['asset_1', 'asset_2']].values to get the values of both columns as an array-like structure. This structure is then passed to beta_kalman, which unpacks it into separate variables.
Both of these approaches should solve your problem.
Last modified on 2024-12-21