get_optimal_grid: Get Optimal Grid Assignment

View source: R/get_optimal_grid.R

get_optimal_gridR Documentation

Get Optimal Grid Assignment

Description

This function computes an optimal grid assignment between two variables x and y based on a third variable z. It uses the quantiles of x and y to segment the data based on the distribution of z. Then, it computes the cost of assigning points from x to y by calculating the counts of y values within quantile ranges of x and y, and then solves the assignment problem using the Hungarian algorithm.

Usage

get_optimal_grid(x, y, z)

Arguments

x

A numeric vector of values representing the first variable.

y

A numeric vector of values representing the second variable.

z

A numeric vector representing the distribution of the third variable, used to define quantiles for x and y.

Value

A numeric vector of optimal indices that represents the optimal assignment of x values to y values.

Examples

# Test Case 1: Simple uniform data
x <- rnorm(1000)
y <- rnorm(1000)
z <- sample(1:5, 1000, replace = TRUE)
optimal_assignment <- get_optimal_grid(x, y, z)

# Test Case 2: Data with a skewed distribution
x <- rexp(1000, rate = 1)
y <- rpois(1000, lambda = 2)
z <- sample(1:3, 1000, replace = TRUE)
optimal_assignment <- get_optimal_grid(x, y, z)


covalchemy documentation built on April 12, 2025, 2:15 a.m.