Why do portfolio weights change without trading?

Buy two stocks at target weights, hold the shares, compute each day’s weights in a long pandas panel, and compare buy and hold with a portfolio rebalanced every day.

After this lesson, you can compute the weights of a portfolio whose shares you hold, and compare its return with a portfolio rebalanced every day.

Why do the weights change?

A stock’s weight is its value divided by the portfolio’s value, and both change with the prices. When AAA rises 20% and BBB stays flat, AAA’s weight grows from 0.500 to 0.545 without any trade.

\[ w_{i,t} = \frac{n_i P_{i,t}}{\sum_j n_j P_{j,t}} \]

Here \(n_i\) is the number of shares of stock \(i\), \(P_{i,t}\) its price on day \(t\), and the sum runs over the stocks in the portfolio.

The example is a long panel, as in the lesson on long and wide panels, of two made-up stocks over three trading days. I invest 1,000 dollars at the first close, half in AAA at 10 dollars a share and half in BBB at 20 dollars a share, so I buy 50 and 25 shares.

position_value computes each row’s value in dollars. .transform("first") puts each stock’s first price on all its rows, .map(target) puts each stock’s target weight on its rows, and dividing the target dollars by the first price gives the shares. .transform("sum") puts each day’s portfolio value on that day’s rows, so dividing gives the weights.

The following box builds the panel and computes the values and weights.

import numpy as np
import pandas as pd

dates = pd.bdate_range("2026-03-02", periods=3)

panel = pd.DataFrame({
    "date": np.tile(dates, 2),
    "ticker": np.repeat(["AAA", "BBB"], 3),
    "price": [10.0, 12.0, 12.0, 20.0, 20.0, 25.0],
})
target = {"AAA": 0.5, "BBB": 0.5}

def position_value(panel, target, capital=1000):
    first_price = panel.groupby("ticker")["price"].transform("first")
    shares = panel["ticker"].map(target) * capital / first_price
    return shares * panel["price"]

panel["value"] = position_value(panel, target)
panel["weight"] = panel["value"] / panel.groupby("date")["value"].transform("sum")
print(panel.round(3))
        date ticker  price  value  weight
0 2026-03-02    AAA   10.0  500.0   0.500
1 2026-03-03    AAA   12.0  600.0   0.545
2 2026-03-04    AAA   12.0  600.0   0.490
3 2026-03-02    BBB   20.0  500.0   0.500
4 2026-03-03    BBB   20.0  500.0   0.455
5 2026-03-04    BBB   25.0  625.0   0.510

On 4 March, BBB rises 25% while AAA stays flat, so the weights become 0.490 and 0.510. A stock gains weight whenever its return beats the portfolio’s return.

What changes if I rebalance every day?

A portfolio rebalanced every day trades back to the targets at each close, so every day’s return uses the weights 0.5 and 0.5. Buy and hold earns 22.5% over the two days, and daily rebalancing earns 23.75%.

As in the lesson on percentage changes, .pct_change() gives each stock’s daily return. Multiplying by the targets and adding within each date gives the rebalanced portfolio’s daily return, and (1 + daily).prod() - 1 compounds it. Buy and hold’s return is the last portfolio value divided by the 1,000 dollars invested, minus 1.

The following box computes both returns.

panel["stock_return"] = panel.groupby("ticker")["price"].pct_change()
daily = (panel["ticker"].map(target) * panel["stock_return"]).groupby(panel["date"]).sum(min_count=1)
rebalanced = (1 + daily).prod() - 1

portfolio_value = panel.groupby("date")["value"].sum()
buy_and_hold = portfolio_value.iloc[-1] / 1000 - 1
print(round(buy_and_hold, 4), round(rebalanced, 4))
0.225 0.2375

Rebalancing sold AAA after its rise and bought BBB before BBB’s 25% gain, so it earned more here. With other prices it can earn less.

Your turn

Start with the targets 0.75 for AAA and 0.25 for BBB and print the weights. You should see AAA at 0.742 and BBB at 0.258 on 4 March.

position_value takes the new targets as its second argument.

value_75 = position_value(panel, {"AAA": 0.75, "BBB": 0.25})
panel["weight_75"] = value_75 / value_75.groupby(panel["date"]).transform("sum")
print(panel[["date", "ticker", "price", "weight_75"]].round(3))
        date ticker  price  weight_75
0 2026-03-02    AAA   10.0      0.750
1 2026-03-03    AAA   12.0      0.783
2 2026-03-04    AAA   12.0      0.742
3 2026-03-02    BBB   20.0      0.250
4 2026-03-03    BBB   20.0      0.217
5 2026-03-04    BBB   25.0      0.258