Module osl_stat

module osl_stat

    ! Uses
    use osl_core, wp => osl_wp
    use osl_dist

    ! Interfaces
    public interface osl_mean
    public interface osl_cov
    public interface osl_var

    ! Subroutines and functions
    public function osl_mean_vwp(v) result (mean)
    public function osl_mean_mwp(a) result (mean)
    public function osl_mean_vi(v) result (mean)
    public function osl_mean_mi(a) result (mean)
    public function osl_cov_vwp(x, y, mean_x, mean_y) result (cov)
    public function osl_cov_mwp(x, y, mean_x, mean_y) result (cov)
    public function osl_var_vwp(x, mean) result (var)
    public function osl_var_mwp(x, mean) result (var)
    public subroutine osl_sample_stats(sample, mean, var, sd, truth, bias, mse, rmse)
    public subroutine osl_tauchen(rho, sigma, n, cover, z, pi)

end module osl_stat

Description of Interfaces

osl_mean

public interface osl_mean
    module procedure osl_mean_vwp
    module procedure osl_mean_mwp
    module procedure osl_mean_vi
    module procedure osl_mean_mi
end interface osl_mean

osl_cov

public interface osl_cov
    module procedure osl_cov_vwp
    module procedure osl_cov_mwp
end interface osl_cov

osl_var

public interface osl_var
    module procedure osl_var_vwp
    module procedure osl_var_mwp
end interface osl_var

Description of Subroutines and Functions

osl_mean_vwp

public function osl_mean_vwp(v) result(mean)
    real (kind=wp), dimension(:) :: v
    real (kind=wp) :: mean
end function osl_mean_vwp

Return the mean of a vector of real numbers V.

osl_mean_mwp

public function osl_mean_mwp(a) result(mean)
    real (kind=wp), dimension(:,:) :: a
    real (kind=wp), dimension(size(a,2)) :: mean
end function osl_mean_mwp

Return the mean of the rows of a matrix of real numbers A.

osl_mean_vi

public function osl_mean_vi(v) result(mean)
    integer, dimension(:) :: v
    real (kind=wp) :: mean
end function osl_mean_vi

Return the mean of a vector of integers V.

osl_mean_mi

public function osl_mean_mi(a) result(mean)
    integer, dimension(:,:) :: a
    real (kind=wp), dimension(size(a,2)) :: mean
end function osl_mean_mi

Return the mean of a matrix of integers A.

osl_cov_vwp

public function osl_cov_vwp(x, y, mean_x, mean_y) result(cov)
    real (kind=wp), dimension(:), intent(in) :: x
    real (kind=wp), dimension(size(x)), intent(in) :: y
    real (kind=wp), optional, intent(in) :: mean_x
    real (kind=wp), optional, intent(in) :: mean_y
    real (kind=wp) :: cov
end function osl_cov_vwp

Return the sample covariance between two vectors X and Y. If Y is omitted, return the variance of X. The provided means MEAN_X and MEAN_Y will be used if given, otherwise they will be calculated internally.

osl_cov_mwp

public function osl_cov_mwp(x, y, mean_x, mean_y) result(cov)
    real (kind=wp), dimension(:,:), intent(in) :: x
    real (kind=wp), dimension(size(X,1),size(X,2)), intent(in) :: y
    real (kind=wp), optional, dimension(size(X,2)), intent(in) :: mean_x
    real (kind=wp), optional, dimension(size(X,2)), intent(in) :: mean_y
    real (kind=wp), dimension(size(X,2),size(X,2)) :: cov
end function osl_cov_mwp

Return the sample covariance between the rows of the matrices X and Y. The provided means MEAN_X and MEAN_Y will be used if given, otherwise they will be calculated internally.

osl_var_vwp

public function osl_var_vwp(x, mean) result(var)
    real (kind=wp), dimension(:), intent(in) :: x
    real (kind=wp), optional, intent(in) :: mean
    real (kind=wp) :: var
end function osl_var_vwp

Return the sample variance of a vector X. The provided MEAN will be used if given, otherwise it will be calculated internally.

osl_var_mwp

public function osl_var_mwp(x, mean) result(var)
    real (kind=wp), dimension(:,:), intent(in) :: x
    real (kind=wp), optional, dimension(size(X,2)), intent(in) :: mean
    real (kind=wp), dimension(size(X,2),size(X,2)) :: var
end function osl_var_mwp

Return the sample covariance between the rows of the matrices X and Y. If Y is omitted, return the variance of X. The provided means will be used if given, otherwise they will be calculated internally.

osl_sample_stats

public subroutine osl_sample_stats(sample, mean, var, sd, truth, bias, mse, rmse)
    real (kind=wp), dimension(:,:), intent(in) :: sample
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: mean
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: var
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: sd
    real (kind=wp), optional, dimension(size(sample,2)), intent(in) :: truth
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: bias
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: mse
    real (kind=wp), optional, dimension(size(sample,2)), intent(out) :: rmse
end subroutine osl_sample_stats

Calculate sample statistics for a sample of vectors. The observations must be stored as rows of the variable SAMPLE. Any of the following statistics may be calculated (all are optional): mean (MEAN), standard deviation (SD), variance (VAR), bias (BIAS), mean squared error (MSE), root mean squared error (RMSE). Note that TRUTH must be present in order to calculate BIAS, MSE, or RMSE (zero is returned otherwise).

osl_tauchen

public subroutine osl_tauchen(rho, sigma, n, cover, z, pi)
    real (kind=wp), intent(in) :: rho
    real (kind=wp), intent(in) :: sigma
    integer, intent(in) :: n
    real (kind=wp), intent(in) :: cover
    real (kind=wp), dimension(n), intent(out) :: z
    real (kind=wp), dimension(n,n), intent(out) :: pi
end subroutine osl_tauchen

Construct a discrete Markov chain approximation for a given AR(1) process using Tauchen's (1986) method. This code is based on code from the online appendix of Aruoba, Fernandez-Villaverde, and Rubio-Ramirez (2006).

References

  • Tauchen, G. (1986). Finite State Markov-Chain Approximations to Univariate and Vector Autoregressions. Economics Letters 20, 177-181.
  • Aruoba, S. B., J. Fernandez-Villaverde, and J. F. Rubio-Ramirez (2006). Comparing Solution Methods for Dynamic Equilibrium Economies. J. Economic Dynamics and Control 30, 2477-2508.