在python

时间:2018-06-09 09:39:09

标签: python scipy maximize log-likelihood

我正在尝试最大化以下功能: enter image description here

我的目标是找到向量x和y的值,最大化L。 K_i ^ out和K_i ^ in是in_degree和节点i的out度,在图中G,基本上是从0到100的整数。我读到minimize函数对此有好处,因此我编写了以下代码:

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)

从此我得到运行时警告

invalid value encountered in log

invalid value encountered in reduce
return umr_maximum(a, axis, None, out, keepdims)

x和y值应该都是正数,并且在0到10最大值之间,大多数在0和1之间。总结:您是否有任何关于如何改进此代码或任何其他方法来解决此问题的建议?

0 个答案:

没有答案