Skip to content

Instantly share code, notes, and snippets.

@techwithshadab
Created November 3, 2021 21:03
Show Gist options
  • Save techwithshadab/8da5aabd2ccd345b81de17a56fe2e605 to your computer and use it in GitHub Desktop.
Save techwithshadab/8da5aabd2ccd345b81de17a56fe2e605 to your computer and use it in GitHub Desktop.
def print_result(result):
selection = sample_most_likely(result.eigenstate)
value = portfolio.portfolio_value(selection, mu, sigma, risk_factor, budget, penalty)
print('Optimal: selection {}, value {:.4f}'.format(selection, value))
eigenvector = result.eigenstate if isinstance(result.eigenstate, np.ndarray) else result.eigenstate.to_matrix()
probabilities = np.abs(eigenvector)**2
i_sorted = reversed(np.argsort(probabilities))
print('\n----------------- Full result ---------------------')
print('selection\tvalue\t\tprobability')
print('---------------------------------------------------')
states, values, probs = [], [], []
for i in i_sorted:
x = index_to_selection(i, num_assets)
value = portfolio.portfolio_value(x, mu, sigma, risk_factor, budget, penalty)
probability = probabilities[i]
print('%10s\t%.4f\t\t%.4f' %(x, value, probability))
states.append(''.join(str(i) for i in x))
values.append(value)
probs.append(probability)
return selection, states, values, probs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment