Skip to content

Commit

Permalink
Use linalg.solve in place of inverse (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirca committed Mar 10, 2022
1 parent bf67c67 commit 70ebdf4
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions deepdow/layers/allocate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def forward(self, covmat, rets=None):
device = covmat.device
dtype = covmat.dtype

covmat_inv = torch.inverse(covmat)
ones = torch.ones(n_samples, n_assets, 1).to(device=device, dtype=dtype)

if rets is not None:
expected_returns = rets.view(n_samples, n_assets, 1)
else:
expected_returns = ones

w_unscaled = torch.matmul(covmat_inv, expected_returns)
w_unscaled = torch.linalg.solve(covmat, expected_returns)
denominator = torch.matmul(ones.permute(0, 2, 1), w_unscaled)
w = w_unscaled / denominator

Expand Down

0 comments on commit 70ebdf4

Please sign in to comment.