在实际应用中,数据分析工程师获得的只是数据,而不清楚数据的产生机理。对于0-1二分类响应变量 Y,Logistic回归是非常常用的分类建模方法。对于从模型(1)中产生的独立同分布观测样本{(x_i,y_i),i = 1,⋯,n}.,建立Logistic 回归模型用信赖域算法和局部二次似编程实现β的Logistic回归估计python代码
时间: 2023-12-10 13:42:10 浏览: 62
基于java的房地产销售管理系统的开题报告.docx
以下是使用信赖域算法和局部二次似编程实现Logistic回归估计的Python代码:
```python
import numpy as np
from scipy.optimize import minimize
# sigmoid函数
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# Logistic回归模型
def logistic_model(beta, X):
return sigmoid(np.dot(X, beta))
# 对数似然函数
def log_likelihood(beta, X, y):
p = logistic_model(beta, X)
return np.sum(y*np.log(p) + (1-y)*np.log(1-p))
# 对数似然函数的梯度
def log_likelihood_gradient(beta, X, y):
p = logistic_model(beta, X)
return np.dot(X.T, y-p)
# 对数似然函数的海森矩阵
def log_likelihood_hessian(beta, X, y):
p = logistic_model(beta, X)
W = np.diag(p*(1-p))
return np.dot(np.dot(X.T, W), X)
# 信赖域算法
def trust_region_method(beta_init, X, y):
# 定义初始步长
delta = 1.0
# 定义信赖域半径
rho = 0.5
# 定义最大迭代次数
max_iter = 100
beta = beta_init
for i in range(max_iter):
# 计算梯度和海森矩阵
grad = log_likelihood_gradient(beta, X, y)
hess = log_likelihood_hessian(beta, X, y)
# 求解步长
p, success = minimize(lambda x: -log_likelihood(x, X, y), grad, method='Newton-CG', jac=log_likelihood_gradient, hess=log_likelihood_hessian, options={'xtol': 1e-8, 'disp': False})
# 计算预测值
p = logistic_model(beta+p, X)
# 计算实际下降量
actual_reduction = log_likelihood(beta+p, X, y) - log_likelihood(beta, X, y)
# 计算模型预测下降量
predicted_reduction = -np.dot(grad, p) - 0.5*np.dot(np.dot(p, hess), p)
# 计算rho
rho = actual_reduction / predicted_reduction
# 根据rho更新beta
if rho < 0.25:
delta = 0.25 * delta
else:
if rho > 0.75 and np.abs(np.linalg.norm(p) - delta) < 1e-8:
delta = min(2.0*delta, 1000)
if rho > 0:
beta = beta + p
return beta
# 局部二次似方法
def local_quadratic_method(beta_init, X, y):
# 定义最大迭代次数
max_iter = 100
beta = beta_init
for i in range(max_iter):
# 计算梯度和海森矩阵
grad = log_likelihood_gradient(beta, X, y)
hess = log_likelihood_hessian(beta, X, y)
# 求解步长
p = np.linalg.solve(hess, -grad)
# 计算预测值
p = logistic_model(beta+p, X)
# 计算实际下降量
actual_reduction = log_likelihood(beta+p, X, y) - log_likelihood(beta, X, y)
# 计算模型预测下降量
predicted_reduction = -np.dot(grad, p) - 0.5*np.dot(np.dot(p, hess), p)
# 计算alpha
alpha = actual_reduction / predicted_reduction
# 根据alpha更新beta
beta = beta + alpha*p
return beta
```
阅读全文