nlm function equivalent in Python (Non-Linear-Minimization)
I test that if you use the same data
, you can get the similar minimum
.
In R:
data <- c(0L, 1L, 0L, 1L, 1L, 2L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 2L,
1L, 3L, 1L, 1L, 0L, 3L, 2L, 0L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 0L,
0L, 0L, 3L, 1L, 0L, 0L, 0L, 1L, 3L, 2L, 0L, 3L, 2L, 2L, 2L, 0L,
0L, 0L, 0L, 3L, 3L, 3L, 5L, 1L, 3L, 0L, 1L, 2L, 3L, 1L, 3L, 5L,
3L, 2L, 2L, 1L, 2L, 5L, 10L, 5L, 8L, 8L, 10L, 8L, 7L, 10L, 6L,
5L, 9L, 6L, 3L, 5L, 9L, 7L, 10L, 5L, 4L, 6L, 8L, 8L, 6L, 8L,
5L, 4L, 13L, 9L, 9L, 4L, 10L)
PAR <- c(0.693147180559945, 1.38629436111989, 1.94591014905531, 0.510825623765991,
-0.405465108108164)
f(PAR)
# 239.3489
Output:
res = nlm(f,PAR);
res
$minimum
[1] 228.2598
$estimate
[1] -1.3531276 0.6409517 1.9662910 -0.6879096 0.5427875
$gradient
[1] -1.228761e-05 -4.965273e-05 1.113862e-04 1.637090e-05 -2.287948e-05
$code
[1] 1
$iterations
[1] 32
In Python
data = np.array([0, 1, 0, 1, 1, 2, 1, 1, 0, 0, 1, 1, 0, 0, 2,
1, 3, 1, 1, 0, 3, 2, 0, 1, 2, 1, 2, 1, 3, 3, 0,
0, 0, 3, 1, 0, 0, 0, 1, 3, 2, 0, 3, 2, 2, 2, 0,
0, 0, 0, 3, 3, 3, 5, 1, 3, 0, 1, 2, 3, 1, 3, 5,
3, 2, 2, 1, 2, 5, 10, 5, 8, 8, 10, 8, 7, 10, 6,
5, 9, 6, 3, 5, 9, 7, 10, 5, 4, 6, 8, 8, 6, 8,
5, 4, 13, 9, 9, 4, 10])
PAR = np.array([ 0.69314718, 1.38629436, 1.94591015, 0.51082562, -0.40546511])
f(PAR)
#239.34891885662626
Output
res = minimize(f, PAR, method='Nelder-Mead', tol=1e-6);
res
final_simplex: (array([[-1.35304276, 0.64096297, 1.96629305, -0.68786541, 0.54278448],
[-1.35304245, 0.64096303, 1.96629305, -0.68786503, 0.54278438],
[-1.35304179, 0.64096308, 1.96629302, -0.68786502, 0.54278439],
[-1.35304231, 0.64096294, 1.96629301, -0.6878652 , 0.54278434],
[-1.35304278, 0.64096297, 1.966293 , -0.68786511, 0.5427845 ],
[-1.3530437 , 0.64096284, 1.96629297, -0.68786577, 0.54278438]]), array([228.25982274, 228.25982274, 228.25982274, 228.25982274,
228.25982274, 228.25982274]))
fun: 228.259822735201
message: 'Optimization terminated successfully.'
nfev: 768
nit: 478
status: 0
success: True
x: array([-1.35304276, 0.64096297, 1.96629305, -0.68786541, 0.54278448])