Experiment 2: Power Sums
Experiment 2: Power Sums
The aim of this experiment is to accompany the derivations in Section: Aggregating a Group of Agents (see Section: Aggregating a Group of Agents). The essence of the program is quite simple:
- Generate vectors of length with realized values drawn from a theoretical distribution .
- Compute the sum of values in the vector .
- Divide it by the sum of the zero levels (), which is equal to .
The outcome is a time series of length , and we store its moments, as well as the moments of its log levels.
import pandas as pd
import numpy as np
# (names of) Log shocks distributions
dists = ['norm', 'lapl', 'empirical']
# Values of micro standard deviation
ss = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
# Values of micro mean fluctuations
mus = [0, 0.01, 0.02, 0.05, 0.1]
# Repetitions
M = 200
# Time steps
T = 17
## List of results
results = []
s0 = empirical_shocks.std()
for distribution in dists:
# Second quantile and next contain most of the value with few agents
for q in range(Q)[1:]:
n = population['n_q'][q] # number of agents in quantile.
for s in ss: # micro sigma
for mu in mus: # micro mu
for m in range(M): # Repeat M times
if distribution == 'norm':
shocks = np.random.normal(mu, s, (n, T))
elif distribution == 'lapl':
shocks = np.random.laplace(mu, s/np.sqrt(2), (n, T))
elif distribution == 'empirical':
shocks = (mu + np.random.choice(emp_shocks, n * T)*(s/s0)).reshape(n, T)
ratio = np.power(10, shocks).sum(0)/n
log_ratio = np.log10(ratio)
results += [[distribution, s, mu, n, m, ratio.mean(), ratio.std(), ratio.var(), log_ratio.mean(), log_ratio.std(), log_ratio.var()]]
# Create dataframe with the computed information
result = pd.DataFrame(results, columns = ['distribution', 's', 'mu', 'nq', 'repeat', 'mean_ratio', 'std_ratio', 'var_ratio', 'mean_log_ratio', 'std_log_ratio', 'var_log_ratio'])
# Save pandas DataFrame
result.to_csv('./filename.csv', index = False)
The above code snippet demonstrates the process of generating and analyzing the power sums. The results are stored in a pandas DataFrame and saved to a CSV file for further analysis.