Ejemplo n.º 1
0
LPTable CreateMin(int m, LPTable parent)
{
	STable stab;
	
	if(!stab.Create(m, parent))
	{
		printf("Error creating STable :: CreateS.\n");
		return(NULL);
	}
	
	LPTable newtab = new Table;
	if(newtab == NULL)
	{
		printf("Error creating STable :: CreateS.\n");
		return(NULL);
	}
	
	if(!newtab->Alloc(parent->Dimens, parent->nDimens))
	{
		printf("Error creating STable :: CreateS.\n");
		return(NULL);
	}
	
	int NotFinished = 1;
	newtab->GetFirst();
	while(NotFinished)
	{
		newtab->Set(stab.GetMin(newtab->Index));
		NotFinished = newtab->GetNext();
	}
	
	stab.Reset();
	
	return newtab;
}
Ejemplo n.º 2
0
LPTable Table::ReduceOne(int ind)
{
	int i, j;
	int dim[MAXDIMENS]; //dimensions of the new table
	int ndim; //number of dimensions for the new table
	int NotFinished = 1;
	int* myIndex = new int[nDimens];CheckPointer(myIndex);
	memset(myIndex,0,nDimens*sizeof(int));
	double s;
	
	LPTable ctab = new Table;
	if(NULL == ctab)
	{
		printf("Error creating new table :: ReduceOne.\n");
		return(NULL);
	}
	ndim = nDimens - 1;
	j = 0;
	for(i=0; i<nDimens; i++)
	{
		if(i != ind)
		{
			dim[j] = Dimens[i];
			j++;
		}
	}
     //allocate storage for the new table
	ctab->Alloc(dim, ndim);
	ctab->GetFirst();
	while(NotFinished)
	{
		j = 0;
		for(i=0; i<nDimens; i++)
		{
			if(i != ind)
			{
				myIndex[i] = ctab->Index[j];
				j++;
			}
		}
		
		myIndex[ind] = 0;
		s = 0.0;
		
		for(i=0; i<Dimens[ind]; i++)
		{
			s += GetI(myIndex);
			myIndex[ind]++;
		}
		ctab->Set(s);
		NotFinished = ctab->GetNext();
	}
	delete[] myIndex;
	return(ctab);
}
Ejemplo n.º 3
0
void PfromTheta(LPTable P,LPTable Theta)
{
	int i;
	double s;
	double total;
	
	total = 0.0;
	P->GetFirst();
	while(P->GetNext())
	{
		Theta->GetFirst();
		s = 0.0;
		while(Theta->GetNext())
		{
			if(subset(Theta->nDimens,Theta->Index,P->Index))
			{
				s += Theta->Get();
			}
		}
		P->Set(exp(s));
		total += P->Get();
	}
	P->Data[0] = 1.0/(1.0+total);
	for(i=1;i<P->Total;i++)
	{
		P->Data[i] *= P->Data[0];
	}
	
	return;
}
Ejemplo n.º 4
0
double probYgivenG(LPTable Y,LPTable Theta,int* amodel)
{
	int i,j;
	double lprob = 0.0;
	double emptyterm;
	double s;
	
	for(i=1;i<Y->Total;i++)
	{
		if(amodel[i])
		{
			lprob += (Y->Data[i]*Theta->Data[i]);
		}
	}
	
	emptyterm = 1.0;
	
	Y->GetFirst();
	while(Y->GetNext())
	{
		int thereisone = 0;
		s = 0.0;
		j = 1;
		Theta->GetFirst();
		while(Theta->GetNext())
		{
			if(amodel[j])
			{
				if(subset(Theta->nDimens,Theta->Index,Y->Index))
				{
					s += Theta->Get();
					thereisone = 1;
				}
			}
			j++;
		}
		if(thereisone)
		{
			emptyterm += exp(s);
		}
	}	
	lprob = lprob-(Y->Data[0]*log(emptyterm));
	
	return(lprob);
}
Ejemplo n.º 5
0
int WriteBounds(LPTable UpperBound, LPTable LowerBound, const char* sFileName)
{
	if((UpperBound == NULL) || (LowerBound == NULL))
	{
		printf("Error :: WriteBounds.\n");
		return 0;
	}
	
	FILE* out;
	if(NULL == (out = fopen(sFileName, "w")))
	{
		printf("Could not open file %s!!\n", sFileName);
		return 0;
	}
	
	int i, j;
	int k = UpperBound->nDimens;
	int NotFinished = 1;
	
	UpperBound->GetFirst();
	for(i=0; i<UpperBound->Total; i++)
	{
		fprintf(out, "(");
		for(j=0; j<k-1; j++)
		{
			fprintf(out, "%d,", UpperBound->Index[j]+1);
		}
		fprintf(out, "%d) :: (%.2lf, %.2lf)\n",
			UpperBound->Index[k-1]+1,
			UpperBound->Data[i], LowerBound->Data[i]);
		
		if(NotFinished)
		{
			NotFinished = UpperBound->GetNext();
		}
	}
	
	fclose(out);
	
	return 1;
}
Ejemplo n.º 6
0
void MakeMarginals(LPTable Y,LPTable dataTable)
{
	int i;
	int lenC = dataTable->nDimens;
	int* C = new int[lenC];

	if(!Y->Alloc(dataTable->Dimens,dataTable->nDimens))
	{
		printf("Failed to allocate marginals table.\n"); exit(1);
	}

	Y->GetFirst();
	Y->Set(dataTable->GetGrandTotal());
	while(Y->GetNext())
	{
		lenC = 0;
		for(i=0;i<Y->nDimens;i++)
		{
			if(Y->Index[i])
			{
				C[lenC] = i;
				lenC++;
			}
		}
		LPNTable p = new NTable;
		p->Create(lenC,C,dataTable);
		Y->Set(p->Data[p->Total-1]);
		
		p->Reset(); delete p; p = NULL;
	}

	delete[] C; C = NULL;
	return;
}
Ejemplo n.º 7
0
//pind is a vector of length m of dimensions
//the dimensions in pind are in an increasing order
LPTable Table::Reduce(int m, int* pind)
{
	int i;
	LPTable newtab;
	LPTable oldtab;
	
	if(m == 0)
	{
		return NULL;
	}
	newtab = ReduceOne(pind[m-1]);
	if(m == 1)
	{
		return newtab;
	}
	for(i=m-2; i>=0; i--)
	{
		oldtab = newtab;
		newtab = oldtab->ReduceOne(pind[i]);
		oldtab->Reset();
		delete oldtab;
	}
	return newtab;
}
Ejemplo n.º 8
0
void InitPriorTable(LPTable prior,LPTable data,double cPrior)
{
	int i;
	
	if(!prior->Alloc(data->Dimens,data->nDimens))
	{
		printf("Failed to allocate prior table.\n"); exit(1);
	}
	
	double s = cPrior;
	if(s<0)
	{
		s = 1.0/((double)data->Total);
	}
	for(i=0;i<prior->Total;i++)
	{
		prior->Data[i] = s;
	}

	return;
}
Ejemplo n.º 9
0
int FrechetBounds1(LPTable table, const char* sFileName)
{
	if(table == NULL)
	{
		printf("Error :: FrechetBounds1.\n");
		return 0;
	}
	
	int k = table->nDimens;
	LPTable UpperBound = CreateMin(1, table);
	if(UpperBound == NULL)
	{
		printf("Error :: FrechetBounds1.\n");
		return 0;
	}
	
	LPTable LowerBound = CreateS(1, table);
	if(LowerBound == NULL)
	{
		printf("Error :: FrechetBounds1.\n");
		UpperBound->Reset();
		delete UpperBound;
		return 0;
	}
	
	double n = table->GetGrandTotal();
	int i;
	
	for(i=0; i<table->Total; i++)
	{
		LowerBound->Data[i] -= n*(k-1);
		if(LowerBound->Data[i] < 0.0)
		{
			LowerBound->Data[i] = 0.0;
		}
	}
	
	int rez = WriteBounds(UpperBound, LowerBound, sFileName);
	
	UpperBound->Reset();
	LowerBound->Reset();
	delete UpperBound;
	delete LowerBound;
	
	return rez;
}
Ejemplo n.º 10
0
void ThetafromP(LPTable theta,LPTable p,int* aModel)
{
	int i,j;
	double s;
	
	theta->GetFirst();
	theta->Set(log(p->Data[0]));
	j = 1;
	while(theta->GetNext())
	{
		if(aModel[j])
		{
			int lenE = 0;
			for(i=0;i<theta->nDimens;i++) lenE+=theta->Index[i];
		
			p->GetFirst();
			s = pow(-1,lenE)*log(p->Data[0]);
			while(p->GetNext())
			{
				if(subset(p->nDimens,p->Index,theta->Index))
				{
					int lenF = 0;
					for(i=0;i<p->nDimens;i++) lenF+=p->Index[i];
				
					s+= pow(-1.0,lenE-lenF)*log(p->Get());
				}
			}
		}
		else
		{
			s = 0;
		}
		theta->Set(s);
		j++;
	}

	return;
}
Ejemplo n.º 11
0
LPTable CreateNBar(LPTable parent)
{
	if(parent == NULL)
	{
		printf("Error :: CreateNBar.\n");
		return NULL;
	}
	
	LPTable newtab = new Table;
	if(newtab == NULL)
	{
		printf("Error :: CreateNBar\n");
		return(NULL);
	}
	
	if(!newtab->Alloc(parent->Dimens, parent->nDimens))
	{
		printf("Error creating STable :: CreateS.\n");
		return(NULL);
	}
	
	double n;
	n = parent->GetGrandTotal();
	int k;
	k = parent->nDimens;
	int m;
	int plus = -1; //we begin with '-'
	int first = 1; //first iteration?
	int i;
	
	for(m=1; m<k; m++)
	{
		LPTable smtab = CreateS(m, parent);
		if(smtab == NULL)
		{
			printf("Error :: CreateNBar.\n");
			return NULL;
		}
		
		if(first)
		{
			for(i=0; i<newtab->Total; i++)
			{
				newtab->Data[i] = 1 - smtab->Data[i]; //n
			}
			
			first = 0;
		}
		else //first == 0
		{
			if(plus == -1)
			{
				for(i=0; i<newtab->Total; i++)
				{
					newtab->Data[i] -= smtab->Data[i];
				}
			}
			else //plus == 1
			{
				for(i=0; i<newtab->Total; i++)
				{
					newtab->Data[i] += smtab->Data[i];
				}
			}
		}
		
		smtab->Reset();
		delete smtab;
		
		plus = - plus;
	}
	
	if(plus == -1)
	{
		for(i=0; i<newtab->Total; i++)
		{
			newtab->Data[i] -= parent->Data[i];
		}
	}
	else
	{
		for(i=0; i<newtab->Total; i++)
		{
			newtab->Data[i] += parent->Data[i];
		}
	}
	
	return newtab;
}
Ejemplo n.º 12
0
void ipf(int** VarSetsMarg,int* lenVarSetsMarg,int nVarSetsMarg,
         LPTable P,LPTable dataTable,int* aModelGenerators,int* aModel)
{
	int i,j,k;
	double delta = 0.0000001;
	const int PT = P->Total;
	double m[PT];
	double mold[PT];
	double s;
	
	int nMssShape = 0;
	for(i=0;i<nVarSetsMarg;i++)
	{
		nMssShape += aModelGenerators[i];
	}

	int* lenC = new int[nMssShape];
	int** C = new int*[nMssShape];
	for(i=0;i<nMssShape;i++)
	{
		C[i] = new int[dataTable->nDimens];
	}
	
	LPNTable margin = new NTable[nMssShape];
	nMssShape = 0;
	for(i=0;i<nVarSetsMarg;i++)
	{
		if(aModelGenerators[i])
		{
			k = 0;
			for(j=0;j<dataTable->nDimens;j++)
			{
				if(VarSetsMarg[i][j])
				{
					C[nMssShape][k]=j;
					k++;
				}
			}
			lenC[nMssShape] = k;
		
			margin[nMssShape].Create(lenC[nMssShape],C[nMssShape],dataTable);
			nMssShape++;
		}
	}
	
	LPTable Theta = new Table;
	if(!Theta->Alloc(dataTable->Dimens,dataTable->nDimens))
	{
		printf("Failed to allocate the theta's table.\n"); exit(1);
	}
	
	LPTable oldTheta = new Table;
	if(!oldTheta->Alloc(dataTable->Dimens,dataTable->nDimens))
	{
		printf("Failed to allocate the theta's table.\n"); exit(1);
	}
	
	//generate a random theta
	InitThetaTable(aModel,Theta);
	PfromTheta(P,Theta);
	
	int notdone = 1;
	while(notdone)
	{
		for(j=0;j<PT;j++)
		{
			mold[j] = P->Data[j];
			oldTheta->Data[j] = Theta->Data[j];
		}
		for(i=0;i<nMssShape;i++)
		{
			LPNTable p = new NTable;
			p->Create(lenC[i],C[i],P);
		
			P->GetFirst();
			j=0;
			m[j] = P->Data[j]*margin[i].GetI(P->Index)/p->GetI(P->Index);
			while(P->GetNext())
			{
				j++;
				m[j] = P->Data[j]*margin[i].GetI(P->Index)/p->GetI(P->Index);
			}
			
			for(j=0;j<PT;j++)
			{
				P->Data[j] = m[j]; 
			}
			
			p->Reset(); delete p ; p = NULL;

			s = 0.0;
			for(j=0;j<PT;j++)
			{
				s += P->Data[j];
			}
	
			for(j=0;j<PT;j++)
			{
				P->Data[j] /= s;
			}

			//transform in the space of thetas and impose the constraints
			ThetafromP(Theta,P,aModel);
			PfromTheta(P,Theta);
		}
	
		notdone = 0;
		for(i=0;i<PT;i++)
		{
			/*
			if(fabs(mold[i]-P->Data[i])>delta)
			{
				notdone = 1;
			}
			*/
			//printf("\t%.5lf",Theta->Data[i]);
			if(fabs(oldTheta->Data[i]-Theta->Data[i])>delta)
			{
				notdone = 1;
			}
		}
		//printf("\n");
	}
	
	s = 0.0;
	for(i=0;i<PT;i++)
	{
		s += P->Data[i];
	}
	
	for(i=0;i<PT;i++)
	{
		P->Data[i] /= s;
	}
	
	//clean memory
	oldTheta->Reset(); delete oldTheta; oldTheta = NULL;
	Theta->Reset(); delete Theta; Theta = NULL;
	for(i=0;i<nMssShape;i++)
	{
		margin[i].Reset();
	}
	delete[] margin; margin = NULL;
	for(i=0;i<nMssShape;i++)
	{
		delete[] C[i]; C[i] = NULL;
	}
	delete[] C; C = NULL;
	delete[] lenC; lenC = NULL;
	return;
}
Ejemplo n.º 13
0
LPTable ReduceOneShuttle(LPTable tab, int ind, LPTable tabS)
{
	LPTable tabR = tab->ReduceOne(ind);
	if(NULL == tabR)
	{
		printf("Error creating new table :: ReduceOneShuttle.\n");
		return(NULL);
	}
	
	LPTable rez = new Table;
	if(NULL == rez)
	{
		printf("Error creating new table :: ReduceOneShuttle.\n");
		tabR->Reset(); delete tabR;
		return(NULL);
	}
	
	if(!rez->Alloc(tab->Dimens, tab->nDimens))
	{
		printf("Error creating new table :: ReduceOneShuttle.\n");
		tabR->Reset(); delete tabR;
		delete rez;
		return(NULL);
	}
	
	rez->GetFirst();
	int i, j;
	int NotFinished = 1;
	while(NotFinished)
	{
		j = 0;
		for(i=0; i<rez->nDimens; i++)
		{
			if(i != ind)
			{
				tabR->Index[j] = rez->Index[i];
				j++;
				tabS->Index[i] = rez->Index[i];
			}
		}
		
		double s = 0.0;
		for(i=0; i<rez->Dimens[ind]; i++)
		{
			if(i != rez->Index[ind])
			{
				tabS->Index[ind] = i;
				s += tabS->Get();
			}
		}
		
		rez->Set(tabR->Get() - s);
		
		if(NotFinished)
		{
			NotFinished = rez->GetNext();
		}
	}
	
	tabR->Reset(); delete tabR;
	
	return rez;
}
Ejemplo n.º 14
0
void getNextTheta(int** VarSets,int* lenVarSets,int nVarSets,
                  int* amodel,int lenCL,int* CL,LPNTable smalltheta,LPTable Theta,LPTable nextTheta)
{
	int i,iF;
	int okay1;
	int iE, iL, iC;
	const int NTD = nextTheta->nDimens;
	int* CLcomplement = new int[NTD];
	int* CLfull = new int[NTD];
	int* FunionL = new int[NTD];
	const int TT = Theta->Total;
	double g[TT];
	int len[TT];
	
	for(i=0;i<NTD;i++)
	{
		CLcomplement[i] = 1;
		CLfull[i] = 0;
	}
	for(i=0;i<lenCL;i++)
	{
		CLcomplement[CL[i]] = 0;
		CLfull[CL[i]] = 1;
	}
	
	for(i=0;i<nextTheta->Total;i++)
	{
		nextTheta->Data[i] = Theta->Data[i];
		g[i] = 0;
		len[i]=0;
	}
	
	////////////////////////////////////////////////
	Theta->GetFirst();
	okay1 = 1;
	iF = 0;
	while(okay1)
	{
		len[iF] = lenVarSets[iF];
										
		double s1 = 1.0;
		for(iL=1;iL<nVarSets;iL++)
		{	
			if(subset(Theta->nDimens,VarSets[iL],CLcomplement))
			{
				for(i=0;i<Theta->nDimens;i++)
				{
					FunionL[i] = 0;
					if(VarSets[iL][i]==1) FunionL[i] = 1;
					if(Theta->Index[i]==1) FunionL[i] = 1;
				}
				
				int thereisone = 0;
				double sC = 0.0;
				for(iC=1;iC<nVarSets;iC++)
				{
					if(0==subset(Theta->nDimens,VarSets[iC],Theta->Index))
					{
						if(1==subset(Theta->nDimens,VarSets[iC],FunionL))
						{
							sC += Theta->GetI(VarSets[iC]);
							thereisone = 1;
						}
					}
				}
				if(thereisone)
				{
					s1 += exp(sC);
				}
			}
		}
		g[iF]=log(s1);
		iF++;
		okay1 = Theta->GetNext();
	}
	////////////////////////////////////////////////
	
	nextTheta->GetFirst();
	nextTheta->Set(0);
	iE = 0;
	while(nextTheta->GetNext())
	{
	    iE++;
		if(amodel[iE])
		{
		    if(1==subset(Theta->nDimens,nextTheta->Index,CLfull))
			{
				double s0 = smalltheta->GetI(nextTheta->Index);
				int lenE = 0;
				for(i=0;i<NTD;i++) lenE+=nextTheta->Index[i];
		
				Theta->GetFirst();
				int okay1 = 1;
				iF = 0;
				while(okay1)
				{
					if(len[iF]>=0)
					{
						if(subset(Theta->nDimens,Theta->Index,nextTheta->Index))
						{
							s0 += pow(-1,lenE-len[iF]-1)*g[iF];
						}
					}
					iF++;
					okay1 = Theta->GetNext();
				}
				nextTheta->Set(s0);
			}
			else
			{
				Theta->SetIndex(nextTheta->Index);
				nextTheta->Set(Theta->Get());
			}
		}
		else
		{
			nextTheta->Set(0);
		}
	}

	delete[] CLcomplement; CLcomplement = NULL;
	delete[] CLfull; CLfull = NULL;
	delete[] FunionL; FunionL = NULL;
	return;
}
Ejemplo n.º 15
0
int ShuttleBounds(LPTable table, int nIterations, const char* sFileName)
{
	int k = table->nDimens;
	LPTable UpperBound = CreateMin(k-1, table);
	if(UpperBound == NULL)
	{
		printf("Error :: ShuttleBounds.\n");
		return 0;
	}
	LPTable LowerBound = new Table;
	if(LowerBound == NULL)
	{
		printf("Error :: ShuttleBounds.\n");
		UpperBound->Reset(); delete UpperBound;
		return 0;
	}
	if(!LowerBound->Alloc(table->Dimens, table->nDimens))
	{
		printf("Error :: ShuttleBounds.\n");
		UpperBound->Reset(); delete UpperBound;
		return 0;
	}
	int i;
	for(i=0; i<table->Total; i++)
	{
		LowerBound->Data[i] = 0;
	}
	
	int ind;
	int iteration;
	for(iteration=0; iteration<nIterations; iteration++)
	{
		LPTable OldUpperBound = UpperBound;
		LPTable OldLowerBound = LowerBound;
		
		UpperBound = ReduceOneShuttle(table, 0, OldLowerBound);
		if(UpperBound == NULL)
		{
			OldUpperBound->Reset(); delete OldUpperBound;
			OldLowerBound->Reset(); delete OldLowerBound;
			return 0;
		}
		for(ind=1; ind<k; ind++)
		{
			LPTable newtab = ReduceOneShuttle(table, ind, OldLowerBound);
			if(newtab == NULL)
			{
				UpperBound->Reset();    delete UpperBound;
				OldUpperBound->Reset(); delete OldUpperBound;
				OldLowerBound->Reset(); delete OldLowerBound;
				return 0;
			}
			for(i=0; i<table->Total; i++)
			{
				if(newtab->Data[i] < UpperBound->Data[i])
				{
					UpperBound->Data[i] = newtab->Data[i];
				}
			}
			newtab->Reset(); delete newtab;
		}
		
		LowerBound = ReduceOneShuttle(table, 0, OldUpperBound);
		if(LowerBound == NULL)
		{
			UpperBound->Reset(); delete UpperBound;
			OldUpperBound->Reset(); delete OldUpperBound;
			OldLowerBound->Reset(); delete OldLowerBound;
			return 0;
		}
		
		for(ind=1; ind<k; ind++)
		{
			LPTable newtab = ReduceOneShuttle(table, ind, OldUpperBound);
			if(newtab == NULL)
			{
				UpperBound->Reset(); delete UpperBound;
				LowerBound->Reset(); delete LowerBound;
				OldUpperBound->Reset(); delete OldUpperBound;
				OldLowerBound->Reset(); delete OldLowerBound;
				return 0;
			}
			for(i=0; i<table->Total; i++)
			{
				if(newtab->Data[i] > LowerBound->Data[i])
				{
					LowerBound->Data[i] = newtab->Data[i];
				}
			}
			newtab->Reset(); delete newtab;
		}
		OldUpperBound->Reset(); delete OldUpperBound;
		OldLowerBound->Reset(); delete OldLowerBound;
	}
	int rez = WriteBounds(UpperBound, LowerBound, sFileName);
	UpperBound->Reset();
	LowerBound->Reset();
	delete UpperBound;
	delete LowerBound;
	return rez;
}
Ejemplo n.º 16
0
void CreateShape(int** VarSets,int* lenVarSets,int nVarSets,
				 int lenC,int* C,LPTable S,LPNTable shape)
{
	int i,k;
	const int SD = S->nDimens;
	int indexC[SD];
	int lenF;
	int lenD;
	double s;
	int okay;
	
	for(i=0;i<SD;i++)
	{
		indexC[i]=0;
	}
	for(i=0;i<lenC;i++)
	{
		indexC[C[i]] = 1;
	}
	
	for(i=0;i<shape->Total;i++)
	{
		shape->Data[i] = 0;
	}
			
	shape->GetFirst();
	s = 0.0;
	S->GetFirst();
	okay = 1;
	while(okay)
	{
		if(subset(SD,S->Index,indexC))
		{
			lenF = 0;
			for(k=0;k<SD;k++)
			{
				lenF += S->Index[k];
			}
			s += pow(-1.0,lenF)*S->Get();
		}
		okay = S->GetNext();
	}
	shape->Set(s);
	
	for(i=0;i<nVarSets;i++)
	{
		if(subset(SD,VarSets[i],indexC))
		{
			s = 0.0;
			S->GetFirst();
			okay = 1;
			while(okay)
			{
				if(subset(SD,S->Index,indexC))
				{
					if(subset(SD,VarSets[i],S->Index))
					{
						lenD = lenVarSets[i];
						lenF = 0;
						for(k=0;k<SD;k++)
						{
							lenF += S->Index[k];
						}
						s += pow(-1.0,lenF-lenD)*S->Get();
					}
				}
				okay = S->GetNext();
			}
			shape->SetIndex(VarSets[i]);
			shape->Set(s);
		}
	}
			
	return;
}
Ejemplo n.º 17
0
//reduce table by the dimensions NOT contained in c
//the vector c has length m
int NTable::Create(int m, int* c, LPTable table)
{
	int i, j, k;
	int index[MAXDIMENS];
	
	k = 0;
	for(i=0; i<c[0]; i++)
	{
		index[k] = i;
		k++;
	}
	j = 1;
	while(j < m)
	{
		for(i=c[j-1]+1; i<c[j]; i++)
		{
			index[k] = i;
			k++;
		}
		j++;
	}
	for(i=c[m-1]+1; i<table->nDimens; i++)
	{
		index[k] = i;
		k++;
	}
	
	if(k>=1)
	{
		LPTable newtab = table->Reduce(k, index);
		if(newtab == NULL)
		{
			printf("NTable :: Error in Create.\n");
			return 0;
		}
		if(!Alloc(newtab->Dimens, newtab->nDimens))
		{
			printf("NTable :: Could not allocate memory.\n");
			return 0;
		}
		for(i=0; i<Total; i++)
		{
			Data[i] = newtab->Data[i];
		}
		newtab->Reset(); delete newtab; newtab = NULL;
	}
	else
	{
		if(!Alloc(table->Dimens,table->nDimens))
		{
			printf("NTable :: Could not allocate memory.\n");
			return 0;
		}
		for(i=0;i<Total;i++)
		{
			Data[i] = table->Data[i];
		}
	}
	nParentDimens = table->nDimens;
	SetConvertIndex(c);
	return 1;
}
Ejemplo n.º 18
0
int Bonferroni(int m, LPTable table, const char* sFileName)
{
	if(table == NULL)
	{
		printf("Error :: BonferroniBounds.\n");
		return 0;
	}
	
	int i;
	double nGrandTotal;
	nGrandTotal = table->GetGrandTotal();
	
	for(i=0; i<table->Total; i++)
	{
		table->Data[i] /= nGrandTotal;
	}  
	
	table->WriteTable("tabinit.dat");
	
	LPTable UpperBound;
	LPTable LowerBound = new Table;
	if(LowerBound == NULL)
	{
		printf("Error :: BonferroniBounds.\n");
		return 0;
	}
	
	if(!LowerBound->Alloc(table->Dimens, table->nDimens))
	{
		printf("Error :: BonferroniBounds.\n");
		return 0;
	}
	
	LPTable nBar = CreateNBar(table);
	if(nBar == NULL)
	{
		printf("Error :: Bonferroni.\n");
		return 0;
	}
	
	nBar->WriteTable("nbar.dat");
	
	double nBarGrandTotal;
	nBarGrandTotal = nBar->GetGrandTotal();
	int m1;
	int plus;
	
	UpperBound = CreateS(1, nBar);
	if(UpperBound == NULL)
	{
		printf("Error :: Bonferroni.\n");
		nBar->Reset(); delete nBar;
		LowerBound->Reset(); delete LowerBound;
		return 0;
	}
	
	UpperBound->WriteTable("s1bar.dat");
	
	for(i=0; i<table->Total; i++)
	{
		UpperBound->Data[i] = 1 - UpperBound->Data[i]; //nBarGrandTotal
	}
	
	plus = 1;
	
	for(m1=2; m1<=m-1; m1++)
	{
		LPTable sm1 = CreateS(m1, nBar);
		if(sm1 == NULL)
		{
			printf("Error :: Bonferroni.\n");
			nBar->Reset(); delete nBar;
			UpperBound->Reset(); delete UpperBound;
			LowerBound->Reset(); delete LowerBound;
			return 0;
		}
		
		if(plus == 1)
		{
			for(i=0; i<table->Total; i++)
			{
				UpperBound->Data[i] += sm1->Data[i];
			}
		}
		else //plus == -1
		{
			for(i=0; i<table->Total; i++)
			{
				UpperBound->Data[i] -= sm1->Data[i];
			}
		}
		
		sm1->Reset(); delete sm1;
		plus = - plus;
	}
	
	for(i=0; i<table->Total; i++)
	{
		LowerBound->Data[i] = UpperBound->Data[i];
	}
	
	if(m < table->nDimens)
	{
		LPTable sm = CreateS(m, nBar);
		if(sm == NULL)
		{
			printf("Error :: Bonferroni.\n");
			nBar->Reset(); delete nBar;
			UpperBound->Reset(); delete UpperBound;
			LowerBound->Reset(); delete LowerBound;
			return 0;
		}
		
		if(plus == 1) //m even
		{
			for(i=0; i<table->Total; i++)
			{
				UpperBound->Data[i] += sm->Data[i];
			}
		}
		else //m odd
		{
			for(i=0; i<table->Total; i++)
			{
				LowerBound->Data[i] -= sm->Data[i];
			}
		}
		sm->Reset();
		delete sm;
	}
	else
	{
		if(plus == 1) //m even
		{
			for(i=0; i<table->Total; i++)
			{
				UpperBound->Data[i] += nBar->Data[i];
			}
		}
		else //m odd
		{
			for(i=0; i<table->Total; i++)
			{
				LowerBound->Data[i] -= nBar->Data[i];
			}
		}
	}
	
	WriteBounds(UpperBound, LowerBound, "extra.dat");
	
	double ratio = nGrandTotal; // /nBarGrandTotal;
	printf("ratio = %lf\n", ratio);
	for(i=0; i<table->Total; i++)
	{
		UpperBound->Data[i] = ceil(UpperBound->Data[i]*ratio);
		LowerBound->Data[i] = floor(LowerBound->Data[i]*ratio);
		if(LowerBound->Data[i] < 0)
		{
			LowerBound->Data[i] = 0;
		}
	}
	
	int rez = WriteBounds(UpperBound, LowerBound, sFileName);
	
	nBar->Reset();
	UpperBound->Reset();
	LowerBound->Reset();
	delete nBar;
	delete UpperBound;
	delete LowerBound;
	
	return rez;
}
Ejemplo n.º 19
0
void FindHierarchicalModels(CRegression* allregs,CData& Data,char* DataFileName,int target,int nMaxRegressors)
{
	int i,j;
	char buffer[2048];
	FILE* out = NULL;
	int responseVariable = target-1;
	int lenmodel = 1+nMaxRegressors + Data.NumOfConfoundingVars; // + all confounding vars
	int* amodel = new int[lenmodel];
	CRegression* p = allregs->Next;
	
	int modelid = 1;
	while(NULL!=p)
	{
		set<int>::iterator it;
		lenmodel = 0;
		for(it=(p->Vars).begin();it!=(p->Vars).end();it++)
		{
			amodel[lenmodel] = *it;
			lenmodel++;
		}

		// Next insert all the confounding vars, followed by the response var
	        j = Data.NumOfConfoundingVars;
        	for(i=0; i<=j; i++)
        	{
                	amodel[lenmodel] = responseVariable-j+i;
			lenmodel++;
        	}

		//amodel[lenmodel] = responseVariable;
		//lenmodel++;
		
		printf("Model [%d] ::",modelid);
		for(i=0;i<lenmodel;i++) printf(" %d",amodel[i]);
		printf("\n");
		
		//get the marginal table for these variables
		LPTable dataTable = new Table;
		int* index = new int[lenmodel];
		for(j=0;j<lenmodel;j++)
		{
			index[j] = 2;
		}
		if(!dataTable->Alloc(index,lenmodel))
		{
			printf("Error allocating memory!\n");
			exit(1);
		}
		for(i=0;i<Data.SampleSize;i++)
		{
			for(j=0;j<lenmodel;j++)
			{
				index[j] = (int) (Data.data[i][amodel[j]]);
			}
			dataTable->SetIndex(index);
			dataTable->Set(dataTable->Get()+1);
		}
		
		//file where the best model will be saved
		sprintf(buffer,"%s.shotgun.%d.%d.reg.model%d.txt",DataFileName,target,nMaxRegressors,modelid);
		if(NULL==(out=fopen(buffer,"w"))) 
		{
			printf("Cannot open file [%s]\n",buffer);
			return;
		}
		
		//do the shotgun search
		TableShotgunSearch(out, dataTable, model.mnShotgunChainReplicates,
			model.mdShotgunCutoffMax, model.mdShotgunCutoffMin, model.mdShotgunProbMax, Data.NumOfConfoundingVars);
		fclose(out);
		
		//clean memory
		dataTable->Reset();
		delete dataTable; dataTable = NULL;
		
		//go to the next model
		p = p->Next;
		modelid++;
	}
	//clean memory
	delete[] amodel; amodel = NULL;
	return;
}
Ejemplo n.º 20
0
void TableShotgunSearch(FILE* out,LPTable dataTable,
					    int ShotgunChainReplicates,double ShotgunCutoffMax,double ShotgunCutoffMin,double ShotgunProbMax, int nconfs)
{
	int i;
	
	if(dataTable->nDimens<=(2+nconfs))
	{
		fprintf(out,"0");
		for(i=1;i<dataTable->Total;i++)
		{
			fprintf(out," 1");
		}
		fprintf(out," 1.0\n");
		return;
	}
	
	int** VarSets = NULL;
	int* lenVarSets = NULL;
	int nVarSets = -1;

	int** DownLinks = NULL;
	int* nDownLinks = NULL;
	int** UpLinks = NULL;
	int* nUpLinks = NULL;
	
	LPTable priorTable = new Table;
	InitPriorTable(priorTable,dataTable,-1);

	InitVarSets(dataTable->nDimens,
	            VarSets,lenVarSets,nVarSets);
	InitLattice(dataTable->nDimens,
				VarSets,lenVarSets,nVarSets,
				DownLinks,nDownLinks, 
				UpLinks,nUpLinks);

	LPTable datapriorTable = new Table;
	if(!datapriorTable->Alloc(dataTable->Dimens,dataTable->nDimens))
	{
		printf("Failed to allocate data+prior table.\n"); exit(1);
	}
	for(i=0;i<datapriorTable->Total;i++)
	{
		datapriorTable->Data[i] = dataTable->Data[i]+priorTable->Data[i];
	}

	//this is where we store all the models we identify
	CModel* models = new CModel;
	models->SetCutoffs(ShotgunCutoffMax,ShotgunCutoffMax);
	
	for(int astartpoint=1;astartpoint<=ShotgunChainReplicates;astartpoint++)
	{
		CModel* localmodels = new CModel;
		localmodels->SetCutoffs(ShotgunCutoffMax,ShotgunCutoffMin);
		
		doRJMCMCstart(models,localmodels,astartpoint,
		              dataTable,priorTable,datapriorTable,
		              VarSets,lenVarSets,nVarSets,
				      DownLinks,nDownLinks,UpLinks,nUpLinks,
					  mystream);
						
		localmodels->DeleteAll();
		delete localmodels; localmodels = NULL;
	}

	//save the best model identified
	models->NormalizeWeights();
	models->SaveBestModel(out);

	//clean memory
	models->DeleteAll();
	delete models; models = NULL;
	datapriorTable->Reset(); delete datapriorTable; datapriorTable = NULL;
	priorTable->Reset(); delete priorTable; priorTable = NULL;
	DeleteLattice(VarSets,lenVarSets,nVarSets,
				  DownLinks,nDownLinks, 
				  UpLinks,nUpLinks);
	DeleteVarSets(VarSets,lenVarSets,nVarSets);			  
	return;
}
Ejemplo n.º 21
0
int FrechetBounds(LPTable table, const char* sFileName)
{
	if(table == NULL)
	{
		printf("Error :: FrechetBounds.\n");
		return 0;
	}
	
	int k = table->nDimens;
	LPTable UpperBound = CreateMin(k-1, table);
	if(UpperBound == NULL)
	{
		printf("Error :: FrechetBounds.\n");
		return 0;
	}
	
	LPTable LowerBound = new Table;
	if(LowerBound == NULL)
	{
		printf("Error :: FrechetBounds.\n");
		UpperBound->Reset();
		delete UpperBound;
		return 0;
	}
	
	if(!LowerBound->Alloc(table->Dimens, table->nDimens))
	{
		printf("Error :: FrechetBounds.\n");
		UpperBound->Reset();
		delete UpperBound;
		return 0;
	}
	
	double n = table->GetGrandTotal();
	int m;
	int plus;
	int first = 1; //first iteration?
	int i;
	
	if(k%2 == 0) //k even
	{
		plus = 1;
		
		for(m=1; m<k; m++)
		{
			LPTable smtab = CreateS(m, table);
			if(smtab == NULL)
			{
				printf("Error :: FrechetBounds.\n");
				UpperBound->Reset();
				LowerBound->Reset();
				delete UpperBound;
				delete LowerBound;
				
				return 0;
			}
			
			if(first)
			{
				for(i=0; i<table->Total; i++)
				{
					LowerBound->Data[i] = smtab->Data[i] - n;
				}
				
				first = 0;
			}
			else
			{
				if(plus == -1)
				{
					for(i=0; i<table->Total; i++)
					{
						LowerBound->Data[i] -= smtab->Data[i];
					}
				}
				else //plus == 1
				{
					for(i=0; i<table->Total; i++)
					{
						LowerBound->Data[i] += smtab->Data[i];
					}
				}
			}
			
			plus = - plus;
			smtab->Reset();
			delete smtab;
		}
	}
	else // k odd
	{
		LPTable nBar = CreateNBar(table);
		if(nBar == NULL)
		{
			printf("Error :: FrechetBounds.\n");
			UpperBound->Reset();
			LowerBound->Reset();
			delete UpperBound;
			delete LowerBound;
			return 0;
		}
		
		LPTable nMinBar = CreateMin(k-1, nBar);
		nBar->Reset();
		delete nBar;
		if(nMinBar == NULL)
		{
			printf("Error :: FrechetBounds.\n");
			UpperBound->Reset();
			LowerBound->Reset();
			delete UpperBound;
			delete LowerBound;
			return 0;
		}
		
		plus = -1;
		
		for(m=1; m<k; m++)
		{
			LPTable smtab = CreateS(m, table);
			if(smtab == NULL)
			{
				printf("Error :: FrechetBounds.\n");
				UpperBound->Reset();
				LowerBound->Reset();
				delete UpperBound;
				delete LowerBound;
				return 0;
			}
			
			if(first)
			{
				for(i=0; i<table->Total; i++)
				{
					LowerBound->Data[i] = n - smtab->Data[i];
				}
				
				first = 0;
			}
			else
			{
				if(plus == -1)
				{
					for(i=0; i<table->Total; i++)
					{
						LowerBound->Data[i] -= smtab->Data[i];
					}
				}
				else //plus == 1
				{
					for(i=0; i<table->Total; i++)
					{
						LowerBound->Data[i] += smtab->Data[i];
					}
				}
			}
			
			plus = - plus;
			smtab->Reset();
			delete smtab;
		}
		
		for(i=0; i<table->Total; i++)
		{
			LowerBound->Data[i] -= nMinBar->Data[i];
		}
		
		nMinBar->Reset();
		delete nMinBar;
	}
	
	for(i=0; i<table->Total; i++)
	{
		if(LowerBound->Data[i] < 0)
		{
			LowerBound->Data[i] = 0;
		}
	}
	
	int rez = WriteBounds(UpperBound, LowerBound, sFileName);
	
	UpperBound->Reset();
	LowerBound->Reset();
	delete UpperBound;
	delete LowerBound;
	
	return rez;
}