Commit bd626b07 authored by Houtan Bastani's avatar Houtan Bastani
Browse files

replace DOS ending with UNIX,

parent d265fcba
//== Flat Independent Markov States and Simple Restrictions ==//
//-----------------------------------------------------------------------------//
//-- Read by CreateMarkovStateVariable_File() only if the passed number of --//
//-- observations is less than or equal to zero. Can be omitted if the --//
//-- passed number of observations is positive. --//
//-----------------------------------------------------------------------------//
//== Number Observations ==//
200
//== Number Independent State Variables ==//
2
//-----------------------------------------------------------------------------//
//-- state_variable[1] (1 <= i <= n_state_variables) --//
//-----------------------------------------------------------------------------//
//== Number of states for state_variable[1] ==//
3
//-----------------------------------------------------------------------------//
//-- Each column contains the parameters for a Dirichlet prior on the --//
//-- corresponding column of the transition matrix. Each element must be --//
//-- positive. For each column, the relative size of the prior elements --//
//-- determine the relative size of the elements of the transition matrix --//
//-- and overall larger sizes implies a tighter prior. --//
//-----------------------------------------------------------------------------//
//== Transition matrix prior for state_variable[1]. (n_states x n_states) ==//
10 1 1
1 10 1
1 1 10
//-----------------------------------------------------------------------------//
//-- An array of n_states integers with each entry between 1 and n_states, --//
//-- inclusive. Determines the number of quasi-free Dirichlet dimensions --//
//-- each column. Since the sum of the elements in a Dirichlet distribution --//
//-- must equal to one, the actual number of free dimensions is one less. --//
//-----------------------------------------------------------------------------//
//== Free Dirichet dimensions for state_variable[1] ==//
3 3 3
//-----------------------------------------------------------------------------//
//-- The jth restriction matrix is n_states x free[j]. Each row of the --//
//-- restriction matrix has at most one non-zero entry and the sum of each --//
//-- column of the restriction matrix must be one. If (x(1),...,x(free[j])) --//
//-- is the Dirichlet random variable for column j, then the jth column of --//
//-- the transition matrix Q is the jth restriction matrix times the --//
//-- Dirichlet random random variable. --//
//-----------------------------------------------------------------------------//
//== Column restrictions for state_variable[1] ==//
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
//-----------------------------------------------------------------------------//
//-- Allows for lagged values of the state variable to be encoded --//
//-----------------------------------------------------------------------------//
//== Number of lags encoded for state_variable[1] ==//
2
//-----------------------------------------------------------------------------//
//-- state_variable[2] --//
//-----------------------------------------------------------------------------//
//== Number of states for state_variable[2] ==//
2
//-----------------------------------------------------------------------------//
//-- Each column contains the parameters for a Dirichlet prior on the --//
//-- corresponding column of the transition matrix. Each element must be --//
//-- positive. For each column, the relative size of the prior elements --//
//-- determine the relative size of the elements of the transition matrix --//
//-- and overall larger sizes implies a tighter prior. --//
//-----------------------------------------------------------------------------//
//== Transition matrix prior for state_variable[2]. (n_states x n_states) ==//
5 1
1 5
//-----------------------------------------------------------------------------//
//-- An array of n_states integers with each entry between 1 and n_states, --//
//-- inclusive. Determines the number of quasi-free Dirichlet dimensions --//
//-- each column. Since the sum of the elements in a Dirichlet distribution --//
//-- must equal to one, the actual number of free dimensions is one less. --//
//-----------------------------------------------------------------------------//
//== Free Dirichet dimensions for state_variable[2] ==//
2 2
//-----------------------------------------------------------------------------//
//-- The jth restriction matrix is n_states x free[j]. Each row of the --//
//-- restriction matrix has at most one non-zero entry and the sum of each --//
//-- column of the restriction matrix must be one. If (x(1),...,x(free[j])) --//
//-- is the Dirichlet random variable for column j, then the jth column of --//
//-- the transition matrix Q is the jth restriction matrix times the --//
//-- Dirichlet random random variable. --//
//-----------------------------------------------------------------------------//
//== Column restrictions for state_variable[2] ==//
1 0
0 1
1 0
0 1
//-----------------------------------------------------------------------------//
//-- Allows for lagged values of the state variable to be encoded --//
//-----------------------------------------------------------------------------//
//== Number of lags encoded for state_variable[2] ==//
0
//== Flat Independent Markov States and Simple Restrictions ==//
//-----------------------------------------------------------------------------//
//-- Read by CreateMarkovStateVariable_File() only if the passed number of --//
//-- observations is less than or equal to zero. Can be omitted if the --//
//-- passed number of observations is positive. --//
//-----------------------------------------------------------------------------//
//== Number Observations ==//
200
//== Number Independent State Variables ==//
2
//-----------------------------------------------------------------------------//
//-- state_variable[1] (1 <= i <= n_state_variables) --//
//-----------------------------------------------------------------------------//
//== Number of states for state_variable[1] ==//
3
//-----------------------------------------------------------------------------//
//-- Each column contains the parameters for a Dirichlet prior on the --//
//-- corresponding column of the transition matrix. Each element must be --//
//-- positive. For each column, the relative size of the prior elements --//
//-- determine the relative size of the elements of the transition matrix --//
//-- and overall larger sizes implies a tighter prior. --//
//-----------------------------------------------------------------------------//
//== Transition matrix prior for state_variable[1]. (n_states x n_states) ==//
10 1 1
1 10 1
1 1 10
//-----------------------------------------------------------------------------//
//-- An array of n_states integers with each entry between 1 and n_states, --//
//-- inclusive. Determines the number of quasi-free Dirichlet dimensions --//
//-- each column. Since the sum of the elements in a Dirichlet distribution --//
//-- must equal to one, the actual number of free dimensions is one less. --//
//-----------------------------------------------------------------------------//
//== Free Dirichet dimensions for state_variable[1] ==//
3 3 3
//-----------------------------------------------------------------------------//
//-- The jth restriction matrix is n_states x free[j]. Each row of the --//
//-- restriction matrix has at most one non-zero entry and the sum of each --//
//-- column of the restriction matrix must be one. If (x(1),...,x(free[j])) --//
//-- is the Dirichlet random variable for column j, then the jth column of --//
//-- the transition matrix Q is the jth restriction matrix times the --//
//-- Dirichlet random random variable. --//
//-----------------------------------------------------------------------------//
//== Column restrictions for state_variable[1] ==//
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
//-----------------------------------------------------------------------------//
//-- Allows for lagged values of the state variable to be encoded --//
//-----------------------------------------------------------------------------//
//== Number of lags encoded for state_variable[1] ==//
2
//-----------------------------------------------------------------------------//
//-- state_variable[2] --//
//-----------------------------------------------------------------------------//
//== Number of states for state_variable[2] ==//
2
//-----------------------------------------------------------------------------//
//-- Each column contains the parameters for a Dirichlet prior on the --//
//-- corresponding column of the transition matrix. Each element must be --//
//-- positive. For each column, the relative size of the prior elements --//
//-- determine the relative size of the elements of the transition matrix --//
//-- and overall larger sizes implies a tighter prior. --//
//-----------------------------------------------------------------------------//
//== Transition matrix prior for state_variable[2]. (n_states x n_states) ==//
5 1
1 5
//-----------------------------------------------------------------------------//
//-- An array of n_states integers with each entry between 1 and n_states, --//
//-- inclusive. Determines the number of quasi-free Dirichlet dimensions --//
//-- each column. Since the sum of the elements in a Dirichlet distribution --//
//-- must equal to one, the actual number of free dimensions is one less. --//
//-----------------------------------------------------------------------------//
//== Free Dirichet dimensions for state_variable[2] ==//
2 2
//-----------------------------------------------------------------------------//
//-- The jth restriction matrix is n_states x free[j]. Each row of the --//
//-- restriction matrix has at most one non-zero entry and the sum of each --//
//-- column of the restriction matrix must be one. If (x(1),...,x(free[j])) --//
//-- is the Dirichlet random variable for column j, then the jth column of --//
//-- the transition matrix Q is the jth restriction matrix times the --//
//-- Dirichlet random random variable. --//
//-----------------------------------------------------------------------------//
//== Column restrictions for state_variable[2] ==//
1 0
0 1
1 0
0 1
//-----------------------------------------------------------------------------//
//-- Allows for lagged values of the state variable to be encoded --//
//-----------------------------------------------------------------------------//
//== Number of lags encoded for state_variable[2] ==//
0
This diff is collapsed.
This diff is collapsed.
#include "switch_opt.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "modify_for_mex.h"
//====== Static Global Variables ======
static TStateModel *Model=(TStateModel*)NULL;
static PRECISION *buffer=(PRECISION*)NULL;
static PRECISION *ModifiedFreeParameters=(PRECISION*)NULL;
static PRECISION *FreeParameters_Q=(PRECISION*)NULL;
static int NumberFreeParameters_Q=0;
static PRECISION *FreeParameters_Theta=(PRECISION*)NULL;
static int NumberFreeParameters_Theta=0;
void SetupObjectiveFunction(TStateModel *model, PRECISION *Modified, PRECISION *FreeQ, PRECISION *FreeTheta)
{
if (buffer) free(buffer);
Model=model;
FreeParameters_Q=FreeQ;
NumberFreeParameters_Q=NumberFreeParametersQ(model);
FreeParameters_Theta=FreeTheta;
NumberFreeParameters_Theta=model->routines->pNumberFreeParametersTheta(model);
ModifiedFreeParameters=Modified;
}
void SetupObjectiveFunction_new(TStateModel *model, int FreeTheta_Idx, int FreeQ_Idx, int Modified_Idx)
{
if (buffer) free(buffer);
Model=model;
NumberFreeParameters_Q=NumberFreeParametersQ(model);
NumberFreeParameters_Theta=model->routines->pNumberFreeParametersTheta(model);
buffer=(PRECISION*)malloc((NumberFreeParameters_Q + NumberFreeParameters_Theta)*sizeof(PRECISION));
FreeParameters_Q=buffer+FreeQ_Idx;
FreeParameters_Theta=buffer+FreeTheta_Idx;
ModifiedFreeParameters=buffer+Modified_Idx;
}
PRECISION PosteriorObjectiveFunction(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogPosterior_StatesIntegratedOut(Model);
//PRECISION lp_Q, lp_Theta, li;
//FILE *f_out;
//lp_Q=LogPrior_Q(Model);
//lp_Theta=LogPrior_Theta(Model);
//li=LogLikelihood_StatesIntegratedOut(Model);
//if (isnan(lp_Q) || isnan(lp_Theta) || isnan(li))
// {
// f_out=fopen("tmp.tmp","wt");
// Write_VAR_Specification(f_out,(char*)NULL,Model);
// WriteTransitionMatrices(f_out,(char*)NULL,"Error: ",Model);
// Write_VAR_Parameters(f_out,(char*)NULL,"Error: ",Model);
// fprintf(f_out,"LogPrior_Theta(): %le\n",lp_Theta);
// fprintf(f_out,"LogPrior_Q(): %le\n",lp_Q);
// fprintf(f_out,"LogLikelihood_StatesIntegratedOut(): %le\n",li);
// fprintf(f_out,"Posterior: %le\n\n",lp_Q+lp_Theta+li);
// fclose(f_out);
// exit(0);
// }
//return -(lp_Q+lp_Theta+li);
}
PRECISION PosteriorObjectiveFunction_csminwel(double *x, int n, double **args, int *dims)
{
return PosteriorObjectiveFunction(x,n);
}
void PosteriorObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate)
{
*f=PosteriorObjectiveFunction(x,*n);
}
PRECISION MLEObjectiveFunction(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogLikelihood_StatesIntegratedOut(Model);
}
PRECISION MLEObjectiveFunction_csminwel(double *x, int n, double **args, int *dims)
{
return MLEObjectiveFunction(x,n);
}
void MLEObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate)
{
*f=MLEObjectiveFunction(x,*n);
}
PRECISION MLEObjectiveFunction_LogQ(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertLogFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogLikelihood_StatesIntegratedOut(Model);
}
PRECISION MLEObjectiveFunction_LogQ_csminwel(double *x, int n, double **args, int *dims)
{
return MLEObjectiveFunction_LogQ(x,n);
}
#include "switch_opt.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "modify_for_mex.h"
//====== Static Global Variables ======
static TStateModel *Model=(TStateModel*)NULL;
static PRECISION *buffer=(PRECISION*)NULL;
static PRECISION *ModifiedFreeParameters=(PRECISION*)NULL;
static PRECISION *FreeParameters_Q=(PRECISION*)NULL;
static int NumberFreeParameters_Q=0;
static PRECISION *FreeParameters_Theta=(PRECISION*)NULL;
static int NumberFreeParameters_Theta=0;
void SetupObjectiveFunction(TStateModel *model, PRECISION *Modified, PRECISION *FreeQ, PRECISION *FreeTheta)
{
if (buffer) free(buffer);
Model=model;
FreeParameters_Q=FreeQ;
NumberFreeParameters_Q=NumberFreeParametersQ(model);
FreeParameters_Theta=FreeTheta;
NumberFreeParameters_Theta=model->routines->pNumberFreeParametersTheta(model);
ModifiedFreeParameters=Modified;
}
void SetupObjectiveFunction_new(TStateModel *model, int FreeTheta_Idx, int FreeQ_Idx, int Modified_Idx)
{
if (buffer) free(buffer);
Model=model;
NumberFreeParameters_Q=NumberFreeParametersQ(model);
NumberFreeParameters_Theta=model->routines->pNumberFreeParametersTheta(model);
buffer=(PRECISION*)malloc((NumberFreeParameters_Q + NumberFreeParameters_Theta)*sizeof(PRECISION));
FreeParameters_Q=buffer+FreeQ_Idx;
FreeParameters_Theta=buffer+FreeTheta_Idx;
ModifiedFreeParameters=buffer+Modified_Idx;
}
PRECISION PosteriorObjectiveFunction(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogPosterior_StatesIntegratedOut(Model);
//PRECISION lp_Q, lp_Theta, li;
//FILE *f_out;
//lp_Q=LogPrior_Q(Model);
//lp_Theta=LogPrior_Theta(Model);
//li=LogLikelihood_StatesIntegratedOut(Model);
//if (isnan(lp_Q) || isnan(lp_Theta) || isnan(li))
// {
// f_out=fopen("tmp.tmp","wt");
// Write_VAR_Specification(f_out,(char*)NULL,Model);
// WriteTransitionMatrices(f_out,(char*)NULL,"Error: ",Model);
// Write_VAR_Parameters(f_out,(char*)NULL,"Error: ",Model);
// fprintf(f_out,"LogPrior_Theta(): %le\n",lp_Theta);
// fprintf(f_out,"LogPrior_Q(): %le\n",lp_Q);
// fprintf(f_out,"LogLikelihood_StatesIntegratedOut(): %le\n",li);
// fprintf(f_out,"Posterior: %le\n\n",lp_Q+lp_Theta+li);
// fclose(f_out);
// exit(0);
// }
//return -(lp_Q+lp_Theta+li);
}
PRECISION PosteriorObjectiveFunction_csminwel(double *x, int n, double **args, int *dims)
{
return PosteriorObjectiveFunction(x,n);
}
void PosteriorObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate)
{
*f=PosteriorObjectiveFunction(x,*n);
}
PRECISION MLEObjectiveFunction(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogLikelihood_StatesIntegratedOut(Model);
}
PRECISION MLEObjectiveFunction_csminwel(double *x, int n, double **args, int *dims)
{
return MLEObjectiveFunction(x,n);
}
void MLEObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate)
{
*f=MLEObjectiveFunction(x,*n);
}
PRECISION MLEObjectiveFunction_LogQ(PRECISION *x, int n)
{
if (x != ModifiedFreeParameters) memmove(ModifiedFreeParameters,x,n*sizeof(PRECISION));
ConvertLogFreeParametersToQ(Model,FreeParameters_Q);
ConvertFreeParametersToTheta(Model,FreeParameters_Theta);
return -LogLikelihood_StatesIntegratedOut(Model);
}
PRECISION MLEObjectiveFunction_LogQ_csminwel(double *x, int n, double **args, int *dims)
{
return MLEObjectiveFunction_LogQ(x,n);
}
#ifndef __MARKOV_SWITCHING_OPTIMIZATION__
#define __MARKOV_SWITCHING_OPTIMIZATION__
#include "switch.h"
void SetupObjectiveFunction(TStateModel *model, PRECISION *MFPparms, PRECISION *FTMparms, PRECISION *FMSparms);
PRECISION PosteriorObjectiveFunction(PRECISION *x, int n);
PRECISION PosteriorObjectiveFunction_csminwel(double *x, int n, double **args, int *dims);
void PosteriorObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate);
PRECISION MLEObjectiveFunction(PRECISION *x, int n);
PRECISION MLEObjectiveFunction_csminwel(double *x, int n, double **args, int *dims);
void MLEObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate);
PRECISION MLEObjectiveFunction_LogQ(PRECISION *x, int n);
PRECISION MLEObjectiveFunction_LogQ_csminwel(double *x, int n, double **args, int *dims);
#endif
#ifndef __MARKOV_SWITCHING_OPTIMIZATION__
#define __MARKOV_SWITCHING_OPTIMIZATION__
#include "switch.h"
void SetupObjectiveFunction(TStateModel *model, PRECISION *MFPparms, PRECISION *FTMparms, PRECISION *FMSparms);
PRECISION PosteriorObjectiveFunction(PRECISION *x, int n);
PRECISION PosteriorObjectiveFunction_csminwel(double *x, int n, double **args, int *dims);
void PosteriorObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate);
PRECISION MLEObjectiveFunction(PRECISION *x, int n);
PRECISION MLEObjectiveFunction_csminwel(double *x, int n, double **args, int *dims);
void MLEObjectiveFunction_npsol(int *mode, int *n, double *x, double *f, double *g, int *nstate);
PRECISION MLEObjectiveFunction_LogQ(PRECISION *x, int n);
PRECISION MLEObjectiveFunction_LogQ_csminwel(double *x, int n, double **args, int *dims);
#endif
#include "switch.h"
/*
Base routines for reading/writing Markov state variables and transition
matrices in native ascii format.
*/
TMarkovStateVariable* ReadMarkovSpecification_SV(FILE *f_in, char *idstring, int nobs);
int WriteMarkovSpecification_SV(FILE *f_out, TMarkovStateVariable *sv, char *idstring);
int ReadTransitionMatrices_SV(FILE *f_in, TMarkovStateVariable* sv, char *header, char *idstring);
int WriteTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable* sv, char *header, char *idstring);
int ReadBaseTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable *sv, char *header, char *idstring);
int WriteBaseTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable *sv, char *header, char *idstring);
int WriteBaseTransitionMatricesFlat_SV(FILE *f_out, TMarkovStateVariable *sv, char *fmt);
void WriteBaseTransitionMatricesFlat_Headers_SV(FILE *f_out, TMarkovStateVariable* sv, char *idstring);
/*
Routines for reading/writing Markov state variables and transition matrices
from TStateModel. Calls base routines.
*/
TMarkovStateVariable* ReadMarkovSpecification(FILE *f, char *filename);
int WriteMarkovSpecification(FILE *f, char *filename, TStateModel *model);
int ReadTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int WriteTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int ReadStates(FILE *f, char *filename, char *header, TStateModel *model);
int WriteStates(FILE *f, char *filename, char *header, TStateModel *model);
int ReadBaseTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int WriteBaseTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int WriteBaseTransitionMatricesFlat(FILE *f, TStateModel *model, char *fmt);
/*
Read flat markov state variable specification from file.
*/
TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, int nobs);
#include "switch.h"
/*
Base routines for reading/writing Markov state variables and transition
matrices in native ascii format.
*/
TMarkovStateVariable* ReadMarkovSpecification_SV(FILE *f_in, char *idstring, int nobs);
int WriteMarkovSpecification_SV(FILE *f_out, TMarkovStateVariable *sv, char *idstring);
int ReadTransitionMatrices_SV(FILE *f_in, TMarkovStateVariable* sv, char *header, char *idstring);
int WriteTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable* sv, char *header, char *idstring);
int ReadBaseTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable *sv, char *header, char *idstring);
int WriteBaseTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable *sv, char *header, char *idstring);
int WriteBaseTransitionMatricesFlat_SV(FILE *f_out, TMarkovStateVariable *sv, char *fmt);
void WriteBaseTransitionMatricesFlat_Headers_SV(FILE *f_out, TMarkovStateVariable* sv, char *idstring);
/*
Routines for reading/writing Markov state variables and transition matrices
from TStateModel. Calls base routines.
*/
TMarkovStateVariable* ReadMarkovSpecification(FILE *f, char *filename);
int WriteMarkovSpecification(FILE *f, char *filename, TStateModel *model);
int ReadTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int WriteTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);
int ReadStates(FILE *f, char *filename, char *header, TStateModel *model);
int WriteStates(FILE *f, char *filename, char *header, TStateModel *model);
int ReadBaseTransitionMatrices(FILE *f, char *filename, char *header, TStateModel *model);