Last active
February 17, 2021 16:56
-
-
Save 3gx/27181adc9086d82c7c9dd3cf55d0462d to your computer and use it in GitHub Desktop.
portoflio model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from math import log, exp | |
| def get_f(fmax): | |
| f = 1+random.randrange(-fmax*100//10,2*fmax*100)/100.0/100.0 | |
| f = 1+random.gauss(fmax,fmax/2)/100 | |
| f = 1 + fmax/100 | |
| assert f > 0 | |
| return f | |
| def usmodel(fmax,n,tax): | |
| xbefore=1 | |
| xafter=1 | |
| for i in range(0,n): | |
| f = get_f(fmax) | |
| xbefore *= f # just compounded growth for n-years | |
| xafter *= f # tax on profit after this compounded growth | |
| xafter = 1 + (xafter-1)*(1-tax) # tax amount of final profits | |
| tax=1-(xafter-1)/(xbefore-1) # effective tax | |
| cagr = exp(log(xbefore)/n) | |
| return xbefore, xafter, 1-xafter/xbefore, tax, cagr | |
| def nlmodel(fmax, n,tax): | |
| xbefore=1 | |
| xafter=1 | |
| for i in range(0,n): | |
| f = get_f(fmax) | |
| xbefore *= f # just compounded growth for n-years | |
| xafter*= f*(1-tax/2) - tax/2 # wealth tax applied on yearly basis at the end of the year | |
| tax=1-(xafter-1)/(xbefore-1) # effective tax | |
| cagr = exp(log(xbefore)/n) | |
| return xbefore, xafter, 1-xafter/xbefore, tax, cagr | |
| def usmodel_rebalance(fmax,n,tax,nr,ar): | |
| assert n%nr == 0 # sanity check, keep things simple | |
| xbefore=1 | |
| xafter=1 | |
| for i in range(0,n,nr): | |
| f = get_f(fmax) | |
| xbefore *= f**nr # grow for nr years | |
| xafter *= (f**nr-1)*ar*(1-tax) + (f**nr-1)*(1-ar) + 1 # tax ar amount of profits that need to be rebalanced | |
| xafter /= (f**nr-1)*ar*(1-tax) + (f**nr-1)*(1-ar) + 1 # undo final rebalancing | |
| xafter *= f**nr # just grow for the final year | |
| xafter = 1 + (xafter-1)*(1-tax) # tax amount of final profits | |
| tax=1-(xafter-1)/(xbefore-1) # effective tax | |
| cagr = exp(log(xbefore)/n) | |
| return xbefore, xafter, 1-xafter/xbefore, tax, cagr | |
| flist = [3,5,7,10,15,20] # CAGR rates | |
| nyears = [2,4,6,10,20,30,40,50] # years to invest | |
| tax=0.3 # US federal + state capital gain taxes, ca is 20+13.3 = 33.3, other states can be lower | |
| print(f"\n\nUS Model, tax={tax}") | |
| for f in flist: | |
| print(f"\nf={f}") | |
| for n in nyears: | |
| print(f"{n}yr: {usmodel(f,n,tax)}") | |
| nr=1 # rebalance every year | |
| ar=0.3 # profits imbalance that needs to be sold and reinvested | |
| print(f"\n\nUS Model with rebalance, tax={tax} nr={nr} ar={ar}") | |
| for f in flist: | |
| print(f"\nf={f}") | |
| for n in nyears: | |
| print(f"{n}yr: {usmodel_rebalance(f,n,tax,nr,ar)}") | |
| tax=0.04*0.3 # NL wealth tax, 30% assuming 4% growth of wealth | |
| print(f"\n\nNL Model, tax={tax}") | |
| for f in flist: | |
| print(f"\nf={f}") | |
| for n in nyears: | |
| print(f"{n}yr: {nlmodel(f,n,tax)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.