gdrsclib
|
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "gdrs.h"
Go to the source code of this file.
Functions | |
void | handle_sql3error (sqlite3 *db, sqlite3_stmt *stmt) |
Function that wraps repetitive code for SQLite3 errors. More... | |
int | loadgdrs (struct gdrs_struct *gdrs, const char *fname) |
Function to translate from SQLite3 database to internal data structures. More... | |
int | readtable (sqlite3 *db, const char *column, struct datablock *table, struct appdata ad) |
Helper function to read a column from the "core" table into a datablock. More... | |
void | cleargdrs (struct gdrs_struct gdrs) |
Clear out the GDRs data structure. More... | |
double | globaldist (double y, int year, struct gdrs_struct *gdrs, int ppp_or_mer, double *dist_deriv) |
Calculate the fraction of global population below a specified income level in the given year. More... | |
double | globalquintile (double q, int year, struct gdrs_struct *gdrs, int ppp_or_mer) |
Calculate the income at a given quintile for the world as a whole. More... | |
double | nationalquintile (double q, double ybar, double sigma) |
Calculate the income at a given quintile for a country. More... | |
void | combine_gap (struct fixedfactor *gap, int i, double newgap, int combine) |
Calculate and store luxury emissions. More... | |
void | calc_kab (struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs) |
Calculate and store "Kyoto gap" for Kyoto-adjusted baselines. More... | |
void | calc_luxemiss (struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs) |
Calculate and store luxury emissions. More... | |
void | calcallocs (struct gdrs_struct *gdrs) |
Calculate allocations for all years. More... | |
void | nextrci (int year, struct gdrs_struct *gdrs) |
Calculate the GDRs Responsibility-Capacity Indicator for a particular year. More... | |
double | M (double a, double zc, double ybar, double sigma) |
Calculate a function \(M_a(y_c,\bar{y},\sigma)\) that is repeatedly used in calculations. More... | |
double | a_gamma (double gamma, double y, double a, double b) |
Calculate the \(a_\gamma(y;a,b)\) function. More... | |
double | b_gamma (double gamma, double y, double b) |
Calculate the \(b_\gamma(y;b)\) function. More... | |
double | A_gamma (double gamma, double za, double zb, double yl, double yu, double ybar, double sigma) |
Calculate the \(A_\gamma(a,b;y_l,y_u/\pi_i,\bar{y}_i,\sigma_i)\) function. More... | |
double | B_gamma (double gamma, double za, double zb, double yu, double ybar, double sigma) |
Calculate the \(B_\gamma(a,b;y_u/\pi_i,\bar{y}_i,\sigma_i)\) function. More... | |
double | capbetween (double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double pi, double luxmult) |
Calculate capacity per capita between two income limits. More... | |
double | respbetween (double gamma, double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double A) |
Calculate responsibility between two income limits. More... | |
double | rhat (double E, double gamma, double sigma, double ybar) |
Calculate the factor \(\hat{r}_i\) for a country. More... | |
double | c_indiv (double y, double yl, double yu, int do_interp) |
Calculate individual per capita capacity \(c_i(y)\) at a specified income level. More... | |
double | r_indiv (double gamma, double y, double yl, double yu, double A, int do_interp) |
Calculate individual per capita annual responsibility \(r_i(y)\) at a specified income level. More... | |
double | luxcap (int year, struct gdrs_struct *gdrs) |
If calculating luxury-capped baselines adjust the luxury threshold if needed to stay above emergency pathway. More... | |
void | calcybar (struct gdrs_struct *gdrs) |
Calculate mean income from GDP and population. More... | |
struct datablock | z (double y, struct gdrs_struct *gdrs, int ppp_or_mer) |
Calculate the "z" transformed version of an income level for all countries and years. More... | |
int | dump2sql3 (const char *fname, struct gdrs_struct gdrs) |
After the calculation, write all results to the SQLite3 database. More... | |
double a_gamma | ( | double | gamma, |
double | y, | ||
double | a, | ||
double | b | ||
) |
Calculate the \(a_\gamma(y;a,b)\) function.
gamma | Elasticity |
y | Income (PPP) at which to evaluate the function |
a | Lower income limit |
b | Upper income limit |
\[0 \mbox{ if } |a - b| < \epsilon\]
or\[a_\gamma(y;a,b) = \frac{1}{b - a}\left[\frac{\gamma}{\gamma +1}\left(y^{\gamma+1}-a^{\gamma+1}\right)-a\left(y^\gamma - a^\gamma\right)\right],\]
double A_gamma | ( | double | gamma, |
double | za, | ||
double | zb, | ||
double | yl, | ||
double | yu, | ||
double | ybar, | ||
double | sigma | ||
) |
Calculate the \(A_\gamma(a,b;y_l,y_u/\pi_i,\bar{y}_i,\sigma_i)\) function.
gamma | Elasticity |
za | Lower income limit (transformed variable) |
zb | Upper income limit (transformed variable) |
yl | Development threshold |
yu | Luxury threshold |
ybar | Mean income for the country |
sigma | Standard deviation of log income for the country |
\[0 \mbox{ if } |y_u - y_l| < \epsilon\]
or\[A_\gamma(a,b) = \frac{1}{y/\pi_i-y_l}\left[ \frac{\gamma}{\gamma+1}\left(M_{\gamma+1}(a) - M_{\gamma+1}(b)\right) - y_l\left(M_\gamma(a) - M_\gamma(b)\right) + \frac{y_l^{\gamma+1}}{\gamma+1} \left(M_0(b) - M_0(a)\right)\right],\]
double b_gamma | ( | double | gamma, |
double | y, | ||
double | b | ||
) |
Calculate the \(b_\gamma(y;b)\) function.
gamma | Elasticity |
y | Income (PPP) at which to evaluate the function |
b | Upper income limit |
\[b_\gamma(y;b) = y^\gamma - b^\gamma.\]
double B_gamma | ( | double | gamma, |
double | za, | ||
double | zb, | ||
double | yu, | ||
double | ybar, | ||
double | sigma | ||
) |
Calculate the \(B_\gamma(a,b;y_u/\pi_i,\bar{y}_i,\sigma_i)\) function.
gamma | Elasticity |
za | Lower income limit (transformed variable) |
zb | Upper income limit (transformed variable) |
yu | Luxury threshold |
ybar | Mean income for the country |
sigma | Standard deviation of log income for the country |
\[B_\gamma(a,b) = \left(M_\gamma(a)-M_\gamma(b)\right) - \left(\frac{y_u}{\pi_i}\right)^\gamma\left(M_0(a) - M_0(b)\right),\]
double c_indiv | ( | double | y, |
double | yl, | ||
double | yu, | ||
int | do_interp | ||
) |
Calculate individual per capita capacity \(c_i(y)\) at a specified income level.
y | Income at which to calculate c_indiv |
yl | Development threshold |
yu | Luxury threshold |
do_interp | Boolean: are we interpolating between thresholds? |
void calc_kab | ( | struct fixedfactor * | gap, |
double * | totgap, | ||
int | year, | ||
int | period, | ||
int | combine, | ||
struct gdrs_struct * | gdrs | ||
) |
Calculate and store "Kyoto gap" for Kyoto-adjusted baselines.
gap | Difference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping) |
totgap | Sum of gaps across all countries |
year | Current year for the calculation |
period | For sequencing: PER_BEFORE_EP, PER_SEQUENCE, or PER_GDRS |
combine | If more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN |
gdrs | The global data structure for the GDRs calculation |
void calc_luxemiss | ( | struct fixedfactor * | gap, |
double * | totgap, | ||
int | year, | ||
int | period, | ||
int | combine, | ||
struct gdrs_struct * | gdrs | ||
) |
Calculate and store luxury emissions.
gap | Difference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping) |
totgap | Sum of gaps across all countries |
year | Current year for the calculation |
period | For sequencing: PER_BEFORE_EP, PER_SEQUENCE, or PER_GDRS |
combine | If more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN |
gdrs | The global data structure for the GDRs calculation |
Set gdrs.lux_emiss, gdrs.lux_emiss_applied
void calcallocs | ( | struct gdrs_struct * | gdrs | ) |
void calcybar | ( | struct gdrs_struct * | gdrs | ) |
double capbetween | ( | double | za, |
double | zb, | ||
double | zl, | ||
double | zu, | ||
double | yl, | ||
double | yu_ppp, | ||
double | ybar, | ||
double | sigma, | ||
double | pi, | ||
double | luxmult | ||
) |
Calculate capacity per capita between two income limits.
za | Lower income limit (transformed variable) |
zb | Upper income limit (transformed variable) |
zl | Development threshold (transformed variable) |
zu | Luxury threshold (transformed variable) |
yl | Development threshold |
yu_ppp | Luxury threshold in PPP terms |
ybar | Mean income for the country |
sigma | Standard deviation of log income for the country |
pi | Inverse of ppp2mer ratio |
luxmult | Tax multiplier on high incomes |
void cleargdrs | ( | struct gdrs_struct | gdrs | ) |
void combine_gap | ( | struct fixedfactor * | gap, |
int | i, | ||
double | newgap, | ||
int | combine | ||
) |
Calculate and store luxury emissions.
gap | Difference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping) |
i | Value in gap to set |
newgap | New value for entry i |
combine | If more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN |
int dump2sql3 | ( | const char * | fname, |
struct gdrs_struct | gdrs | ||
) |
double globaldist | ( | double | y, |
int | year, | ||
struct gdrs_struct * | gdrs, | ||
int | ppp_or_mer, | ||
double * | dist_deriv | ||
) |
Calculate the fraction of global population below a specified income level in the given year.
y | Income level below which to calculate the share of the global population |
year | Year to report population share |
gdrs | The global data structure for the GDRs calculation |
ppp_or_mer | Whether using PPP or MER conversion rates |
dist_deriv | Pointer to a variable to store the derivative of the function (for finding quintiles) or NULL |
double globalquintile | ( | double | q, |
int | year, | ||
struct gdrs_struct * | gdrs, | ||
int | ppp_or_mer | ||
) |
Calculate the income at a given quintile for the world as a whole.
q | The quintile (e.g., 0.5 for the median) |
year | The year in which to calculate the quintile |
gdrs | The global data structure for the GDRs calculation |
ppp_or_mer | Whether using PPP or MER conversion rates |
void handle_sql3error | ( | sqlite3 * | db, |
sqlite3_stmt * | stmt | ||
) |
int loadgdrs | ( | struct gdrs_struct * | gdrs, |
const char * | fname | ||
) |
double luxcap | ( | int | year, |
struct gdrs_struct * | gdrs | ||
) |
If calculating luxury-capped baselines adjust the luxury threshold if needed to stay above emergency pathway.
year | Year for calculating the adjusted luxury cutoff |
gdrs | Global data structure containing parameters and data for all calculations |
double M | ( | double | a, |
double | zc, | ||
double | ybar, | ||
double | sigma | ||
) |
Calculate a function \(M_a(y_c,\bar{y},\sigma)\) that is repeatedly used in calculations.
a | Elasticity |
zc | Income cutoff, transformed into "z" variable \( z = \frac{1}{\sigma} \ln\left(y/\bar y\right) + \frac{1}{2}\sigma\) |
ybar | Mean income |
sigma | Standard deviation of log income |
\[M_a(y_c,\sigma,\bar y) = \bar{y}^a e^{\frac{\sigma^2}{2} a (a - 1)} \left[1 - N(z_c - a \sigma)\right],\]
where \(N(\cdot)\) is the cumulative standardized normal distribution.\[M_a(k y_c,\sigma, k\bar y) = k^aM_a(y_c, \sigma, \bar y)\]
double nationalquintile | ( | double | q, |
double | ybar, | ||
double | sigma | ||
) |
Calculate the income at a given quintile for a country.
q | The quintile (e.g., 0.5 for the median, 0.2 for the bottom 20%, 0.8 for the top 20%) |
ybar | Mean income |
sigma | Standard deviation of log income |
\[y = \bar{y} e^{\sigma N^{-1}(q) - \sigma^2/2}\]
void nextrci | ( | int | year, |
struct gdrs_struct * | gdrs | ||
) |
Calculate the GDRs Responsibility-Capacity Indicator for a particular year.
year | Year for which to calculate RCI |
gdrs | Global data structure containing parameters and data Set gdrs.c, gdrs.r, gdrs.pop_above_dl, gdrs.pop_above_lux, gdrs.rci, gdrs.tax_revenue_mer_dens, tax_revenue_ppp_dens, gdrs.tax_pop_dens, gdrs.tax_income_mer_dens, tax_income_ppp_dens, gdrs.tax_pop_below |
double r_indiv | ( | double | gamma, |
double | y, | ||
double | yl, | ||
double | yu, | ||
double | A, | ||
int | do_interp | ||
) |
Calculate individual per capita annual responsibility \(r_i(y)\) at a specified income level.
gamma | Emissions elasticity |
y | Income at which to calculate r_indiv |
yl | Development threshold |
yu | Luxury threshold |
A | Overall coefficient (factor \(\hat{r}_i\)) |
Helper function to read a column from the "core" table into a datablock.
db | Handle for the SQLite3 database |
column | Name of the column in the core table of the database |
table | Internal data representation |
ad | appdata structure with dimensions |
double respbetween | ( | double | gamma, |
double | za, | ||
double | zb, | ||
double | zl, | ||
double | zu, | ||
double | yl, | ||
double | yu_ppp, | ||
double | ybar, | ||
double | sigma, | ||
double | A | ||
) |
Calculate responsibility between two income limits.
gamma | Emissions elasticity |
za | Lower income limit (transformed variable) |
zb | Upper income limit (transformed variable) |
zl | Development threshold (transformed variable) |
zu | Luxury threshold (transformed variable) |
yl | Development threshold |
yu_ppp | Luxury threshold in PPP terms |
ybar | Mean income for the country |
sigma | Standard deviation of log income for the country |
A | Overall multiplicative constant to convert to emissions |
double rhat | ( | double | E, |
double | gamma, | ||
double | sigma, | ||
double | ybar | ||
) |
Calculate the factor \(\hat{r}_i\) for a country.
E | Annual emissions |
gamma | Emissions elasticity |
sigma | Standard deviation of the log of income |
ybar | Mean income |
\[\hat{r}_i=\frac{E_{i}}{{\bar{y}_{i}}^{\gamma }}e^{-{\frac{\sigma _{i}^{2}}{\;2}}\gamma (\gamma -1)}\]
struct datablock z | ( | double | y, |
struct gdrs_struct * | gdrs, | ||
int | ppp_or_mer | ||
) |
Calculate the "z" transformed version of an income level for all countries and years.
y | The income at which to calculate z |
gdrs | Global data structure with variables and parameters |
ppp_or_mer | Whether using PPP or MER conversion rates |