2

Suppose we have to allocate x amount to k desired amounts. Is there algorithm to do this that minimizes the squared distance between the actual k allocated values and the k desired amounts?

For example suppose we need to allocate x=5 to k=3 desired amounts of 2,-3,4.

We could allocate the 5 to 2,-3,6 producing squared distance of 0^2 + 0^2 + 2^2 = 4.

We are allowed to allocate negative amounts or any amount to the k amounts. The only restriction is the allocated amounts must sum to original x. Also allocated amounts do not need to be integers, only real numbers.

josliber
  • 43,891
  • 12
  • 98
  • 133
steviekm3
  • 905
  • 1
  • 8
  • 19

4 Answers4

1

Let y be the sum of the desired amounts and let d be the difference between x and y. The minimum is obtained by distributing d equally among the k desired amounts. Exercise for the reader: prove this using Lagrangian multipliers.

In the given example, y = 2 - 3 + 4 = 3 and d = 5 - 3 = 2. Allocating d=2 evenly to the k desired values means adding 2/3 to each of the items, leading to an allocation of 2 2/3, -2 1/3, and 4 2/3.

mhum
  • 2,928
  • 1
  • 16
  • 11
0

Two approaches come to mind:

  1. Lagrange multipliers. Ideal for minimizing a quadratic cost function with a linear constraint. The link provides several example on how to setup and solve the problem.

    cost

    lagrange

    which yields

    lambda1

    applying the constraint and defining

    y

    gives

    lambda2

    substituting back gives the final solution

    soln

    In other words, we compute the difference between the total desired outputs and total constraint and divide it equally among them.

  2. Fair division. If you are willing to relax the least squares criteria your problem might be solveable using fair division techniques. In particular, adjusted winner is a way of fairly allocating goods among players. You won't get negative answers like you suggested but is more like a cake cutting solution.

dpmcmlxxvi
  • 1,292
  • 1
  • 9
  • 13
0

Basically for vector v (of length k) of data and total budget b, you have the following optimization problem:

min_{x1, x2, ..., xk} (x1-v1)^2 + (x2-v2)^2 + ... + (xk-vk)^2
s.t. x1 + x2 + ... + xk = b

This is a linearly constrained quadratic program, which can be solved using quadratic programming software packages. For instance, here's a solution with the quadprog package in the R language:

# Setup data
v <- c(2, -3, 4)
b <- 5

# Solve quadratic program
library(quadprog)
solve.QP(diag(length(v)), v, matrix(rep(1, length(v))), b, 1)$solution
# [1]  2.666667 -2.333333  4.666667

In this example, we get objective value 4/3, smaller than the objective value of 4 for the allocation provided in the original post.

josliber
  • 43,891
  • 12
  • 98
  • 133
0

This is a special case LP. The minimum is always achieved by allocating such that your delta-k values are all the same.

Since the objective function is sum of squares, and all data points are equally weighted, allocating other than same-value delta-ks results in a higher sum of squares. (Note that in josilber's quadprog solution the delta-k values are all 2/3. There's a proof hiding somewhere in partial derivative land that I'm too tired or dumb to work out.)