I am trying to maximize the following function:
What my goal is to find the values of the vectors x and y, that maximize L. K_i^out and K_i^in are the in_degree and the out degree of a node i, in graph G
, which are basically integers from 0 to 100. I read that the minimize
function was good for this and therefor I wrote the following code:
import networkx as nx
import scipy as sp
import numpy as np
from scipy.optimize import minimize
def f(z, n):
frst_term = 0 # first term in B(43)
scnd_term = 0 # second term in B(43)
for i in range(n): # n is the amount of nodes in Graph G.
frst_term += -G.out_degree(i)*np.log(z[i]) + -G.in_degree(i)*np.log(z[n+i])
#description of first term where z[i] is x_i and z[n+i] = y_i
for j in range(n):
if i == j:
None
else:
scnd_term += np.log(1+z[i]*z[n+j]) #z[i] = x_i z[n+j] = y_j
lik = (frst_term - scnd_term) #the total function
return(lik)
w = 2*n*[0.5] #my first guess
max_val = minimize(f, w, args=(n))
print(max_val)
From this I get the runtime warning
invalid value encountered in log
and
invalid value encountered in reduce
return umr_maximum(a, axis, None, out, keepdims)
The x and y values are supposed to be all positive, and somewhere between 0 and 10 maximimum, mostly between 0 and 1 though. To conclude: do any of you have suggestions on how to improve this code or any other ways to solve this problem?