Hi! I want to minimize the square distance between two normal distributions of residuals for two different cross-sections (t and t'). The first is the empirical one (in t'), and the second is constructed based on a CDF, which is a linear combination of two normal variables times a normal density. This is how one of the normal variable looks like:
gen g_t_prime = p*normalden(u_m, miu_1, sigma_1) + (1-p)*normalden(u_m, miu_2, sigma_2)
My set of parameters to be optimized are their respective means (miu_1 and miu_2) and standard deviations (sigma_1 and sigma_2) plus p, which is the probability of being one or the other normal density.
In order to calculate my parameters I have tried to use the optimize function in mata as follows:
mata:
mata set matastrict off
void eval0(todo, p, v, g, H)
{
v = sum(normalden(x_k) - sum((x_k-u_mean)/rho_final)*(p[1]*normalden(u_m, miu[1], sigma[1]) +
(1-p[1])*normalden(u_m, miu[2], sigma[2]) - p[1]*normalden(u_m_1, miu[1], sigma[1]) + (1-p[1])*normalden(u_m_1, miu[2], sigma[2]))/(u_m - u_m-1))^2
}
S = optimize_init()
optimize_init_which(S, "min")
optimize_init_evaluator(S, &eval0())
optimize_init_params(S, 0)
v = optimize(S)
}
Where u_m, u_m_1, u_mean and x_k are vectors of random variables with size 180000x1.
However, when I run the code I receive the following message from STATA: "3204 matrix found where scalar required"
I've alredy tried to perform a similar exercise with moptimize but I am not pretty sure which one to use and how to display my optimization problem.
I'll appreciate some help, thanks!!
gen g_t_prime = p*normalden(u_m, miu_1, sigma_1) + (1-p)*normalden(u_m, miu_2, sigma_2)
My set of parameters to be optimized are their respective means (miu_1 and miu_2) and standard deviations (sigma_1 and sigma_2) plus p, which is the probability of being one or the other normal density.
In order to calculate my parameters I have tried to use the optimize function in mata as follows:
mata:
mata set matastrict off
void eval0(todo, p, v, g, H)
{
v = sum(normalden(x_k) - sum((x_k-u_mean)/rho_final)*(p[1]*normalden(u_m, miu[1], sigma[1]) +
(1-p[1])*normalden(u_m, miu[2], sigma[2]) - p[1]*normalden(u_m_1, miu[1], sigma[1]) + (1-p[1])*normalden(u_m_1, miu[2], sigma[2]))/(u_m - u_m-1))^2
}
S = optimize_init()
optimize_init_which(S, "min")
optimize_init_evaluator(S, &eval0())
optimize_init_params(S, 0)
v = optimize(S)
}
Where u_m, u_m_1, u_mean and x_k are vectors of random variables with size 180000x1.
However, when I run the code I receive the following message from STATA: "3204 matrix found where scalar required"
I've alredy tried to perform a similar exercise with moptimize but I am not pretty sure which one to use and how to display my optimization problem.
I'll appreciate some help, thanks!!
Comment