Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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;
}