Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Covariance function to Gekko #138

Open
APMonitor opened this issue Feb 3, 2022 · 3 comments
Open

Add Covariance function to Gekko #138

APMonitor opened this issue Feb 3, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@APMonitor
Copy link
Member

From a gekko user:
This may be a long shot but I am doing a portfolio optimization which require integer constraints. I am looking at the Gekko package that you maintain. However, my issue is that I would need to calculate a covariance matrix within my implementation based on my variable. Is there a way to do it as I am not able to use the numpy.cov function within gekko?

Covariance of two variables is sum(i=1...N) ((xi-xmean)*(yi-ymean))/N This can be written in gekko directly. Consider adding a new gekko function to support the calculation with a function call, similar to numpy.cov.

@APMonitor APMonitor added the enhancement New feature or request label Feb 3, 2022
@APMonitor APMonitor self-assigned this Feb 3, 2022
@shayandavoodii
Copy link

shayandavoodii commented Feb 3, 2022

it's a great idea. I guess the Gekko user wants to implement portfolio optimization by Markowitz Model(due to mentioned covariance matrix). this package helped me a lot; I did portfolio optimization based on the Kelly model almost last year and used Gekko for that purpose. I'll use this great tool for further research as always. Thanks

@APMonitor
Copy link
Member Author

Thanks @shayandavoodii - that use case is helpful.

Here is a similar StackOverflow question with example code in Gekko:

import numpy as np
from gekko import GEKKO

def cov(m,x,y,ddof=1):
    ''' Calculate the covariance matrix of x, y
    Inputs:
      m: Gekko model
      x: x vector of equal length to y
      y: y vector of equal length to x
      [ddof=1]: delta degrees of freedom
    Returns:
      c: covariance as a Gekko variable
    '''
    nx = len(x); ny = len(y)  # length of x, y
    if nx!=ny:
        print('Error: mismatch of x and y')
    xm = m.sum(x)/nx  # mean of x
    ym = m.sum(y)/ny  # mean of y
    c = m.Var()       # covariance
    m.Equation(c==(m.sum([(x[i]-xm)*(y[i]-ym) \
                     for i in range(nx)]))/(nx-ddof))
    return c

m = GEKKO()

n = 4
x = m.Array(m.Param,n)
y = m.Array(m.Param,n)
xi = [2.1,2.5,3.6,4.0]
yi = [8,10,12,14]
for i in range(n):
    x[i].value = xi[i]
    y[i].value = yi[i]

c0 = cov(m,x,y,ddof=0)
c1 = cov(m,x,y)

m.solve(disp=False)

print('Covariance (Numpy) population cov: ', np.cov(xi,yi,ddof=0)[0,1])
print('Covariance (Numpy) sample cov: ', np.cov(xi,yi)[0,1])
print('Covariance (Gekko) population cov: ', c0.value[0])
print('Covariance (Gekko) sample cov: ', c1.value[0])

@shayandavoodii
Copy link

Thanks @shayandavoodii - that use case is helpful.

Here is a similar StackOverflow question with example code in Gekko:

import numpy as np
from gekko import GEKKO

def cov(m,x,y,ddof=1):
    ''' Calculate the covariance matrix of x, y
    Inputs:
      m: Gekko model
      x: x vector of equal length to y
      y: y vector of equal length to x
      [ddof=1]: delta degrees of freedom
    Returns:
      c: covariance as a Gekko variable
    '''
    nx = len(x); ny = len(y)  # length of x, y
    if nx!=ny:
        print('Error: mismatch of x and y')
    xm = m.sum(x)/nx  # mean of x
    ym = m.sum(y)/ny  # mean of y
    c = m.Var()       # covariance
    m.Equation(c==(m.sum([(x[i]-xm)*(y[i]-ym) \
                     for i in range(nx)]))/(nx-ddof))
    return c

m = GEKKO()

n = 4
x = m.Array(m.Param,n)
y = m.Array(m.Param,n)
xi = [2.1,2.5,3.6,4.0]
yi = [8,10,12,14]
for i in range(n):
    x[i].value = xi[i]
    y[i].value = yi[i]

c0 = cov(m,x,y,ddof=0)
c1 = cov(m,x,y)

m.solve(disp=False)

print('Covariance (Numpy) population cov: ', np.cov(xi,yi,ddof=0)[0,1])
print('Covariance (Numpy) sample cov: ', np.cov(xi,yi)[0,1])
print('Covariance (Gekko) population cov: ', c0.value[0])
print('Covariance (Gekko) sample cov: ', c1.value[0])

Thank you, professor, this would be great help and practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants