Esempio n. 1
0
/**
 * \brief Remove a variable from the current parameters and remove it's scaling and prior values
 *
 * This function will clear out a variable from the \c currentParams and also remove it's scale
 * factors and prior ranges.
 *
 * \param runState [in] The analysis information structure
 * \param ifo [in] The IFO data structure
 * \param var [in] The variable to remove
 *
 */
void remove_variable_and_prior( LALInferenceRunState *runState, LALInferenceIFOModel *ifo, const CHAR *var ){
  /* remove variable from currentParams */
  if( LALInferenceCheckVariable( runState->currentParams, var ) ){
    LALInferenceRemoveVariable( runState->currentParams, var );
  }
  else{
    fprintf(stderr, "Error... variable %s cannot be removed as it does not exist!\n", var);
    exit(3);
  }

  /* remove variable scale parameters from data */
  LALInferenceIFOModel *ifotemp = ifo;
  while ( ifotemp ){
    CHAR *varscale = XLALStringDuplicate( var );
    varscale = XLALStringAppend( varscale, "_scale" );

    if( LALInferenceCheckVariable( ifotemp->params, varscale ) ){
      LALInferenceRemoveVariable( ifotemp->params, varscale );
    }
    else{
      fprintf(stderr, "Error... variable %s cannot be removed as it does not exist!\n", varscale);
      exit(3);
    }

    varscale = XLALStringAppend( varscale, "_min" );
    if( LALInferenceCheckVariable( ifotemp->params, varscale ) ){
      LALInferenceRemoveVariable( ifotemp->params, varscale );
    }
    else{
      fprintf(stderr, "Error... variable %s cannot be removed as it does not exist!\n", varscale);
      exit(3);
    }

    ifotemp = ifotemp->next;
  }

  /* remove prior */
  if ( LALInferenceCheckGaussianPrior( runState->priorArgs, var ) ){
    LALInferenceRemoveGaussianPrior( runState->priorArgs, var );
  }
  else { LALInferenceRemoveMinMaxPrior( runState->priorArgs, var ); }
}
Esempio n. 2
0
/**
 * \brief Add a variable, checking first if it already exists and is of type \c LALINFERENCE_PARAM_FIXED
 * and if so removing it before re-adding it
 *
 * This function is for use as an alternative to \c LALInferenceAddVariable, which does not
 * allow \c LALINFERENCE_PARAM_FIXED variables to be changed. If the variable already exists
 * and is of type  \c LALINFERENCE_PARAM_FIXED, then it will first be removed and then re-added
 */
void check_and_add_fixed_variable( LALInferenceVariables *vars, const char *name, void *value, LALInferenceVariableType type ){
   if ( LALInferenceCheckVariable( vars, name ) ){
     if ( LALInferenceGetVariableVaryType( vars, name ) == LALINFERENCE_PARAM_FIXED ) { LALInferenceRemoveVariable( vars, name ); }
   }
   LALInferenceAddVariable( vars, name, value, type, LALINFERENCE_PARAM_FIXED );
}
int LALInferenceDrawFromPriorTest(void)
{
	TEST_HEADER();

	int errnum;
	const char *name;
	gsl_rng *rng = gsl_rng_alloc(gsl_rng_default);
	gsl_rng_set(rng, 0);

	LALInferenceVariables *output = XLALCalloc(1, sizeof(LALInferenceVariables));
	LALInferenceVariables *priorArgs = XLALCalloc(1, sizeof(LALInferenceVariables));

	// Null reference checks.
	int outcome = 1;
	XLAL_TRY(LALInferenceDrawFromPrior(NULL, priorArgs, rng), errnum);
	outcome &= errnum == XLAL_EFAULT;
	XLAL_TRY(LALInferenceDrawFromPrior(output, NULL, rng), errnum);
	outcome &= errnum == XLAL_EFAULT;
	XLAL_TRY(LALInferenceDrawFromPrior(output, priorArgs, NULL), errnum);
	outcome &= errnum == XLAL_EFAULT;
	if (!outcome)
		TEST_FAIL("Null reference check failed.");

	int i;
	const char *varyName=NULL;
	char caseTag[VARNAME_MAX];
	LALInferenceVariableType type = LALINFERENCE_REAL8_t;
	LALInferenceParamVaryType vary=-1;
	for (i = 0; i < 2; i++)
	{
		switch (i)
		{
			case 0:
				vary = LALINFERENCE_PARAM_LINEAR;
				varyName = "linear";
				break;
			case 1:
				vary = LALINFERENCE_PARAM_CIRCULAR;
				varyName = "circular";
				break;
		}
		sprintf(caseTag, "[%s] ", varyName);

		// Try and generate some normally distributed variables for various mu and sigma.
		REAL8 gaussian = 0;
		REAL8 mu;
		REAL8 sigma;
		name = "gaussian";
		LALInferenceAddVariable(output, name, &gaussian, type, vary);

		// Zero standard deviation; should always equal mean.
		mu = -50;
		sigma = 0;
		LALInferenceRemoveGaussianPrior(priorArgs, name);
		LALInferenceAddGaussianPrior(priorArgs, name, &mu, &sigma, type);
		XLAL_TRY(LALInferenceDrawFromPrior(output, priorArgs, rng), errnum);
		gaussian = *(REAL8*)LALInferenceGetVariable(output, name);
		if (errnum != XLAL_SUCCESS)
		{
			TEST_FAIL("%sFailed to generate Gaussian variable; XLAL error: %s.", caseTag, XLALErrorString(errnum));
		}
		else if (!compareFloats(gaussian, mu, EPSILON))
		{
			TEST_FAIL("%sGaussian variable with zero standard deviation did not match the mean; X = %f, mu = %f.", caseTag, gaussian, mu);
		}

		LALInferenceRemoveVariable(output, name);
		LALInferenceRemoveGaussianPrior(priorArgs, name);

		// Try a uniform variable!
		REAL8 uniform = 0;
		REAL8 min;
		REAL8 max;
		name = "uniform";
		LALInferenceAddVariable(output, name, &uniform, type, vary);

		min = -1;
		max = 1;
		LALInferenceRemoveMinMaxPrior(priorArgs, name);
		LALInferenceAddMinMaxPrior(priorArgs, name, &min, &max, type);
		XLAL_TRY(LALInferenceDrawFromPrior(output, priorArgs, rng), errnum);
		if (errnum != XLAL_SUCCESS)
			TEST_FAIL("%sFailed to generate uniform variable; XLAL error: %s.", caseTag, XLALErrorString(errnum));

		LALInferenceRemoveVariable(output, name);
		LALInferenceRemoveMinMaxPrior(priorArgs, name);

		// Try a correlated variable!
		REAL8 correlated = 0;
		UINT4 idx = 0;
		name = "correlated";
		gsl_matrix *covariance = gsl_matrix_calloc(3, 3);
		LALInferenceAddVariable(output, name, &correlated, type, vary);
		LALInferenceRemoveCorrelatedPrior(priorArgs);

		// See what happens when we try to add a non-positive-definite covariance matrix
                gsl_matrix_set(covariance, 0, 0, -1);
                XLAL_TRY(LALInferenceAddCorrelatedPrior(priorArgs, name, &covariance, &mu, &sigma, &idx), errnum);
                if (errnum == XLAL_SUCCESS)
                        TEST_FAIL("%sNon-positive-definite covariance matrix was not rejected.", caseTag);
                LALInferenceRemoveCorrelatedPrior(priorArgs);

		// Now try a positive-semi-definite matrix; this should be accepted
                covariance = gsl_matrix_calloc(3, 3);
                gsl_matrix_set(covariance, 0, 0, 1);
                XLAL_TRY(LALInferenceAddCorrelatedPrior(priorArgs, name, &covariance, &mu, &sigma, &idx), errnum);
                if (errnum != XLAL_SUCCESS)
                        TEST_FAIL("%sCould not add semi-positive-definite covariance matrix.", caseTag);
		LALInferenceRemoveCorrelatedPrior(priorArgs);

		// Try a legitimate positive-definite covariance matrix.
                covariance = gsl_matrix_calloc(3, 3);
		gsl_matrix_set(covariance, 0, 0, 2);
		gsl_matrix_set(covariance, 0, 1, 1);
		gsl_matrix_set(covariance, 0, 2, 0);
		gsl_matrix_set(covariance, 1, 0, 1);
		gsl_matrix_set(covariance, 1, 1, 5);
		gsl_matrix_set(covariance, 1, 2, 1);
		gsl_matrix_set(covariance, 2, 0, 0);
		gsl_matrix_set(covariance, 2, 1, 1);
		gsl_matrix_set(covariance, 2, 2, 1);
                XLAL_TRY(LALInferenceAddCorrelatedPrior(priorArgs, name, &covariance, &mu, &sigma, &idx), errnum);
                if (errnum != XLAL_SUCCESS)
                        TEST_FAIL("%sCould not add correlated prior.", caseTag);
                XLAL_TRY(LALInferenceDrawFromPrior(output, priorArgs, rng), errnum);
		if (errnum != XLAL_SUCCESS)
			TEST_FAIL("%sCould not generate correlated variable from positive-definite matrix; XLAL error: %s.", caseTag, XLALErrorString(errnum));

		LALInferenceRemoveVariable(output, name);
		LALInferenceRemoveCorrelatedPrior(priorArgs);

		//gsl_matrix_free(covariance);
		LALInferenceRemoveVariable(output, "gaussian");
		LALInferenceRemoveVariable(output, "uniform");
		LALInferenceRemoveVariable(output, "correlated");
	}

	XLALFree(output);
	XLALFree(priorArgs);
	TEST_FOOTER();
}