Skip to content

Instantly share code, notes, and snippets.

# Benchmarking a call to 'Inequality.gini' without weights,
# and with a vector of 1s as weights.
using BenchmarkTools
using Inequality
v = rand(1:10000, 300)
w = repeat([1], 300)
@benchmark gini($v) evals=15
@JosepER
JosepER / wtd_atkinson.jl
Created February 13, 2022 14:41
Weighted Atkinson optimized
##### Atkinson index #####
"""
wtd_atkinson(v, w, ϵ)
Computes the weighted Atkinson index given an inequality adversion parameter ϵ.
"""
function wtd_atkinson(v::Array{<:Real,1}, w::Array{<:Real,1}, ϵ::Real)::Float64
norm_mean!(v)
w = w/sum(w)
@JosepER
JosepER / weighted_inequality_indicators.jl
Last active February 13, 2022 14:39
Weighted inequality indicators
##### Atkinson index #####
"""
atkinson(v, w, ϵ)
Computes the weighted Atkinson index given an inequality adversion parameter ϵ.
"""
function wtd_atkinson(v::Array{<:Real,1}, w::Array{<:Real,1}, ϵ::Real) ::Float64
v = v/Statistics.mean(v)
w = w/sum(w)
@JosepER
JosepER / benchmarking_weighted_Atkinson_functions.R
Created September 9, 2021 07:07
Benchmarking Weighted Atkinson functions
library(tidyverse)
library(magrittr)
library(bench)
julia_weighted_atkinson_function <- JuliaCall::julia_eval("function weighted_atkinson(v, w, ϵ::Real, skipmissing::Bool)
if skipmissing
w = w[findall(!ismissing, v)]
v = v[findall(!ismissing, v)]
end
v = v/Statistics.mean(v)
@JosepER
JosepER / julia_weighted_atkinson.R
Created September 9, 2021 07:04
An R function wrapping over Julia code for weighted Atkinson index
weighted_atkinson_jl <- function(x, weights, epsilon, na.rm = FALSE){
JuliaCall::julia_call("weighted_atkinson", x, weights, epsilon, na.rm)
}
julia_weighted_atkinson_function <- JuliaCall::julia_eval("function weighted_atkinson(v, w, ϵ::Real, skipmissing::Bool)
if skipmissing
w = w[findall(!ismissing, v)]
v = v[findall(!ismissing, v)]
@JosepER
JosepER / wtd_quantile.R
Created August 29, 2021 16:13
An R function wrapping over Julia's 'StatsBase.quantile()'
#' Weighted quantiles
#'
#' Wrap over Julia's 'StatsBase.quantile()' function.
#'
#' @param x A numeric vector for which the quantiles should be computed.
#' @param weights A numeric vector with the weights.
#' @param probs A numeric vector with the quantiles to compute.
#' @param na.rm a logical value indicating whether NA values should be stripped before the computation proceeds.
#'
#' @return A numeric vector with the weighted quantiles.
import re
def backward_string_by_word(text: str) -> str:
spaces = re.findall(" +", text)
words = text.split()
if len(words) == 0:
@JosepER
JosepER / LISSY_R_code_for_example_plots
Last active June 15, 2020 19:51
LISSY - R code for example plots
library(readr)
library(dplyr)
library(magrittr)
library(purrr)
library(ggplot2)
all_lissyrtools_scripts <- fs::dir_ls("/media/user/lissyrtools/")
invisible(purrr::map(all_lissyrtools_scripts, ~ source(.x)))
# Prepare session
library(rlang)
library(glue)
library(fs)
library(dplyr)
library(magrittr)
library(purrr)
library(ggplot2)
@JosepER
JosepER / gini function
Last active May 25, 2020 14:57
Compute the gini index with (optional) weights
#' Compute gini index.
#'
#' Compute the gini index with (optional) weights.
#'
#' @param x A numeric vector with the data.
#' @param weights A numeric vector with sample weights.
#' @return res An atomic vector with the gini index.
gini <- function(x, weights = NULL, na.rm = FALSE) {