#include <stdio.h>
char input_data[5][100] =
{ "HTTTHHTHTH",
"HHHHTHHHHH",
"HTHHHHHTHH",
"HTHTTTHHTT",
"THHHTHHHTH"};
int func_E_STEP( float possi_A, float possi_B, float *new_posii_A, float *new_posii_B)
{
float curValue_A = 0;
float curValue_B = 0;
float curPercent_A = 0;
float curPercent_B = 0;
float countHead_A = 0;
float countTail_A = 0;
float countHead_B = 0;
float countTail_B = 0;
int i, j;
float real_Head, real_Tail;
for( i = 0 ; i < 5 ; i++ )
{
curValue_A = curValue_B = 1;
real_Head = real_Tail = 0;
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_A *= possi_A;
}
else
{
curValue_A *= (1-possi_A);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_B *= possi_B;
}
else
{
curValue_B *= (1-possi_B);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
real_Head++;
}
else
{
real_Tail++;
}
}
curPercent_A = curValue_A / (curValue_A + curValue_B);
curPercent_B = curValue_B / (curValue_A + curValue_B);
countHead_A += curPercent_A * 10 * real_Head / 10;
countTail_A += curPercent_A * 10 * real_Tail / 10;
countHead_B += curPercent_B * 10 * real_Head / 10;
countTail_B += curPercent_B * 10 * real_Tail / 10;
}
*new_posii_A = countHead_A / ( countHead_A + countTail_A );
*new_posii_B = countHead_B / ( countHead_B + countTail_B );
return 0;
}
int func_em_select( float possi_A, float possi_B )
{
float curValue_A = 0;
float curValue_B = 0;
float curPercent_A = 0;
float curPercent_B = 0;
int i, j;
for( i = 0 ; i < 5 ; i++ )
{
curValue_A = curValue_B = 1;
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_A *= possi_A;
}
else
{
curValue_A *= (1-possi_A);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_B *= possi_B;
}
else
{
curValue_B *= (1-possi_B);
}
}
curPercent_A = curValue_A / (curValue_A + curValue_B);
curPercent_B = curValue_B / (curValue_A + curValue_B);
if( curPercent_A > curPercent_B )
{
fprintf( stdout, "%d:%s is A(%lf)n", i, input_data[i], curPercent_A );
}
else
{
fprintf( stdout, "%d:%s is B(%lf)n", i, input_data[i], curPercent_B );
}
}
return 0;
}
int main()
{
float possi_A = 0.60;
float possi_B = 0.50;
float new_possi_A = 0;
float new_possi_B = 0;
int i;
for( i = 0 ; i < 10 ; i++ )
{
func_E_STEP( possi_A, possi_B, &new_possi_A, &new_possi_B );
fprintf( stdout, "(%d) A : %lf, B : %lfn", i, new_possi_A, new_possi_B );
// M STEP
possi_A = new_possi_A;
possi_B = new_possi_B;
}
func_em_select( possi_A, possi_B );
return 0;
}
char input_data[5][100] =
{ "HTTTHHTHTH",
"HHHHTHHHHH",
"HTHHHHHTHH",
"HTHTTTHHTT",
"THHHTHHHTH"};
int func_E_STEP( float possi_A, float possi_B, float *new_posii_A, float *new_posii_B)
{
float curValue_A = 0;
float curValue_B = 0;
float curPercent_A = 0;
float curPercent_B = 0;
float countHead_A = 0;
float countTail_A = 0;
float countHead_B = 0;
float countTail_B = 0;
int i, j;
float real_Head, real_Tail;
for( i = 0 ; i < 5 ; i++ )
{
curValue_A = curValue_B = 1;
real_Head = real_Tail = 0;
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_A *= possi_A;
}
else
{
curValue_A *= (1-possi_A);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_B *= possi_B;
}
else
{
curValue_B *= (1-possi_B);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
real_Head++;
}
else
{
real_Tail++;
}
}
curPercent_A = curValue_A / (curValue_A + curValue_B);
curPercent_B = curValue_B / (curValue_A + curValue_B);
countHead_A += curPercent_A * 10 * real_Head / 10;
countTail_A += curPercent_A * 10 * real_Tail / 10;
countHead_B += curPercent_B * 10 * real_Head / 10;
countTail_B += curPercent_B * 10 * real_Tail / 10;
}
*new_posii_A = countHead_A / ( countHead_A + countTail_A );
*new_posii_B = countHead_B / ( countHead_B + countTail_B );
return 0;
}
int func_em_select( float possi_A, float possi_B )
{
float curValue_A = 0;
float curValue_B = 0;
float curPercent_A = 0;
float curPercent_B = 0;
int i, j;
for( i = 0 ; i < 5 ; i++ )
{
curValue_A = curValue_B = 1;
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_A *= possi_A;
}
else
{
curValue_A *= (1-possi_A);
}
}
for( j = 0 ; j < 10 ; j++ )
{
if( input_data[i][j] == 'H' )
{
curValue_B *= possi_B;
}
else
{
curValue_B *= (1-possi_B);
}
}
curPercent_A = curValue_A / (curValue_A + curValue_B);
curPercent_B = curValue_B / (curValue_A + curValue_B);
if( curPercent_A > curPercent_B )
{
fprintf( stdout, "%d:%s is A(%lf)n", i, input_data[i], curPercent_A );
}
else
{
fprintf( stdout, "%d:%s is B(%lf)n", i, input_data[i], curPercent_B );
}
}
return 0;
}
int main()
{
float possi_A = 0.60;
float possi_B = 0.50;
float new_possi_A = 0;
float new_possi_B = 0;
int i;
for( i = 0 ; i < 10 ; i++ )
{
func_E_STEP( possi_A, possi_B, &new_possi_A, &new_possi_B );
fprintf( stdout, "(%d) A : %lf, B : %lfn", i, new_possi_A, new_possi_B );
// M STEP
possi_A = new_possi_A;
possi_B = new_possi_B;
}
func_em_select( possi_A, possi_B );
return 0;
}
댓글 달기