Brasilian_student
Brasilian_student

Reputation: 97

How to calculate correlation between two functions in Python

I need to calculate the correlation of two functions in Python

In R, I'd do:

g = function(x) {exp(-0.326723067*x)*cos(0.36002998837*x)}
f = function(x) {-x+1}

cor(f(u),g(u))

What would be the equivalent way in Python? I think I'd need to evaluate the function first before calculate the correlation. Is that so?

Upvotes: 2

Views: 806

Answers (1)

bird
bird

Reputation: 3316

Assuming you want Pearson's correlation. If so:

Using numpy:

import numpy as np
np.corrcoef(f(u), g(u))

Using scipy:

import scipy.stats
scipy.stats.pearsonr(f(u), g(u))

Using pandas:

import pandas as pd
f(u).corr(g(u)) # or
g(u).corr(f(u))

Using pingouin:

import pingouin as pg
pg.corr(f(u), g(u))

Upvotes: 2

Related Questions