gdrsclib
Functions
blockmath.c File Reference
#include <math.h>
#include <stdlib.h>
#include "datastruct.h"
#include "blockmath.h"

Go to the source code of this file.

Functions

struct datablock transform (struct datablock dbin, double(*f)(double), struct appdata ad)
 Apply a function to a datablock. More...
 
struct datablock binop (struct datablock a, struct datablock b, char op, struct appdata ad)
 Carry out a binary operation (+, -, *, /) on two datablock structures. More...
 
struct datablock scalmult (double scalar, struct datablock dbin, struct appdata ad)
 Multiply a datablock by a scaling factor. More...
 
double pnorm (double x)
 The cumulative standardized normal distribution. More...
 
double quick_qnorm (double p)
 Abramowitz & Stegun approximation to qnorm to get a good first guess. More...
 
double qnorm (double p)
 The inverse cumulative standardized normal distribution. More...
 
double dnorm (double x)
 The standard normal probability density. More...
 
double ztransf (double y, double ybar, double sigma)
 The transformed value \( z = \frac{1}{\sigma} \ln\left(y/\bar y\right) + \frac{1}{2}\sigma\). More...
 
double gini2sdlog (double gini)
 The standard deviation of the log of income corresponding to a given Gini, assuming lognormal. More...
 
struct timeseries filter (struct datablock data, struct fixedfactor filter, struct appdata ad)
 Aggregate data in a datablock using a fixedfactor "filter" to produce a timeseries. More...
 
double filteryr (int year, struct datablock data, struct fixedfactor filter, struct appdata ad)
 Aggregate data in a datablock using a fixedfactor "filter" for a particular year. More...
 

Function Documentation

◆ binop()

struct datablock binop ( struct datablock  a,
struct datablock  b,
char  op,
struct appdata  ad 
)

Carry out a binary operation (+, -, *, /) on two datablock structures.

Parameters
aFirst input datablock
bSecond input datablock
opBinary operation to carry out
adappdata structure with dimensions
Returns
The result as a datablock

Definition at line 69 of file blockmath.c.

◆ dnorm()

double dnorm ( double  x)

The standard normal probability density.

Parameters
xThe value at which the density function is computed
Returns
The standard normal probability density at the specified value

Definition at line 201 of file blockmath.c.

◆ filter()

struct timeseries filter ( struct datablock  data,
struct fixedfactor  filter,
struct appdata  ad 
)

Aggregate data in a datablock using a fixedfactor "filter" to produce a timeseries.

Parameters
dataDatablock with data
filterFixed factor with coefficients (typically 1 and 0) to multiply data by before summing
adappdata with dimensions
Returns
A time series, calculated as sum(filter * data)

Definition at line 250 of file blockmath.c.

◆ filteryr()

double filteryr ( int  year,
struct datablock  data,
struct fixedfactor  filter,
struct appdata  ad 
)

Aggregate data in a datablock using a fixedfactor "filter" for a particular year.

Parameters
yearYear for which to evaluate result
dataDatablock with data
filterFixed factor with coefficients (typically 1 and 0) to multiply data by before summing
adappdata with dimensions
Returns
A single value, calculated as sum(filter * data) for year "year"

Definition at line 279 of file blockmath.c.

◆ gini2sdlog()

double gini2sdlog ( double  gini)

The standard deviation of the log of income corresponding to a given Gini, assuming lognormal.

Parameters
giniThe Gini coefficient value at which to evaluate the function (in percentage points, 0..100)
Returns
The standard deviation of the log of income, or NaN if out of bounds

Definition at line 235 of file blockmath.c.

◆ pnorm()

double pnorm ( double  x)

The cumulative standardized normal distribution.

Parameters
xValue at which to evaluate the function
Returns
Cumulative standardized normal distribution \(N(x)\)

Definition at line 128 of file blockmath.c.

◆ qnorm()

double qnorm ( double  p)

The inverse cumulative standardized normal distribution.

Parameters
pThe probability at which to evaluate the function
Returns
Inverse cumulative standardized normal distribution, \(N^{-1}(p)\), or NaN if exceed max iterations
Note
Carries out a standard Newton method, using quick_qnorm to get a first guess

Definition at line 167 of file blockmath.c.

◆ quick_qnorm()

double quick_qnorm ( double  p)

Abramowitz & Stegun approximation to qnorm to get a good first guess.

Parameters
pProbability level at which to evaluate the function
Returns
Approximation to qnorm
Note
Uses A&S formula 26.2.22, good to 3e-3

Definition at line 140 of file blockmath.c.

◆ scalmult()

struct datablock scalmult ( double  scalar,
struct datablock  dbin,
struct appdata  ad 
)

Multiply a datablock by a scaling factor.

Parameters
scalarThe factor to multiply dbin by
dbinThe input datablock
adappdata structure with dimensions
Returns
The result as a datablock

Definition at line 103 of file blockmath.c.

◆ transform()

struct datablock transform ( struct datablock  dbin,
double(*)(double)  f,
struct appdata  ad 
)

Apply a function to a datablock.

This file implements special mathematical functions, and math operations on data structures

Parameters
dbinThe datablock to transform
fA function to apply to each element of the datablock
adappdata structure with dimensions
Returns
The datablock after applying function f

Definition at line 38 of file blockmath.c.

◆ ztransf()

double ztransf ( double  y,
double  ybar,
double  sigma 
)

The transformed value \( z = \frac{1}{\sigma} \ln\left(y/\bar y\right) + \frac{1}{2}\sigma\).

Parameters
yThe income at which the function is evaluated
ybarMean income for the country
sigmaStandard deviation of the log of income for the country
Returns
The transformed value z

Definition at line 213 of file blockmath.c.