Skip to content

Instantly share code, notes, and snippets.

@brendano
Last active August 31, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendano/235acdcc7b6c48eb589e4c33a7a7d4fc to your computer and use it in GitHub Desktop.
Save brendano/235acdcc7b6c48eb589e4c33a7a7d4fc to your computer and use it in GitHub Desktop.
longrun_bettng_inequality
log(c(1.05,.6))
log(c(1.5,.6))
ifelse(runif(1000)>.5, 1.5, .6)
x=ifelse(runif(1000)>.5, 1.5, .6)
mean(x)
prod(x)
x=ifelse(runif(10)>.5, 1.5, .6)
x
y=replicate(100000,{x=ifelse(runif(10)>.5, 1.5, .6); prod(x)})
summary(y)
y=replicate(100000,{x=ifelse(runif(10)>.5, 1.5, .6); prod(x)})
summary(y)
y=replicate(100000,{x=ifelse(runif(1000)>.5, 1.5, .6); prod(x)})
summary(y)
ns=10^seq(1,4,.5)
ns
r=sapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
r
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
lapply(r,summary)
x=r[[1]]
hist(x)
hist(x);hist(log(1+x))
par(mfrow=c(2,1))
hist(x);hist(log(1+x))
hist(x);hist(log(x))
x=r[[2]]
hist(x);hist(log(x))
x=r[[3]]
hist(x);hist(log(x))
x=r[[3]];print(summary(x));hist(x);hist(log(x))
x=r[[4]];print(summary(x));hist(x);hist(log(x))
ns[4]
x=r[[5]];print(summary(x));hist(x);hist(log(x))
ns
par(mfrow=c(2,4))
for(i in 2:5){ x=r[[5]];print(summary(x));hist(x,main=sprintf("mean=%.6e",mean(x));hist(log(x))
for(i in 2:5){ x=r[[5]];print(summary(x));hist(x,main=sprintf("mean=%.6e",mean(x)));hist(log(x))
for(i in 2:5){ x=r[[5]];print(summary(x));hist(x,main=sprintf("mean=%.6e",mean(x)));hist(log(x)) }
par(mfrow=c(2,4))
for(i in 2:5){ x=r[[5]];print(summary(x));hist(x,main=sprintf("mean=%.6e",mean(x)));hist(log(x)) }
?par
?par
par(mfcol=c(2,4))
for(i in 2:5){ x=r[[5]];print(summary(x));hist(x,main=sprintf("mean=%.6e",mean(x)));hist(log(x)) }
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%d mean=%.6e",ns[i],mean(x)));hist(log(x)) }
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%.1e mean=%.6e",ns[i],mean(x)));hist(log(x)) }
par(mfcol=c(2,4))
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%.1e mean=%.6e",ns[i],mean(x)));hist(log(x)) }
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%.1e mean=%.3e",ns[i],mean(x)));hist(log(x),main=sprintf("median logret=%.3e ret=%.3e",median(log(x)),median(x))) }
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%.1e mean=%.3e",ns[i],mean(x)));hist(log(x),main=sprintf("median logret=%.1f ret=%.3e",median(log(x)),median(x))) }
for(i in 2:5){ x=r[[i]];print(summary(x));hist(x,main=sprintf("n=%.1e mean=%.1e",ns[i],mean(x)));hist(log(x),main=sprintf("median logret=%.1f ret=%.1e",median(log(x)),median(x))) }
ns=10^seq(3,4,.1)
r=lapply(ns,function(n){ y=replicate(1000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(n), lapply(r, function(x) median(x)))
plot(log10(ns), lapply(r, function(x) median(x)))
plot(log10(ns), lapply(r, function(x) mean(x)))
ns=10^seq(2,3,.1)
r=lapply(ns,function(n){ y=replicate(1000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(ns), lapply(r, function(x) mean(x)))
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(ns), lapply(r, function(x) mean(x)))
r=lapply(ns,function(n){ y=replicate(100000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(ns), lapply(r, function(x) mean(x)))
ns=2:10
r=lapply(ns,function(n){ y=replicate(1000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(ns), lapply(r, function(x) mean(x)))
plot(log10(ns), lapply(r, function(x) mean(x)))
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
plot(log10(ns), lapply(r, function(x) mean(x)))
r=lapply(ns,function(n){ y=replicate(1000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
ns=10^seq(1,5,.5)
r=lapply(ns,function(n){ y=replicate(1000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
par(mfrow=c(2,4))
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("median logret=%.1f ret=%.1e",median(log(x)),median(x))) }
ns=1:10
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
lapply(r,summary)
1.05^ns
lapply(r,mean)
sapply(r,mean)
1.05^ns
ns=c(10,316,1000)
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
par(mfrow=c(1,3))
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f median logret=%.1f median ret=%.1e",ns[i], median(log(x)),median(x))) }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian logret=%.1f, median ret=%.1e, mean ret=%.1e",ns[i], median(log(x)),median(x))) }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian logret=%.1f, median ret=%.1e, mean ret=%.1e",ns[i], median(log(x)), median(x), mean(x))) }
par(mfrow=c(1,3))
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian logret=%.1f, median ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))) }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))) }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='blue'); abline(v=log(median(x)),col='gray40') }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='blue'); abline(v=log(median(x)),col='gray10') }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='blue'); abline(v=log(median(x)),col='red') }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='black'); abline(v=log(median(x)),col='red') }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='gray50'); abline(v=log(median(x)),col='red') }
for(i in 1:length(r)){ x=r[[i]]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns",ns[i], median(log(x)), median(x), mean(x))); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nWealth sum\nDistribution of log returns",ns[i], median(log(x)), median(x), ms)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
1.05^1000
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns", ns[i], median(log(x)), median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
mx
ns
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns", ns[i], median(x), mean(x))); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian ret=%.1e, mean ret=%.1e\nDistribution of log returns", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes. Red=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=mx,col='red') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=mx,col='red') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; hist(log(x),main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=log(mx),col='red') }
log(1.5e21)
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; xlim=range(c(log(x), log(mx))); hist(log(x),main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=log(mx),col='red') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; xlim=range(c(log(x), log(mx))); hist(log(x),xlim=xlim,main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=log(mx),col='red') }
for(i in 1:length(r)){ x=r[[i]]; mx=1.05^ns[i]; xlim=range(c(log(x), log(mx))); hist(log(x),xlim=xlim,main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)); abline(v=0,col='gray50'); abline(v=log(median(x)),col='blue'); abline(v=log(mx),col='red') }
par(mfrow=c(1,3))
source("stuff.r")
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
ns=c(10,316,1000)
r=lapply(ns,function(n){ y=replicate(10000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
source("stuff.r")
source("stuff.r")
?text
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
r1=r
source("stuff.r")
source("stuff.r")
source("stuff.r")
?axis
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
source("stuff.r")
summary(r[[1]])
mean(r[[1]]>=10)
mean(r[[1]]>=100)
mean(r[[1]]>=10)
mean(r[[2]]<1)
1- 5.2e-3
log(1.5, .6)
log(c(1.5, .6))
source("stuff.r")
1.05^1000
plot in https://twitter.com/brendan642/status/1167808597864132609
Via https://twitter.com/jonathanstray/status/1167217965638406144
From Chapter 1, Fig 5 here:
Ergodicity Economics
Ole Peters and Alexander Adamou
2018, accessed 2019-08-31
https://ergodicityeconomics.files.wordpress.com/2018/06/ergodicity_economics.pdf
=============
This example is amazing. I like just the metaphor for wealth accumulation and inequality.
From: https://ergodicityeconomics.com/lecture-notes/
Via tweet thread https://twitter.com/jonathanstray/status/1167217965638406144
The 50/50 bet on gaining 50% versus losing 40% sounds possibly reasonable by itself. But the more times everyone takes the bet, the proportion of people going broke approaches 100% -- even as an exponentially small fraction of the population gets exponentially richer.
Here's the outcome distrbution for 100k simulated individuals. After 10 trials, the median outcome (-41%) is already quite a bit worse than the mean (+60%), and there are a small number of people with more than a 10x return.
After 100 trials, a large majority (86%) of individuals are worse-off than where they started. The median individual lost 99.5% of their wealth.
By 1000 trials, nearly everyone has lost everything. I didn't even get the super-rich tail in this simulation; they drive the insanely high expected outcome of 1.05^1000 ~ 10^21. Log-normal distributions are crazy.
(The plots show the monte carlo median, but analytically calculated mean)
Mathematically, I guess the situation is that avg return is positive (r=0.5 or -0.4), but avg log return is negative (log(1+r)=0.4 or -0.51). Thus the log wealth distribution shifts more and more to the left over time.
The total *sum* of wealth, though, really is exponentially increasing at rate 1.05^t. If you redistributed wealth at each timestep, or even just once in a while, *everyone* can get exponentially wealthy!
[I think this is the argument for social democracy? Capitalism generates overall wealth albeit with high inequality; redistribute it and it should work for everyone.]
And an intergenerational wealth transfer story? It's the long *sequence* of bets that really drives the extreme case. In this setting, transmitting inherited wealth across many generations should lead to skewed outcomes, even if most of the time the wealth doesn't last.
I think the key stylized fact assumption is that economic life tends to have positive expected returns, but negative expected log-returns. Does this correspond to real-world circumstances, and if so under what conditions?
[If both returns and log-returns have positive expectations, you still get enormous inequality, but at least individuals don't tend lose wealth. Wealth redistribution would still be potentially very powerful.]
Caveat: I don't know any of this academic literature and am probably reinventing a bunch of stuff. The lecture notes have lots more on this topic. https://ergodicityeconomics.com/lecture-notes/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>https://ergodicityeconomics.files.wordpress.com/2018/06/ergodicity_economics.pdf</string>
</dict>
</plist>
Display the source blob
Display the rendered blob
Raw
# ns=c(10,100,1000)
# r=lapply(ns,function(n){ y=replicate(100000,{x=ifelse(runif(n)>.5, 1.5, .6); prod(x)}) })
# names(r)=ns
par(mfrow=c(1,length(ns)))
for(i in 1:length(r)){
x=r[[i]]; mx=1.05^ns[i];
xlim=range(c(log(x), log(mx)));
hiy=30e3
ylim=c(0,hiy+500)
hist(log(x),xlim=xlim,ylim=ylim,
axes=FALSE,xlab="Wealth outcome (log scale). 1=original amount",
main=sprintf("t=%.0f \nDistribution of wealth outcomes", ns[i], median(x), mx)
# main=sprintf("n=%.0f \nmedian wealth=%.1e, mean=%.1e\nDistribution of log wealth outcomes.\nRed=median, Blue=mean", ns[i], median(x), mx)
)
axis(2)
n=ns[i]
tickbase = if(n==10) -3:3
else if(n==100) c(-10,-5,0,5,10)
else if(n==1000) seq(-100,100, by=20)
xat = log(10^tickbase)
ticklabels = ifelse( tickbase>=0 & tickbase<=3,
sprintf("%d",round(10^tickbase)),
ifelse(-3 <= tickbase & tickbase < 0,
sprintf("1/%d", round(10^abs(tickbase))),
sprintf("10^%d",tickbase)))
axis(1,at=xat,labels=ticklabels)
text(log(median(x)),hiy,labels=sprintf("Median\n%.1e", median(x)), col='blue', adj=c(1.1,1))
text(log(mx),hiy,labels=sprintf("Mean\n%.1e", mx), col='red',
adj=c(if(ns[i]<1000) -0.1 else 1.1, 1))
abline(v=0,col='gray50')
abline(v=log(median(x)),col='blue')
abline(v=log(mx),col='red')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment