Skip to content
Advertisement

Subset-AVG – Finding a subset of List Which Matches Known Rational Number

I’ve asked this on math overflow and used comments to clarify/overstate my question. I hope it has the intended effect and doesn’t come off as jarring.

I’m attempting to find what subset of numbers reach a known average.

I have a list of known values, negative and possible decimals. They look something like this {-.32,-.64,-.12,.08,-.54,-.43, …}

It’s around 50 numbers in some cases, though this problem would be tested for other cases as well.

The set mostly contains negative decimal numbers, while in rare cases, has a few positive decimals – it never has whole numbers.

I also have a known value, which I know to be the average of some subset of the above list.

The known value is similar to -.03.

I’m uncertain of the grouping mechanism used, but seemed to reach stack overflow trying to solve this problem when not grouping.

I’ve attempted a few ways of going about solving this problem. I’m using Python 3.6 and imported numpy as np.

I’m wondering if the “subset-avg” code I’ve adapted from another solution for subset-sum here (I’ll give due credit when i can find that question again) is not the most efficient way/if there’s any huge mistake in my even attempting to resolve this that I haven’t seen.

Thanks in advance for any thoughts.

JavaScript

Advertisement

Answer

Here is a solution I adapted from a subSet sum algorithm I posted for another question (here). Since the algorithm loops through potential solution sizes, it was easy to adapt it to search for an average.

The iSubSum() function takes 3 parameters: The target average, the list of values and an optional rounding precision parameter. It is a generator so it will produce all possible solutions when used in a loop. You can also get the first solution quickly using the next()function. This should produce results much faster than a brute force approach, especially for large lists.

The function is based on a modified version of a subset-sum algorithm that returns solutions as lists of indices. This is intended to distinguish combinations that have duplicate values coming from different indices in the original list.

JavaScript

To get the actual values, the iSubAvg() function maps indices to the corresponding values in the list:

JavaScript

note that the function returns all combinations including repetitions for duplicate values in the source list. You can filter out these duplicates if you don’t need them

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement