"""Separate investment returns from external cash flows.

The portfolio is valued immediately before each external flow. Link subperiod returns; do not treat a contribution as profit.
All inputs are synthetic teaching examples. Run: python 44_time-weighted-cashflows.py
"""
from tv_math import *

opening=1000
pre_contribution_value=1100
contribution=500
second_opening=pre_contribution_value+contribution
ending=1520
first=pre_contribution_value/opening-1
second=ending/second_opening-1
time_weighted=(1+first)*(1+second)-1
assert abs(time_weighted-.045)<1e-12
naive=ending/opening-1
assert abs(naive-.52)<1e-12
print({"linked_return":time_weighted,"misleading_unadjusted_change":naive})
