## Working with Transaction Costs

The difference between net and gross portfolio returns is transaction costs. The net portfolio return proxy has distinct proportional costs to purchase and to sell assets which are maintained in the `Portfolio` object properties `BuyCost` and `SellCost`. Transaction costs are in units of total return and, as such, are proportional to the price of an asset so that they enter the model for net portfolio returns in return form. For example, suppose that you have a stock currently priced \$40 and your usual transaction costs are five cents per share. Then the transaction cost for the stock is 0.05/40 = 0.00125 (as defined in Net Portfolio Returns). Costs are entered as positive values and credits are entered as negative values.

### Setting Transaction Costs Using the `Portfolio` Function

To set up transaction costs, you must specify an initial or current portfolio in the `InitPort` property. If the initial portfolio is not set when you set up the transaction cost properties, `InitPort` is `0`. The properties for transaction costs can be set using the`Portfolio` object. For example, assume that purchase and sale transaction costs are in the variables `bc` and `sc` and an initial portfolio is in the variable `x0`, then transaction costs are set:

```bc = [ 0.00125; 0.00125; 0.00125; 0.00125; 0.00125 ]; sc = [ 0.00125; 0.007; 0.00125; 0.00125; 0.0024 ]; x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio('BuyCost', bc, 'SellCost', sc, 'InitPort', x0); disp(p.NumAssets) disp(p.BuyCost) disp(p.SellCost) disp(p.InitPort)```
``` 5 0.0013 0.0013 0.0013 0.0013 0.0013 0.0013 0.0070 0.0013 0.0013 0.0024 0.4000 0.2000 0.2000 0.1000 0.1000```

### Setting Transaction Costs Using the `setCosts` Function

You can also set the properties for transaction costs using `setCosts`. Assume that you have the same costs and initial portfolio as in the previous example. Given a `Portfolio` object `p` with an initial portfolio already set, use `setCosts` to set up transaction costs:

```bc = [ 0.00125; 0.00125; 0.00125; 0.00125; 0.00125 ]; sc = [ 0.00125; 0.007; 0.00125; 0.00125; 0.0024 ]; x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio('InitPort', x0); p = setCosts(p, bc, sc); disp(p.NumAssets) disp(p.BuyCost) disp(p.SellCost) disp(p.InitPort) ```
``` 5 0.0013 0.0013 0.0013 0.0013 0.0013 0.0013 0.0070 0.0013 0.0013 0.0024 0.4000 0.2000 0.2000 0.1000 0.1000```

You can also set up the initial portfolio's `InitPort` value as an optional argument to `setCosts` so that the following is an equivalent way to set up transaction costs:

```bc = [ 0.00125; 0.00125; 0.00125; 0.00125; 0.00125 ]; sc = [ 0.00125; 0.007; 0.00125; 0.00125; 0.0024 ]; x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio; p = setCosts(p, bc, sc, x0); disp(p.NumAssets) disp(p.BuyCost) disp(p.SellCost) disp(p.InitPort) ```
``` 5 0.0013 0.0013 0.0013 0.0013 0.0013 0.0013 0.0070 0.0013 0.0013 0.0024 0.4000 0.2000 0.2000 0.1000 0.1000```

For an example of setting costs, see Portfolio Analysis with Turnover Constraints.

### Setting Transaction Costs with Scalar Expansion

Both the `Portfolio` object and the `setCosts` function implement scalar expansion on the arguments for transaction costs and the initial portfolio. If the `NumAssets` property is already set in the `Portfolio` object, scalar arguments for these properties are expanded to have the same value across all dimensions. In addition, `setCosts` lets you specify `NumAssets` as an optional final argument. For example, assume that you have an initial portfolio `x0` and you want to set common transaction costs on all assets in your universe. You can set these costs in any of these equivalent ways:

```x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio('InitPort', x0, 'BuyCost', 0.002, 'SellCost', 0.002);```

or

```x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio('InitPort', x0); p = setCosts(p, 0.002, 0.002); ```

or

```x0 = [ 0.4; 0.2; 0.2; 0.1; 0.1 ]; p = Portfolio; p = setCosts(p, 0.002, 0.002, x0); ```

To clear costs from your `Portfolio` object, use either the `Portfolio` object or `setCosts` with empty inputs for the properties to be cleared. For example, you can clear sales costs from the `Portfolio` object `p` in the previous example:

`p = Portfolio(p, 'SellCost', []);`