0

I have two datasets, like:

A=[ 1, 1.1, 1.2, 1.3, 1.1, 1.1, 1.2, 1.1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, 1.1]
B=[1.4, 1.4, 1.3, 1.4, 1.4, 1.5, 1.4, 1.4, 1.3, 1.3, 1.3, 1.4, 1.3, 1.3, 1.2, 1.2, 1.4]

I want to divide the distributions of them, distributionA/distributionB, but I can not find any solution, because they are not list to divide them easily. Actually I want to calculate the supremium of distributionA/distributionB in python. I found a toolbox in R that does the same thing:

https://github.com/hoxo-m/densratio

but I want to do this in Python

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • Does this answer your question? [Divide two lists in python](https://stackoverflow.com/questions/16418415/divide-two-lists-in-python) – David Buck Nov 12 '19 at 17:55
  • Unfortunately no. I should divide two distributions, but here they divide two lists. –  Nov 12 '19 at 18:34
  • What exactly do you mean by a 'distribution'? What you have in your question is two lists. – David Buck Nov 12 '19 at 19:30
  • For example, we consider the A as normal distribution by mean 1.08 and B as another normal distribution. –  Nov 12 '19 at 20:07
  • But regardless of the contents, in what format will you be getting A and B if not lists? If you want to elementwise divide A by B, the actually numbers don’t matter but the object they’re stored in does. Your example suggested they were in two equal length lists but if thats not right, you need to tell people what format they are in. – David Buck Nov 12 '19 at 20:20
  • @DavidBuck OP means a [probability distribution from statistics](https://en.wikipedia.org/wiki/Probability_distribution), the calculation they want to do involves lots of numerical optimisation (as per the paper associated with the R package) – Sam Mason Nov 13 '19 at 09:47

3 Answers3

2

This will divide every element in A by every element in B. If this is not what you need, please expand on your answer or post expected outcome.

res = [i / j for i, j in zip(A, B)] 
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
1

You can just map with the truediv operator:

from operator import truediv

list(map(truediv, A, B))
# [0.7142857142857143, 0.7857142857142858, 0.923076923076923, 0.9285714285714287...
yatu
  • 86,083
  • 12
  • 84
  • 139
0

that R package links to a Python module by the same author, I'd just use that! to install, just do the normal:

pip3 install -U densratio

then to use just follow the example in the docs:

from densratio import densratio

result = densratio(A, B)
print(result)

note though that this does crazy things with your data. I'd assume because because it's been rounded too much.

I'd start getting an estimate of the supremum by doing:

import numpy as np

x = np.linspace(-10, 10, 500)
y_hat = result.compute_density_ratio(x)

print(max(y_hat), x[np.argmax(y_hat)])

but you'd probably want to do lots of plots to make sure densratio is doing the right thing, e.g. start with:

import matplotlib.pyplot as plt

plt.plot(x, y_hat)

note I've not seen this package before, somebody who's used this before might be able to help more

Sam Mason
  • 15,216
  • 1
  • 41
  • 60