## 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 ]; AssetScenarios = mvnrnd(m, C, 20000); p = PortfolioCVaR; p = setScenarios(p, AssetScenarios); p = setDefaultConstraints(p); p = setProbabilityLevel(p, 0.9); pwgt = estimateFrontierByRisk(p, [0.12, 0.14, 0.16]); display(pwgt)```
```pwgt = 0.3594 0.2524 0.1543 0.3164 0.3721 0.4248 0.1044 0.1193 0.1298 0.2199 0.2563 0.2910```

Sometimes, you can request a risk for which no efficient portfolio exists. Based on the previous example, suppose that you want a portfolio with 6% risk (individual assets in this universe have risks ranging from 7% to 42.5%). It turns out that a portfolio with 6% 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.06)`
```Warning: One or more target risk values are outside the feasible range [ 0.0735749, 0.436667 ]. Will return portfolios associated with endpoints of the range for these values. > In PortfolioCVaR.estimateFrontierByRisk at 80 pwgt = 0.7899 0.0856 0.0545 0.0700```
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 CVaR Portfolio Risks and Returns).
```prsk = estimatePortRisk(p, p.estimateFrontierLimits); display(prsk)```
```prsk = 0.0736 0.4367```
This result indicates that efficient portfolios have risks that range from 7% to 42.5%. Note, your results for these examples may be different due to the random generation of scenarios.

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.3594 0.2524 0.1543 0.3164 0.3721 0.4248 0.1044 0.1193 0.1298 0.2199 0.2563 0.2910 pbuy = 0.0594 0 0 0.0164 0.0721 0.1248 0 0 0 0.1199 0.1563 0.1910 psell = 0 0.0476 0.1457 0 0 0 0.0956 0.0807 0.0702 0 0 0```
If you do not specify an initial portfolio, the purchase and sale weights assume that your initial portfolio is `0`.