Added README, code and plots

This commit is contained in:
Nuno Sempere 2020-09-24 12:39:48 +02:00
parent b9e7799fea
commit 9cd14d5dc7
63 changed files with 1727 additions and 0 deletions

44
README.md Normal file
View File

@ -0,0 +1,44 @@
## README
## About
This is some R code for the paper [Labor, Capital, and the Optimal Growth ofSocial Movements](https://nunosempere.github.io/ea/MovementBuildingForUtilityMaximizers.pdf)
## Structure of the code.
1. `Variables.R`: Contains variables
2. `Transition dynamics.R`: If we know our system at time t, generate an approximation of our system at time t+stepsize.
3. `Forward Shooting.R`: Carries out the forward shooting.
4. `ReverseShooting.R`: Nonfunctional. Ignore.
5. `Plotting.R`: Generates graphs of the results
## How to run
Open files 1.,2.,3., and 5. in an IDE for R, like RStudio. Run them in order. For `Plotting.R`, add or create the directory in which you want the graphs to be generated, making sure it has suitable permissions.
## Gotchas
Using a very small stepsize runs into floating point errors. Consider a stylized example:
```r
options(digits=22)
dx <- 10^43
numsteps <- 10^7
stepsize <- 10^(-3)
## Example 1
x <- pi*1e+60
print(x)
for(i in c(1:numsteps)){
x <- x+dx*stepsize
}
x == pi*1e+60
print(x)
## Example 2
x <- pi*1e+60 + numsteps*stepsize*dx
x == pi*1e+60
print(x)
```
The two examples should give the same results, but don't.

63
code/ForwardShooting.R Normal file
View File

@ -0,0 +1,63 @@
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r1_stepsize) + dx1(t)*stepsize
x3_t = x3_t*(1+r3_stepsize) + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
fin = Sys.time()
fin-comienzo
## Checking conditions
options(digits=22)
l = length(times_forward_shooting)
x1_array_forward_shooting[l]
x1_growth = (x1_array_forward_shooting[l]-x1_array_forward_shooting[l-1])/x1_array_forward_shooting[l-1]/(1*stepsize)
x1_growth
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100):l])

442
code/Plotting.R Normal file
View File

@ -0,0 +1,442 @@
# Plotting
## install.packages("tidyverse") <- Not totally necessary.
## install.packages("ggplot2")
## install.packages("ggsci")
library(ggplot2)
library(ggsci)
## General variables
saveplots=TRUE
times = times_forward_shooting
## Also for reverse shooting plots; time in reverse shooting is inverted
shootingtype="reverse"#"forward"#
shootingtype="forward"#"reverse"#
# directory = "/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/temp"
workingdir = "/home/<user>/Documents/<path>" # Write your own
# directory = paste(workingdir, shootingtype, "shooting", "/", last, "years", sep="")
# directory
setwd(workingdir)
# setwd(directory)
if(shootingtype=="forward"){
x1_array_plotting <- x1_array_forward_shooting
x3_array_plotting <- x3_array_forward_shooting
a1_array_plotting <- a1_array_forward_shooting
a3_array_plotting <- a3_array_forward_shooting
s1_array_plotting <- s1_array_forward_shooting
s3_array_plotting <- s3_array_forward_shooting
} else if(shootingtype=="reverse"){
x1_array_plotting <- x1_array_reverse_shooting
x3_array_plotting <- x3_array_reverse_shooting
a1_array_plotting <- a1_array_reverse_shooting
a3_array_plotting <- a3_array_reverse_shooting
s1_array_plotting <- s1_array_reverse_shooting
s3_array_plotting <- s3_array_reverse_shooting
}
height = 5
width = floor(height*(1+sqrt(5))/2)
imagenumbercounter = 1
saveplot = function(imagename){
if(saveplots){
ggsave(paste(imagenumbercounter,"_", imagename, "_",shootingtype, "shooting",".png", sep =""), units="in", width=width, height=height)
imagenumbercounter <<- imagenumbercounter+1
## https://stackoverflow.com/questions/1236620/global-variables-in-r
}
}
options(digits=1) ## Just for display
## x1 and x3
xs <- list()
xs$values <- c(x1_array_plotting, x3_array_plotting)
xs$var <- c(rep("x1", length(x1_array_plotting)), rep("x3", length(x3_array_plotting)))
xs$times = rep(times,2)
xs <- as.data.frame(xs)
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+ labs(
title=title_text,
x="Year since start",
y="Capital and labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(breaks = seq(min(x1_array_plotting), max(x1_array_plotting), length.out=5)) ## Necessary for displaying very large numbers
)
saveplot("StateVariablesX1X3")
## x1 and x3: log plot
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Capital and labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("StateVariablesX1X3logplot")
## Only x3
equis3 <- list()
equis3$values <- c(x3_array_plotting)
equis3$times = times
equis3 <- as.data.frame(equis3)
title_text = "Evolution of movement size"
(ggplot(data = equis3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
)
saveplot("StateVariableX3")
## s1, s3, s4
sigmas <- list()
s4_array_plotting=1-s1_array_plotting-s3_array_plotting
sigmas$values <- c(s1_array_plotting, s3_array_plotting,s4_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)),rep("s4", length(s3_array_plotting)))
sigmas$times = rep(times,3)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Fraction of total labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3", "s4"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementFractions")
## s1 and s3
sigmas <- list()
sigmas$values <- c(s1_array_plotting, s3_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)))
sigmas$times = rep(times,2)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
subtitle_text = "(only direct work and movement building)"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle=subtitle_text,
x="Year since start",
y="Fraction of total labor"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementFractionsS1S3")
## Just direct work.
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of total labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Fraction of total labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
)
saveplot("MovementFractionsS1")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of total labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Fraction of labor"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
)
saveplot("MovementFractionsS3")
## Just direct work - logplot
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of labor"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS1logplot")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of total labor"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.box="vertical"
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS3logplot")
## sigma_1*x3 and sigma3*x3, sigma_4*x3,
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting, s4_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)), rep("s4x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,3)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of labor\n in absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Absolute number\nof movement participants"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3", "s4x3"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementParticipantNumbersS1S3S4")
## sigma_1*x3 and sigma3*x3
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementParticipantNumbersS1S3")
## sigma_1*x3 and sigma3*x3 logplot
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms \n(logarithmic scale)"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementParticipantNumbersS1S3logplot")
## alpha1 and alpha3
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
x="Year since start",
y="Spending"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
)
saveplot("SpendingA1A3")
## alpha1 and alpha3: log plot
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Spending"
)
+theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("SpendingA1A3logplot")

63
code/ReverseShooting.R Normal file
View File

@ -0,0 +1,63 @@
# Reverse shooting
options(digits=22)
## Evolution
x1_array_reverse_shooting <- c()
x3_array_reverse_shooting <- c()
a1_array_reverse_shooting <- c()
a3_array_reverse_shooting <- c()
s1_array_reverse_shooting <- c()
s3_array_reverse_shooting <- c()
l = length(a1_array_forward_shooting)
x1_t = x1_array_forward_shooting[l]
x3_t = x3_array_forward_shooting[l]
comienzo = Sys.time()
max = max(times_reverse_shooting)
t=times_reverse_shooting[1]
for(t in times_reverse_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=3)){
print(paste(floor(100*t/max), "%", sep=""))
#print("\n")
}
t_previous = t-stepsize
a1_t = a1(t_previous)
a3_t = a3(t_previous)
s1_t = s1(t_previous)
s3_t = s3(t_previous)
guessmin = 0
guessmax = x3_t
x3_target = x3_t
x3_t = dx3_reverse_shooting(t, guessmin, guessmax, x3_target, t_previous)
# x3_t = x3_t - dx3(t-stepsize)*stepsize #
x1_t = (x1_t - dx1(t-stepsize)*stepsize)/(1+r1_stepsize)
## But then I can calulate s1, s3 again, and do another iteration.
# for(i in c(1:10)){
# # using the new x3_t
# s3_t = s3(t_previous)
# x3_t = dx3_reverse_shooting(t, guessmin, guessmax, x3_target)
# x3_t
# }
# s1_t = s1(t_previous)
# x1_t = (x1_t - dx1(t-stepsize)*stepsize)/(1+r_stepsize)
a1_array_reverse_shooting <- c(a1_t, a1_array_reverse_shooting)
a3_array_reverse_shooting <- c(a3_t, a3_array_reverse_shooting)
s1_array_reverse_shooting <- c(s1_t, s1_array_reverse_shooting)
s3_array_reverse_shooting <- c(s3_t, s3_array_reverse_shooting)
x1_array_reverse_shooting <- c(x1_t, x1_array_reverse_shooting)
x3_array_reverse_shooting <- c(x3_t, x3_array_reverse_shooting)
}
fin = Sys.time()
fin-comienzo
options(digits=7)
x1_array_reverse_shooting[1] ## 10^11
x3_array_reverse_shooting[1]
# View(x1_array_reverse_shooting)

55
code/TransitionDynamics.R Normal file
View File

@ -0,0 +1,55 @@
# Transition dynamics
# install.packages("pracma")
library(pracma)
## For forward shooting
a1 <- function(t){
term_a1 = λ_1 *
((1- λ_1)/( λ_1 * w_3 * exp(γ_1 * t)))^((1-λ_1) * (1-η)) /
(k1_forward_shooting * exp((ρ-r_1) * t))
result_a1 = term_a1^(1/η)
return(result_a1)
}
a3 <- function(t){
first_term_a3 = w_3 * exp(γ_1 * t) * δ_3 * λ_3 *
β_3 * exp(γ_3 * t) / (r_1 - γ_1 - r_3)
second_term_a3 = (1-λ_3)/(λ_3 * w_3 * exp(γ_1 * t))
subresult_a3 = first_term_a3 * (second_term_a3^(δ_3*(1-λ_3)))
result_a3 = subresult_a3^(1/(1-δ_3))
return(result_a3)
}
s1 <- function(t){
result_s1 = ((1-λ_1)/ λ_1) * (a1_t / (x3_t * w_3 * exp(γ_1 * t)))
return(result_s1)
}
s3 <- function(t){
result_s3 = ((1-λ_3)/ λ_3) * (a3_t / (x3_t * w_3 * exp(γ_1 * t)))
return(result_s3)
}
dx1 <- function(t){
result_x1 = - a1_t - a3_t + x3_t * w_3 * exp(γ_1 * t) * (1-s1_t - s3_t)
return(result_x1)
}
dx3 <- function(t){
result_x3 = β_3 * exp(γ_3 * t) * (a3(t)^λ_3 * (s3_t*x3_t)^(1-λ_3))^δ_3
return(result_x3)
}
dx3_reverse_shooting <- function(t, guessmin, guessmax, x3_target, t_previous){
## Inverting dx3 turns out to be a little bit involved.
check_hypothesis <- function(x3_hypothesis){
dx3_hypothesis = β_3 * exp(γ_3 * t_previous) * (a3(t_previous)^λ_3 * (s3_t*x3_hypothesis)^(1-λ_3))^δ_3
difference = x3_target - (x3_hypothesis + dx3_hypothesis*stepsize)
return(difference)
}
x3_best_guess <- bisect(check_hypothesis, guessmin, guessmax)$root
#uniroot(check_hypothesis, lower=guessmin, upper=guessmax, tol = 10^(-22))[[1]]
# x3_best_guess <- uniroot(check_hypothesis, lower=(x3_target-2), upper=(x3_target), tol = 10^(-22), extendInt = "down")[[1]]
## x3_best_guess <- uniroot(check_hypothesis, c(0, x3_target), tol = 10^(-22), a = 0, extendInt = "upX")[[1]]
return(x3_best_guess)
}

36
code/Variables.R Normal file
View File

@ -0,0 +1,36 @@
# Variables
η = 0.9 ## 1.1
ρ = 0.005
r_1 = 0.06
r_3 = -0.02
γ_1 = 0.03
γ_3 = 0.01
w_3 = 5000 ## Mean on the EA Survey 2018: ~7000
β_3 = 1 ## 1 ## 0.5 Corresponds to roughly 5 people convincing 10 other people per year on a 50k year budget
## Dramatic if beta = 0.5, w_3 = 1000
λ_1 = 0.5
λ_3 = 0.5
δ_3 = 0.44
## k1_forward_shooting
k1_forward_shooting = 3*10^(-7)
## 1*10^(-7) fails. 2*10^(-7) fails
## Artfully guessed.
k1_reverse_shooting = k1_forward_shooting
stepsize = 0.1
## stepsize = 0.1 => seconds (7s).
## stepsize = 0.01 => minutes (3 mins).
r1_stepsize = ((1+r_1)^stepsize)-1
r3_stepsize = ((1+r_3)^stepsize)-1
first = 0
last = 1000
times_forward_shooting = seq(from=first, to=last, by=stepsize)
times_reverse_shooting = seq(from=last, to=first, by=-stepsize)
### Initial conditions. Correspond to "year 0".
x1_init = 10^10
x3_init = 10^5

512
plots/10000years/.Rhistory Normal file
View File

@ -0,0 +1,512 @@
k1_reverse_shooting = k1_forward_shooting
## Artfully guessed, such that s1 < 1
stepsize = 0.1
## stepsize = 0.1 => seconds (7s).
## stepsize = 0.01 => minutes (3 mins).
r_stepsize = ((1+r_1)^stepsize)-1
first = 0
last = 10000
times_forward_shooting = seq(from=first, to=last, by=stepsize)
times_reverse_shooting = seq(from=last, to=first, by=-stepsize)
### Initial conditions. Correspond to "year 0".
x1_init = 10^10 ## 1 billion? 15 billion? 100 billion? Too much?
x3_init = 10^5
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r_stepsize) + dx1(t)*stepsize
x3_t = x3_t + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
fin = Sys.time()
fin-comienzo
## Checking condition
options(digits=22)
l = length(times_forward_shooting)
x1_array_forward_shooting[l]
x1_growth = (x1_array_forward_shooting[l]-x1_array_forward_shooting[l-1])/x1_array_forward_shooting[l-1]/(1*stepsize)
x1_growth
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100):l])
# Plotting
## install.packages("tidyverse") <- Not totally necessary.
## install.packages("ggplot2")
## install.packages("ggsci")
library(ggplot2)
library(ggsci)
## General variables
saveplots=TRUE
times = times_forward_shooting
## Also for reverse shooting plots; time in reverse shooting is inverted
shootingtype="reverse"#"forward"#
shootingtype="forward"#"reverse"#
# directory = "/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/temp"
directory = paste("/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/satisfactory3/", shootingtype, "shooting", "/", last, "years", sep="")
directory
setwd(directory)
if(shootingtype=="forward"){
x1_array_plotting <- x1_array_forward_shooting
x3_array_plotting <- x3_array_forward_shooting
a1_array_plotting <- a1_array_forward_shooting
a3_array_plotting <- a3_array_forward_shooting
s1_array_plotting <- s1_array_forward_shooting
s3_array_plotting <- s3_array_forward_shooting
} else if(shootingtype=="reverse"){
x1_array_plotting <- x1_array_reverse_shooting
x3_array_plotting <- x3_array_reverse_shooting
a1_array_plotting <- a1_array_reverse_shooting
a3_array_plotting <- a3_array_reverse_shooting
s1_array_plotting <- s1_array_reverse_shooting
s3_array_plotting <- s3_array_reverse_shooting
}
height = 5
width = floor(height*(1+sqrt(5))/2)
imagenumbercounter = 1
saveplot = function(imagename){
if(saveplots){
ggsave(paste(imagenumbercounter,"_", imagename, "_",shootingtype, "shooting",".png", sep =""), units="in", width=width, height=height)
imagenumbercounter <<- imagenumbercounter+1
## https://stackoverflow.com/questions/1236620/global-variables-in-r
}
}
options(digits=1) ## Just for display
## x1 and x3
xs <- list()
xs$values <- c(x1_array_plotting, x3_array_plotting)
xs$var <- c(rep("x1", length(x1_array_plotting)), rep("x3", length(x3_array_plotting)))
xs$times = rep(times,2)
xs <- as.data.frame(xs)
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+ labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(breaks = seq(min(x1_array_plotting), max(x1_array_plotting), length.out=5))
)
saveplot("StateVariablesX1X3")
## x1 and x3: log plot
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("StateVariablesX1X3logplot")
## Only x3
equis3 <- list()
equis3$values <- c(x3_array_plotting)
equis3$times = times
equis3 <- as.data.frame(equis3)
title_text = "Evolution of movement size"
(ggplot(data = equis3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("StateVariableX3")
## s1, s3, s4
sigmas <- list()
s4_array_plotting=1-s1_array_plotting-s3_array_plotting
sigmas$values <- c(s1_array_plotting, s3_array_plotting,s4_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)),rep("s4", length(s3_array_plotting)))
sigmas$times = rep(times,3)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3", "s4"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementFractions")
## s1 and s3
sigmas <- list()
sigmas$values <- c(s1_array_plotting, s3_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)))
sigmas$times = rep(times,2)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
subtitle_text = "(only direct work and movement building)"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle=subtitle_text,
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementFractionsS1S3")
## Just direct work.
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of total labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("MovementFractionsS1")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of total labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("MovementFractionsS3")
## Just direct work - logplot
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS1logplot")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS3logplot")
## sigma_1*x3 and sigma3*x3, sigma_4*x3,
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting, s4_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)), rep("s4x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,3)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of labor\n in absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3", "s4x3"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementParticipantNumbersS1S3S4")
## sigma_1*x3 and sigma3*x3
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementParticipantNumbersS1S3")
## sigma_1*x3 and sigma3*x3 logplot
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms \n(logarithmic scale)"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementParticipantNumbersS1S3logplot")
## alpha1 and alpha3
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Spending"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
)
saveplot("SpendingA1A3")
## alpha1 and alpha3: log plot
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Spending"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("SpendingA1A3logplot")

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

512
plots/1000years/.Rhistory Normal file
View File

@ -0,0 +1,512 @@
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100):l])
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r1_stepsize) + dx1(t)*stepsize
x3_t = x3_t*(1+r3_stepsize) + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
# Variables
η = 0.9 ## 1.1
ρ = 0.005
r_1 = 0.06
r_3 = -0.05
γ_1 = 0.03
γ_3 = 0.01
w_3 = 5000 ## Mean on the EA Survey 2018: ~7000
β_3 = 1 ## 1 ## 0.5 Corresponds to roughly 5 people convincing 10 other people per year on a 50k year budget
## Dramatic if beta = 0.5, w_3 = 1000
λ_1 = 0.5
λ_3 = 0.5
δ_3 = 0.44
## k1_forward_shooting
k1_forward_shooting = 3*10^(-7) ## 1*10^(-7) fails. 2*10^(-7) fails
k1_reverse_shooting = k1_forward_shooting
## Artfully guessed, such that s1 < 1
stepsize = 0.1
## stepsize = 0.1 => seconds (7s).
## stepsize = 0.01 => minutes (3 mins).
r1_stepsize = ((1+r_1)^stepsize)-1
r3_stepsize = ((1+r_3)^stepsize)-1
first = 0
last = 1000
times_forward_shooting = seq(from=first, to=last, by=stepsize)
times_reverse_shooting = seq(from=last, to=first, by=-stepsize)
### Initial conditions. Correspond to "year 0".
x1_init = 10^10 ## 1 billion? 15 billion? 100 billion? Too much?
x3_init = 10^5
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r1_stepsize) + dx1(t)*stepsize
x3_t = x3_t*(1+r3_stepsize) + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
fin = Sys.time()
fin-comienzo
## Checking condition
options(digits=22)
l = length(times_forward_shooting)
x1_array_forward_shooting[l]
x1_growth = (x1_array_forward_shooting[l]-x1_array_forward_shooting[l-1])/x1_array_forward_shooting[l-1]/(1*stepsize)
x1_growth
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100):l])
# Plotting
## install.packages("tidyverse") <- Not totally necessary.
## install.packages("ggplot2")
## install.packages("ggsci")
library(ggplot2)
library(ggsci)
## General variables
saveplots=TRUE
times = times_forward_shooting
## Also for reverse shooting plots; time in reverse shooting is inverted
shootingtype="reverse"#"forward"#
shootingtype="forward"#"reverse"#
directory = paste("/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/satisfactory3/", shootingtype, "shooting", "/", last, "years", sep="")
directory
setwd(directory)
if(shootingtype=="forward"){
x1_array_plotting <- x1_array_forward_shooting
x3_array_plotting <- x3_array_forward_shooting
a1_array_plotting <- a1_array_forward_shooting
a3_array_plotting <- a3_array_forward_shooting
s1_array_plotting <- s1_array_forward_shooting
s3_array_plotting <- s3_array_forward_shooting
} else if(shootingtype=="reverse"){
x1_array_plotting <- x1_array_reverse_shooting
x3_array_plotting <- x3_array_reverse_shooting
a1_array_plotting <- a1_array_reverse_shooting
a3_array_plotting <- a3_array_reverse_shooting
s1_array_plotting <- s1_array_reverse_shooting
s3_array_plotting <- s3_array_reverse_shooting
}
height = 5
width = floor(height*(1+sqrt(5))/2)
imagenumbercounter = 1
saveplot = function(imagename){
if(saveplots){
ggsave(paste(imagenumbercounter,"_", imagename, "_",shootingtype, "shooting",".png", sep =""), units="in", width=width, height=height)
imagenumbercounter <<- imagenumbercounter+1
## https://stackoverflow.com/questions/1236620/global-variables-in-r
}
}
options(digits=1) ## Just for display
xs <- list()
xs$values <- c(x1_array_plotting, x3_array_plotting)
xs$var <- c(rep("x1", length(x1_array_plotting)), rep("x3", length(x3_array_plotting)))
xs$times = rep(times,2)
xs <- as.data.frame(xs)
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+ labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(breaks = seq(min(x1_array_plotting), max(x1_array_plotting), length.out=5))
)
## x1 and x3: log plot
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("StateVariablesX1X3logplot")
## Only x3
equis3 <- list()
equis3$values <- c(x3_array_plotting)
equis3$times = times
equis3 <- as.data.frame(equis3)
title_text = "Evolution of movement size"
(ggplot(data = equis3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("StateVariableX3")
## s1, s3, s4
sigmas <- list()
s4_array_plotting=1-s1_array_plotting-s3_array_plotting
sigmas$values <- c(s1_array_plotting, s3_array_plotting,s4_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)),rep("s4", length(s3_array_plotting)))
sigmas$times = rep(times,3)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3", "s4"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
# Variables
η = 0.9 ## 1.1
ρ = 0.005
r_1 = 0.06
r_3 = -0.02
γ_1 = 0.03
γ_3 = 0.01
w_3 = 5000 ## Mean on the EA Survey 2018: ~7000
β_3 = 1 ## 1 ## 0.5 Corresponds to roughly 5 people convincing 10 other people per year on a 50k year budget
## Dramatic if beta = 0.5, w_3 = 1000
λ_1 = 0.5
λ_3 = 0.5
δ_3 = 0.44
## k1_forward_shooting
k1_forward_shooting = 3*10^(-7) ## 1*10^(-7) fails. 2*10^(-7) fails
k1_reverse_shooting = k1_forward_shooting
## Artfully guessed, such that s1 < 1
stepsize = 0.1
## stepsize = 0.1 => seconds (7s).
## stepsize = 0.01 => minutes (3 mins).
r1_stepsize = ((1+r_1)^stepsize)-1
r3_stepsize = ((1+r_3)^stepsize)-1
first = 0
last = 1000
times_forward_shooting = seq(from=first, to=last, by=stepsize)
times_reverse_shooting = seq(from=last, to=first, by=-stepsize)
### Initial conditions. Correspond to "year 0".
x1_init = 10^10 ## 1 billion? 15 billion? 100 billion? Too much?
x3_init = 10^5
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r1_stepsize) + dx1(t)*stepsize
x3_t = x3_t*(1+r3_stepsize) + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
fin = Sys.time()
fin-comienzo
## Checking condition
options(digits=22)
l = length(times_forward_shooting)
x1_array_forward_shooting[l]
x1_growth = (x1_array_forward_shooting[l]-x1_array_forward_shooting[l-1])/x1_array_forward_shooting[l-1]/(1*stepsize)
x1_growth
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100):l])
# Plotting
## install.packages("tidyverse") <- Not totally necessary.
## install.packages("ggplot2")
## install.packages("ggsci")
library(ggplot2)
library(ggsci)
## General variables
saveplots=TRUE
times = times_forward_shooting
## Also for reverse shooting plots; time in reverse shooting is inverted
shootingtype="reverse"#"forward"#
shootingtype="forward"#"reverse"#
directory = paste("/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/satisfactory3/", shootingtype, "shooting", "/", last, "years", sep="")
directory
setwd(directory)
if(shootingtype=="forward"){
x1_array_plotting <- x1_array_forward_shooting
x3_array_plotting <- x3_array_forward_shooting
a1_array_plotting <- a1_array_forward_shooting
a3_array_plotting <- a3_array_forward_shooting
s1_array_plotting <- s1_array_forward_shooting
s3_array_plotting <- s3_array_forward_shooting
} else if(shootingtype=="reverse"){
x1_array_plotting <- x1_array_reverse_shooting
x3_array_plotting <- x3_array_reverse_shooting
a1_array_plotting <- a1_array_reverse_shooting
a3_array_plotting <- a3_array_reverse_shooting
s1_array_plotting <- s1_array_reverse_shooting
s3_array_plotting <- s3_array_reverse_shooting
}
height = 5
width = floor(height*(1+sqrt(5))/2)
imagenumbercounter = 1
saveplot = function(imagename){
if(saveplots){
ggsave(paste(imagenumbercounter,"_", imagename, "_",shootingtype, "shooting",".png", sep =""), units="in", width=width, height=height)
imagenumbercounter <<- imagenumbercounter+1
## https://stackoverflow.com/questions/1236620/global-variables-in-r
}
}
options(digits=1) ## Just for display
xs <- list()
xs$values <- c(x1_array_plotting, x3_array_plotting)
xs$var <- c(rep("x1", length(x1_array_plotting)), rep("x3", length(x3_array_plotting)))
xs$times = rep(times,2)
xs <- as.data.frame(xs)
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+ labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(breaks = seq(min(x1_array_plotting), max(x1_array_plotting), length.out=5))
)
saveplot("StateVariablesX1X3")
## x1 and x3: log plot
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("StateVariablesX1X3logplot")
## Only x3
equis3 <- list()
equis3$values <- c(x3_array_plotting)
equis3$times = times
equis3 <- as.data.frame(equis3)
title_text = "Evolution of movement size"
(ggplot(data = equis3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("StateVariableX3")
## s1, s3, s4
sigmas <- list()
s4_array_plotting=1-s1_array_plotting-s3_array_plotting
sigmas$values <- c(s1_array_plotting, s3_array_plotting,s4_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)),rep("s4", length(s3_array_plotting)))
sigmas$times = rep(times,3)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3", "s4"),
labels=c("Direct work", "Movement building", "Money-making")
)
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB