首页 > 解决方案 > Python最小化目标函数来估计模型参数

问题描述

我已经开发了标准 Bass 模型的扩展,现在我想使用最小化来估计这个模型的参数。

模型为:SVe(t) = θ[t-tv] * { p*m + (qp) * ∑SVe(t-1) + (q/m) * ∑SVe(t-1)^2 }

目标函数是:最小化 ∑(SVr - Sve)^2

SVr 是一个包含每日 Youtube 视频观看量的测量数据的数组。SVe 是我的估计视图。我的目标是估计 Sve,然后使用最小化来接近真实值。

θ[t-tv] 是一个重载函数。这也是我的输入,看起来像:

[[0,1,1,1,1,1][0,0,0,1,1,1][0,0,0,0,1,1]]

p、q 和 m 是我感兴趣的参数,我想估计它们。p 和 q 介于 0 和 1 之间,m 是一个很大的正数。∑SVe(t-1) 是前几期的累积视​​图。

我的想法是定义一个包含模型的函数和一个我使用最小化的目标函数。

import numpy as np
from scipy.optimize import minimize
SV = np.array([100,10000,1000])
heavi = np.array([[0,1,1,1],[0,0,1,1],[0,0,0,1]])

def model(x):
    p = x[0] 
    q = x[1]
    m = x[2]

    SVe = sum(heavi * (p*m+(q-p)*sum(SVe[:-1])+(q/m)*(sum(SVe[:-1])**2))

    return SVe

def objective(SVr):

    #Somehow Call model and compare results, then do it again until res is close to 0

    return sum(SVr - SVe)**2


x0 = np.array([0.1, 0.1, 10000])

b1 = (0,1)
b2 = (1,1000000000)              
bnds = (b1,b1,b2)               
res = minimize(objective, x0, method='SLSQP',bounds = bnds)

print(res)

这只是我的想法的一个代码示例,它不起作用。你将如何解决这个问题?如何将我的模型与我的目标联系起来,以便它一遍又一遍地重新估计,直到找到一个接近的解决方案?请随时询问更多信息。

标签: python-3.xnumpyoptimizationestimationminimization

解决方案


This somehow looks a bit like a homework assignment... ;)

From your code sample it seems that you may be struggling with the idea of functions (in the sense of functions in programming, not in mathematics) and how their arguments work and how arguments are passed to functions, i.e. how you call a function. I suggest you look up the documentation of scipys minimize function and try to find out how you should design your objective function and its arguments. Inside of your objective function you can then call your model function and calculate the objective value.


推荐阅读