Beispiel #1
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;
}
Beispiel #2
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;
}
Beispiel #3
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;
}
Beispiel #4
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);
}
Beispiel #5
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;
}
Beispiel #6
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;
}
Beispiel #7
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;
}
Beispiel #8
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;
}