简体   繁体   中英

Distribute a sales value to four different values in python

I have a sales data for few years, I want to distribute data for few quarters. I can simply divide a that sales number over four quarters, but I would like to have it like a gaussian distribution or sinusoidal distribution kind of using python code. Any idea would be of great help. Asking for the first time in stack overflow, forgive me if I there is some mistake in my presentation.

What I have

Year Sales
2020 1400
2021 1800

What is required

Quarter Sales
2020Q1 300
2020Q2 500
2020Q3 400
2020Q4 200

when I sum up all quarter values it sums up to 1400 as above.

I tried to check if there are few modules, I found there is a module called sympy but dont know how to fit my problem in that.

I don't think you need SymPy for this. The python random module has the ability to generate random numbers in a Gauss distribution but this is going to be hard to see on only 4 bins. One approach would be to generate random numbers (with some distribution), scale them to get close to the answer, add any deficit, and then sort them and return them centered on the maximum value to get some peak symmetry:

import random
def g(bins, total, mu, si):
  pts = [abs(random.gauss(mu, si)) for i in range(bins)]
  mag = total/sum(pts)
  pts = [int(i*mag) for i in pts]
  defic = total - sum(pts)
  while defic:
    random.shuffle(pts)  # distribute deficit randomly
    pts[0] += 1
    defic -= 1
  pts = sorted(pts)
  rv = []
  while pts:
      if len(rv)%2:
          rv.append(pts.pop())
      else:
          rv.insert(0, pts.pop())
  return rv

eg

>>> g(10,1000,1000,100)
[91, 96, 103, 107, 112, 109, 105, 98, 94, 85]
>>> g(10,1000,100,100)
[9, 47, 63, 154, 360, 161, 125, 49, 30, 2]
>>> sum(_)
1000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM