## Obtaining Efficient Portfolios for Target Risks

To obtain efficient portfolios that have targeted portfolio risks, the `estimateFrontierByRisk` function accepts one or more target portfolio risks and obtains efficient portfolios with the specified risks. Suppose that you have a universe of four assets where you want to obtain efficient portfolios with target portfolio risks of 12%, 14%, and 16%.

```m = [ 0.05; 0.1; 0.12; 0.18 ]; C = [ 0.0064 0.00408 0.00192 0; 0.00408 0.0289 0.0204 0.0119; 0.00192 0.0204 0.0576 0.0336; 0 0.0119 0.0336 0.1225 ]; p = Portfolio; p = setAssetMoments(p, m, C); p = setDefaultConstraints(p); pwgt = estimateFrontierByRisk(p, [0.12, 0.14, 0.16]); display(pwgt)```
```pwgt = 0.3984 0.2659 0.1416 0.3064 0.3791 0.4474 0.0882 0.1010 0.1131 0.2071 0.2540 0.2979```

Sometimes, you can request a risk for which no efficient portfolio exists. Based on the previous example, suppose that you want a portfolio with 7% risk (individual assets in this universe have risks ranging from 8% to 35%). It turns out that a portfolio with 7% risk cannot be formed with these four assets. `estimateFrontierByRisk` warns if your target risks are outside the range of efficient portfolio risks and replaces it with the endpoint of the efficient frontier closest to your target risk:

`pwgt = estimateFrontierByRisk(p, 0.07)`
```Warning: One or more target risk values are outside the feasible range [ 0.0769288, 0.35 ]. Will return portfolios associated with endpoints of the range for these values. > In Portfolio.estimateFrontierByRisk at 82 pwgt = 0.8891 0.0369 0.0404 0.0336```
The best way to avoid this situation is to bracket your target portfolio risks with `estimateFrontierLimits` and `estimatePortRisk` (see Obtaining Endpoints of the Efficient Frontier and Obtaining Portfolio Risks and Returns).
```prsk = estimatePortRisk(p, p.estimateFrontierLimits); display(prsk)```
```prsk = 0.0769 0.3500```
This result indicates that efficient portfolios have risks that range from 7.7% to 35%.

Starting with an initial portfolio, `estimateFrontierByRisk` also returns purchases and sales to get from your initial portfolio to the target portfolios on the efficient frontier. For example, given an initial portfolio in `pwgt0`, you can obtain purchases and sales from the example with target risks of 12%, 14%, and 16%:

```pwgt0 = [ 0.3; 0.3; 0.2; 0.1 ]; p = setInitPort(p, pwgt0); [pwgt, pbuy, psell] = estimateFrontierByRisk(p, [0.12, 0.14, 0.16]); display(pwgt) display(pbuy) display(psell)```
```pwgt = 0.3984 0.2659 0.1416 0.3064 0.3791 0.4474 0.0882 0.1010 0.1131 0.2071 0.2540 0.2979 pbuy = 0.0984 0 0 0.0064 0.0791 0.1474 0 0 0 0.1071 0.1540 0.1979 psell = 0 0.0341 0.1584 0 0 0 0.1118 0.0990 0.0869 0 0 0```
If you do not specify an initial portfolio, the purchase and sale weights assume that your initial portfolio is `0`.