Skip to content

Commit

Permalink
Merge pull request #24 from arcondello/fix/nans
Browse files Browse the repository at this point in the history
Fix covariance calculation to not generate NaNs
  • Loading branch information
arcondello authored Jun 28, 2023
2 parents bac53ab + 9d71936 commit 870721b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion single_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ def load_data(self, file_path='', dates=None, df=None, num=0):
self.price = self.df.iloc[-1]
self.monthly_returns = self.df[list(self.stocks)].pct_change().iloc[1:]
self.avg_monthly_returns = self.monthly_returns.mean(axis=0)
self.covariance_matrix = self.monthly_returns.cov()
self.covariance_matrix = covariance_matrix = self.monthly_returns.cov()

# convert any NaNs in the covariance matrix to 0s
covariance_matrix.replace(np.nan, 0)

def build_cqm(self, max_risk=None, min_return=None, init_holdings=None):
"""Build and store a CQM.
Expand Down

0 comments on commit 870721b

Please sign in to comment.