##############################################################################
# We simulate populations over time with different starting conditions, and
# observe how the total population changes. Later, immigration is included in
# the model.
# ----------------------------------------------------------------------------
# This material was developed as part of the course EE103 taught at Stanford
# University by Professor Stephen Boyd. For details, see the course
# website at .
##############################################################################
using Gadfly;
using MMA;
include("pop_dyn_data.jl");
# A moving average smoothing function
function smooth(v,window)
new_v = float(v);
half_window = int((window - 1)/2);
for i in 1:length(v)
min_below = i-1; max_above = length(v)-i;
curr_window = min(min(min_below,max_above),half_window);
new_v[i] = sum(v[i-curr_window:i+curr_window])/(2*curr_window+1);
end
return new_v;
end;
# Begin by plotting given data
# Population distribution
new_figure(xlabel="Age",ylabel="Number of people");
line_plot(p2010);
save_figure("pop2010.eps",5inch,3inch,format="ps");
# Birth rate
new_figure(xlabel="Age",ylabel="Births per person");
line_plot(b);
save_figure("birth_rates.eps",5inch,3inch,format="ps");
# Death rate
new_figure(xlabel="Age",ylabel="Deaths per person");
line_plot(d[1:end-1]);
save_figure("death_rates.eps",5inch,3inch,format="ps");
# Immigration rate
new_figure(xlabel="Age",ylabel="Number of immigrants");
line_plot(u);
save_figure("immigration.eps",5inch,3inch,format="ps");
# Form A as the state transition matrix
# b and d are the birth and death rates respectively
A = b';
A = [A; diagm(1-d)];
A = A[1:end-1,:];
# Simulate population without immigration
T = 20;
p = p2010
total_pop = sum(p);
populations = p;
for i in 1:T
p = A*p;
total_pop = [total_pop; sum(p)];
populations = [populations p];
end
# Total population (no immigration)
new_figure(xlabel="Years after 2010",ylabel="Number of people");
line_plot(total_pop);
save_figure("total_pop_noim.eps",5inch,3inch,format="ps");
# 5-year projection (no immigration)
new_figure(xlabel="Age",ylabel="Number of people");
line_plot(populations[:,1],color="blue");
line_plot(populations[:,5],color="red");
save_figure("pop_and_proj_noim.eps",5inch,3inch,format="ps");
# Simulate with immigration
p = p2010;
total_pop_im = sum(p);
populations_im = p;
for i=1:T
p = A * p + u;
total_pop_im = [total_pop_im; sum(p)];
populations_im = [populations_im p];
end
# Total population (no immigration)
new_figure(xlabel="Years after 2010",ylabel="Number of people");
line_plot(total_pop_im);
save_figure("total_pop_wim.eps",5inch,3inch,format="ps");
# 5-year projection (no immigration)
new_figure(xlabel="Age",ylabel="Number of people");
line_plot(populations_im[:,1],color="blue");
line_plot(populations_im[:,5],color="red");
save_figure("pop_and_proj_wim.eps",5inch,3inch,format="ps");
# Simulate with a sudden college country
# Immigration is reduced by a factor of 5e3
p = zeros(100);
p[19:26] = 18e3 / 8;
total_pop_stanford = sum(p);
populations_stanford = p;
Ts = 200;
for i=1:Ts
p = A * p;
total_pop_stanford = [total_pop_stanford; sum(p)];
populations_stanford = [populations_stanford p];
end
# Stanford population prediction
colors = {"blue","green","orange","red"};
plot_data = {populations_stanford[:,1],populations_stanford[:,6],
populations_stanford[:,26],populations_stanford[:,61]};
new_figure(xlabel="Age",ylabel="Number of people");
for i = 1:4
line_plot(plot_data[i],color=colors[i]);
end
save_figure("proj_stanford_small.eps",5inch,3inch,format="ps");
# Stanford population prediction (long term)
plot_data = {populations_stanford[:,61],populations_stanford[:,101],
populations_stanford[:,201]};
new_figure(xlabel="Age",ylabel="Number of people");
for i = 1:3
line_plot(plot_data[i],color=colors[i])
end
save_figure("proj_stanford_long.eps",5inch,3inch,format="ps");
# Total population stanford
new_figure(xlabel="Years after college nation",ylabel="Number of people");
line_plot(total_pop_stanford);
save_figure("total_pop_stanford.eps",5inch,3inch,format="ps");