44"""
55import numba
66import numpy as np
7- import scipy .optimize as opt
8-
9- from ruspy .estimation .bootstrapping import bootstrapp
7+ import pandas as pd
8+ from estimagic .optimization .optimize import minimize
109
1110
1211def estimate_transitions (df ):
@@ -29,25 +28,34 @@ def estimate_transitions(df):
2928 usage = df ["usage" ].to_numpy (dtype = float )
3029 usage = usage [~ np .isnan (usage )].astype (int )
3130 result_transitions ["trans_count" ] = transition_count = np .bincount (usage )
32- raw_result_trans = opt .minimize (
33- loglike_trans ,
34- args = transition_count ,
35- x0 = np .full (len (transition_count ), 0.1 ),
36- method = "BFGS" ,
37- )
38- p_raw = raw_result_trans ["x" ]
39- result_transitions ["x" ] = reparam_trans (p_raw )
4031
41- result_transitions ["95_conf_interv" ], result_transitions ["std_errors" ] = bootstrapp (
42- p_raw , raw_result_trans ["hess_inv" ], reparam = reparam_trans
32+ # Prepare DataFrame for estimagic
33+ name = ["trans_prob" ]
34+ number = np .arange (1 , len (transition_count ) + 1 )
35+ index = pd .MultiIndex .from_product ([name , number ], names = ["name" , "number" ])
36+ params = pd .DataFrame (
37+ np .full (len (transition_count ), 1 / len (transition_count )),
38+ columns = ["value" ],
39+ index = index ,
40+ )
41+ constr = [{"loc" : "trans_prob" , "type" : "probability" }]
42+
43+ raw_result_trans = minimize (
44+ criterion = loglike_trans ,
45+ params = params ,
46+ algorithm = "scipy_L-BFGS-B" ,
47+ constraints = constr ,
48+ criterion_kwargs = {"transition_count" : transition_count },
49+ logging = False ,
4350 )
4451
45- result_transitions ["fun" ] = loglike_trans (p_raw , transition_count )
52+ result_transitions ["x" ] = raw_result_trans [1 ]["value" ].to_numpy ()
53+ result_transitions ["fun" ] = raw_result_trans [0 ]["fitness" ]
4654
4755 return result_transitions
4856
4957
50- def loglike_trans (p_raw , transition_count ):
58+ def loglike_trans (params , transition_count ):
5159 """
5260 Log-likelihood function of transition probability estimation.
5361
@@ -65,31 +73,11 @@ def loglike_trans(p_raw, transition_count):
6573 log_like : numpy.float
6674 The negative log-likelihood value of the transition probabilities
6775 """
68- trans_probs = reparam_trans ( p_raw )
69- log_like = - np .sum (np .multiply (transition_count , np .log (trans_probs )))
76+ p_raw = params . loc [ "trans_prob" , "value" ]. to_numpy ( )
77+ log_like = - np .sum (np .multiply (transition_count , np .log (p_raw )))
7078 return log_like
7179
7280
73- def reparam_trans (p_raw ):
74- """
75- The reparametrization function for transition probabilities.
76-
77- Parameters
78- ----------
79- p_raw : numpy.array
80- The raw values before reparametrization, on which there are no constraints
81- or bounds.
82-
83- Returns
84- -------
85- trans_prob : numpy.array
86- The probabilities of an state increase.
87-
88- """
89- p = np .exp (p_raw ) / np .sum (np .exp (p_raw ))
90- return p
91-
92-
9381@numba .jit (nopython = True )
9482def create_transition_matrix (num_states , trans_prob ):
9583 """
0 commit comments