【问题标题】:Seeking for the suggestion for optimization in python在python中寻求优化建议
【发布时间】:2020-08-30 14:36:32
【问题描述】:

我在 python 中的优化逻辑有问题。我有创建优化功能。逻辑是我需要更改 2 个参数,它们是 monthly_ccirhomonthly_cci 是包含 40 个值的随机 numpy 数组,rho 是一个必须为正的单个值(也是随机的)。

我需要这两个参数来适应公式来找到fitted_upper。我循环了 40 个循环以获得 40 个 fitted_upper 组合为一个名为 monthly_fitted 的数组。然后我用另一个公式转换monthly_fitted 得到fitted_matrix。我将fitted_matrix定义错误函数,即error_matrix

我需要通过更改monthly_ccirho 来尽可能降低error_matrix 的方差。我试图编写如下代码,但它不起作用。对这个案例有什么建议吗?

谢谢!

from scipy.optimize import minimize   

def cci(monthly_cci, rho):
    monthly_cci = np.random.randn(40)
    rho = np.random.rand(1)

    monthly_fitted = []

    for i in range(0, len(monthly_cci)):
        fitted_upper = (upper_array - (np.sqrt(rho) * monthly_cci[i])) / np.sqrt(1 - rho)
        monthly_fitted.append(fitted_upper)

    monthly_fitted = np.vstack(monthly_fitted)

    #Fitted matrix

    m_0 = 1 - norm.cdf(monthly_fitted[0:, 1])
    m_1 = norm.cdf(monthly_fitted[0:, 1]) - norm.cdf(monthly_fitted[0:, 2])
    m_2 = norm.cdf(monthly_fitted[0:, 2]) - norm.cdf(monthly_fitted[0:, 3])
    m_3 = norm.cdf(monthly_fitted[0:, 3]) - norm.cdf(monthly_fitted[0:, 4])
    m_4 = norm.cdf(monthly_fitted[0:, 4])

    fitted_matrix = np.stack((m_0, m_1, m_2, m_3, m_4), axis = 1)
    fitted_matrix = fitted_matrix.reshape(cci['Monthkey'].max(), -1)

    error_matrix = n_array * ((monthly_array - fitted_matrix) ** 2) / (fitted_matrix * (1 - fitted_matrix))

    return np.var(error_matrix)

res = minimize(cci, x0 = [monthly_cci, rho], method='Nelder-Mead')

我收到错误消息

ValueError: setting an array element with a sequence.

【问题讨论】:

标签: python python-3.x optimization scipy solver


【解决方案1】:

代码还有其他问题我无法解决,因为我没有您的设置,但我可以帮助您解决一些错误。首先,在您的最小化函数cci 中,输入参数会立即被覆盖,这对最小化不起作用,因此我们将其删除。其次,minimize 将最小化标量参数。我假设monthly_cci 是您已经加载的一些数据,所以我们将其传递给minimizeargs。我已经编辑了您的代码以包含这些修复:

from scipy.optimize import minimize   
from scipy.stats import norm
import numpy as np

def cci(rho, monthly_cci):
    # we are minimising rho, so that is first argument
    # we pass through monthly_cci to args of minimise below
    
    # removed random variable creation
    upper_array = np.random.randn(40)

    monthly_fitted = []

    for i in range(0, len(monthly_cci)):
        fitted_upper = (upper_array - (np.sqrt(rho) * monthly_cci[i])) / np.sqrt(1 - rho)
        monthly_fitted.append(fitted_upper)

    monthly_fitted = np.vstack(monthly_fitted)

    #Fitted matrix

    m_0 = 1 - norm.cdf(monthly_fitted[0:, 1])
    m_1 = norm.cdf(monthly_fitted[0:, 1]) - norm.cdf(monthly_fitted[0:, 2])
    m_2 = norm.cdf(monthly_fitted[0:, 2]) - norm.cdf(monthly_fitted[0:, 3])
    m_3 = norm.cdf(monthly_fitted[0:, 3]) - norm.cdf(monthly_fitted[0:, 4])
    m_4 = norm.cdf(monthly_fitted[0:, 4])

    fitted_matrix = np.stack((m_0, m_1, m_2, m_3, m_4), axis = 1)
    fitted_matrix = fitted_matrix.reshape(cci['Monthkey'].max(), -1)

    error_matrix = n_array * ((monthly_array - fitted_matrix) ** 2) / (fitted_matrix * (1 - fitted_matrix))

    return np.var(error_matrix)

# use test data
monhtly_cci = np.random.randn(40)
# x0 are the parameters to be minimises
# other arguments are passed as args
res = minimize(cci, x0=[rho], method='Nelder-Mead', args=(monthly_cci,))

【讨论】:

  • 我收到错误 ValueError: 操作数无法与形状 (4,5) (40,) 一起广播。此外,我不想最小化 rho,但我需要通过不断更改 rho 来最小化 error_matrix 的方差。
  • 很遗憾,我没有你的数据,所以我无法重现整个示例,否则我不能对错误说太多。是的,没错,minimize 将通过优化rho 的值来最小化目标函数的输出,在这种情况下是error_matrix 的方差。
最近更新 更多