Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pypfopt/discrete_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def greedy_portfolio(self, reinvest=False, verbose=False):
# Calculate the equivalent continuous weights of the shares that
# have already been bought
current_weights = np.array(buy_prices) * np.array(shares_bought)
current_weights /= current_weights.sum()
invested = current_weights.sum()
if invested > 0:
current_weights /= invested
ideal_weights = np.array([i[1] for i in self.weights])
deficit = ideal_weights - current_weights

Expand Down
12 changes: 12 additions & 0 deletions tests/test_discrete_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
from tests.utilities_for_tests import get_data, setup_efficient_frontier


@pytest.mark.parametrize("budget, expected, cash", [(100, {"B": 1}, 50), (10, {}, 10)])
def test_greedy_portfolio_with_no_initial_shares(budget, expected, cash):
da = DiscreteAllocation(
{"A": 0.9, "B": 0.1}, pd.Series({"A": 1000.0, "B": 50.0}),
total_portfolio_value=budget,
)
with np.errstate(divide="raise", invalid="raise"):
allocation, leftover = da.greedy_portfolio()
assert allocation == expected
assert leftover == pytest.approx(cash)


@pytest.mark.parametrize(
"prices",
[
Expand Down
Loading