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