-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimalportfolio.java
More file actions
68 lines (55 loc) · 2.31 KB
/
Optimalportfolio.java
File metadata and controls
68 lines (55 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.List;
import java.util.ArrayList;
public class OptimalPortfolio {
private List<Asset> optimalAllocation;
private double expectedPortfolioReturn;
private double portfolioRiskLevel;
private boolean foundFeasibleSolution;
public OptimalPortfolio() {
this.optimalAllocation = new ArrayList<>();
this.expectedPortfolioReturn = -Double.MAX_VALUE;
this.portfolioRiskLevel = Double.MAX_VALUE;
this.foundFeasibleSolution = false;
}
public OptimalPortfolio(List<Asset> allocation, double expectedReturn, double riskLevel) {
this.optimalAllocation = allocation;
this.expectedPortfolioReturn = expectedReturn;
this.portfolioRiskLevel = riskLevel;
this.foundFeasibleSolution = true;
}
public List<Asset> getOptimalAllocation() {
return optimalAllocation;
}
public double getExpectedPortfolioReturn() {
return expectedPortfolioReturn;
}
public double getPortfolioRiskLevel() {
return portfolioRiskLevel;
}
public boolean isFoundFeasibleSolution() {
return foundFeasibleSolution;
}
public void updatePortfolio(List<Asset> currentAllocation, double currentReturn, double currentRisk) {
List<Asset> allocationCopy = new ArrayList<>();
for (Asset asset : currentAllocation) {
allocationCopy.add(new Asset(asset, asset.getAllocatedQuantity()));
}
this.optimalAllocation = allocationCopy;
this.expectedPortfolioReturn = currentReturn;
this.portfolioRiskLevel = currentRisk;
this.foundFeasibleSolution = true;
}
@Override
public String toString() {
if (!foundFeasibleSolution) {
return "No feasible solution found within the specified risk tolerance.";
}
StringBuilder sb = new StringBuilder("Optimal Allocation:\n");
for (Asset asset : optimalAllocation) {
sb.append(" ").append(asset.toString()).append("\n");
}
sb.append("Expected Portfolio Return: ").append(String.format("%.3f", expectedPortfolioReturn)).append("\n");
sb.append("Portfolio Risk Level: ").append(String.format("%.3f", portfolioRiskLevel));
return sb.toString();
}
}