Divide a number to many groups based on several factor using Python
Your assumption is not so good. Some cities may spend a bigger fraction of their total consumation in beer.
I think a better assumption is that it's a variable fraction of the total consumation in beer, let's say, city i consumes a fraction xi of national consumation in beer, where xi is somehow dependent on the GDP and on the city consumation.
To find xi, firstly scale the GDP to be in [delta, 1-delta], where delta is a positive quantity very close to zero, and keep their relative order. To do that, consider the biggest GDP is GDPmax and the minimum GDP is GDPmin. Then, map each GDPi to
scaleGDPi = [(GDPi - GDPmin) * (1 - 2 * delta)/(GDPmax-GDPmin)] + delta.
In a similar way, also scale the consumation to be in [delta, 1 - delta].
Then, consider xi = scaleGDPi * scaleConsumationi * x and you get (city beer)i = scaleGDPi * scaleConsumationi * x * national beer
By imposing that the sum of city beer is equal to national beer, you get:
x = 1 / (sum scaleGDPi * scaleConsumationi).
So, city beer = (scaleGDPi * scaleConsumationi * national beer)/(sum scaleGDPi * scaleConsumationi).
I think this would be a more adequate modelization of your problem.
I have retail sales data of the whole Germany, for example beer revenue. Now I want to find a way to divide that number into 596 cities of Germany based on the GDP per capita of each city and consumer spending of each city. So after that I can have the beer revenue of each single city in Germany.
My assumption is: city beer = city consumer spending * x + city GDP per cap * y. and then sum of city beer = national beer
Could you please advice which kind of algorithm or a way to do it in Python?
Thank you so much.