pythonrandomcorrelationcross-correlationpearson-correlation

How to calculate values based on correlation coefficients?


I have three lists with angle values:

import pandas as pd
import random

# Example data
angles1 = [1,1,5,6,4]
angles2 = [3,2,5,4,9]
angles3 = [6,9,8,2,1]

The angle values of the three lists are dependant from each other. This is what the correlation coefficients are showing me.

# Calculate the correlation coefficient
angles = list(zip(angles1,angles2,angles3))
anglesData = pd.DataFrame(angles,columns=['angle 1','angle 2','angle 3'])
print(anglesData.corr())

These are the calculated coefficients (result):

          angle 1   angle 2   angle 3
angle 1  1.000000  0.474267 -0.530212
angle 2  0.474267  1.000000 -0.690651
angle 3 -0.530212 -0.690651  1.000000

I want to randomize values for angle1, angle2 and angle 3. I can easily randomize the angle1 value:

# Get a random number from angles1
randomAngle1 = random.choice(angles1)
print(randomAngle1)

The thing is that I can not simply randomize values for angle2 and angle3 as well, as they are dependant from the value of angle1. They are also dependant from each other.

How can I randomize values for angle1, angle2 and angle3, which make sense and without neglecting the correlation?

Any kind of feedback is always appreciated. Thanks.


Solution

  • If the angles of your anglesData correspond to each other if they in the same row (and should not be separated), then you can simply select a random row of your dataframe (it will be a new dataframe consisting of only 1 selected row), which you can then convert into a list:

    anglesData.sample().to_numpy().flatten().tolist()
    

    You could also shuffle the rows of your dataframe:

    anglesData.sample(frac=1)