Esempio n. 1
0
void main()
{
	int CurveLength = file_length(BalanceFile)/sizeof(var);
	var *Balances = file_content(BalanceFile);

	int M = CurveLength - DrawDownDays + 1;
	int T = TradeDays - DrawDownDays + 1;
	
	if(T < 1 || M <= T) {
		printf("Not enough samples!");
		return;
	}
	
	var GMin=0, N=0;
	int i = 0;
	for(; i < M; i++)
	{
		var G = Balances[i+DrawDownDays-1] - Balances[i];
		if(G <= -DrawDown) N += 1.;
		if(G < GMin) GMin = G;
	}  

	var P;
	if(TradeDays > DrawDownDays)
		P = 1. - exp(logsum(M-N)+logsum(M-T)-logsum(M)-logsum(M-N-T));
	else
		P = N/M;

	printf("\nTest period: %i days",CurveLength);
	printf("\nWorst test drawdown: %.f",-GMin);
	printf("\nM: %i  N: %i  T: %i",M,(int)N,T);
	printf("\nCold Blood Index: %.1f%%",100*P);
}
Esempio n. 2
0
// Special logsum case for size == 3
double logsum(double a, double b, double c){
  double buf[3];
  buf[0] = a;
  buf[1] = b;
  buf[2] = c;
  return logsum(buf, 3);
}
Esempio n. 3
0
// Special logsum case for size == 4
double logsum(double a, double b, double c, double d){
  double buf[4];
  buf[0] = a;
  buf[1] = b;
  buf[2] = c;
  buf[3] = d;
  return logsum(buf, 4);
}
Esempio n. 4
0
// run forward algorithm for one column of the table
void forward_step(double *col1, double* col2,
                  int nstates1, int nstates2, double **trans, double *emit)
{
    double tmp[nstates1];

    for (int k=0; k<nstates2; k++) {
        for (int j=0; j<nstates1; j++)
            tmp[j] = col1[j] + trans[j][k];
        col2[k] = logsum(tmp, nstates1) + emit[k];
    }
}
Esempio n. 5
0
float score_gmm (int gmm_index, float *input) { 
	gmm *g = &gmms[gmm_index];	
	const float *mixtures = g->mixture_weights;
	float score = 0.0f;
	int k=0;
	for(k = 0; k < g->count; k++) {
		float likelihood = log_likelihood( &g->gaussians[k], input) + mixtures[k];
		score = logsum(score, likelihood);			
	}
	return score;	
}
Esempio n. 6
0
int sample_hmm_posterior_step(int nstates1, double **trans, double *col1,
                              int state2)
{
    double A[nstates1];

    for (int j=0; j<nstates1; j++)
        A[j] = col1[j] + trans[j][state2];
    double total = logsum(A, nstates1);
    for (int j=0; j<nstates1; j++)
        A[j] = exp(A[j] - total);
    return sample(A, nstates1);
}
Esempio n. 7
0
// Calculate posterio probabilities (PP) from GLs and prior
// GL in log-space by default, but can be in normal-space if flag set
// prior and PP always given log-space
void post_prob(double *pp, double *lkl, double *prior, uint64_t n_geno){
  for(uint64_t cnt = 0; cnt < n_geno; cnt++){
    pp[cnt] = lkl[cnt];

    if(prior != NULL)
      pp[cnt] += prior[cnt];
  }

  double norm = logsum(pp, n_geno);

  for(uint64_t cnt = 0; cnt < n_geno; cnt++)
    pp[cnt] -= norm;
}
Esempio n. 8
0
/*
  f - previous estimate of the 4 possible haplotype frequencies (in normal scale)
  s1 - GLs (in log scale) for site1 for all "n" individuals
  s2 - GLs (in log scale) for site2 for all "n" individuals
  n - number of individuals
*/
int pair_freq_iter_log(double f[4], double **s1, double **s2, uint64_t n)
{
  double ff[4];
  int k, h;

  // Convert haplot frequencies to log-scale
  conv_space(f, 4, log);
  for (k = 0; k < 4; ++k)
    ff[k] = -INFINITY;

  for (uint64_t i = 0; i < n; ++i) {
    double *p[2], sum, tmp;
    p[0] = s1[i];
    p[1] = s2[i];

    sum = -INFINITY;
    for (k = 0; k < 4; ++k)
      for (h = 0; h < 4; ++h)
	sum = logsum(sum, f[k] + f[h] + p[0][_G1(k,h)] + p[1][_G2(k,h)]);

    for (k = 0; k < 4; ++k) {
      tmp = -INFINITY;
      for (h = 0; h < 4; ++h)
	logsum(tmp, f[k] + f[h] + logsum(p[0][_G1(h,k)] + p[1][_G2(h,k)], p[0][_G1(k,h)] + p[1][_G2(k,h)]));

      ff[k] = logsum(ff[k], tmp - sum);
    }
  }

  // Calculate frequency
  for (k = 0; k < 4; ++k)
    f[k] = exp(ff[k]) / (2 * n);

  // Normalize
  for (k = 0; k < 4; k++)
    f[k] /= f[0] + f[1] + f[2] + f[3];

  return 0;
}
Esempio n. 9
0
// run backward algorithm
// NOTE: last column of bw table must already be calculated
void backward_alg(int n, int nstates, double **trans, double **emit,
                  double **bw)
{
    double vec[nstates];

    for (int i=n-2; i>-1; i--) {
        double *col1 = bw[i];
        double *col2 = bw[i+1];
        double *emit2 = emit[i+1];

        for (int j=0; j<nstates; j++) {
            for (int k=0; k<nstates; k++)
                vec[k] = trans[j][k] + col2[k] + emit2[k];
            col1[j] = logsum(vec, nstates);
        }
    }
}
Esempio n. 10
0
// run forward algorithm
// NOTE: first column of fw table must already be calculated
void forward_alg(int n, int nstates, double **trans, double **emit,
                 double **fw)
{
    double vec[nstates];

    for (int i=1; i<n; i++) {
        double *col1 = fw[i-1];
        double *col2 = fw[i];
        double *emit2 = emit[i];

        for (int k=0; k<nstates; k++) {
            for (int j=0; j<nstates; j++)
                vec[j] = col1[j] + trans[j][k];
            col2[k] = logsum(vec, nstates) + emit2[k];
        }
    }
}
Esempio n. 11
0
void sample_hmm_posterior(int n, int nstates, double **trans,
                          double **fw, int *path)
{
    // NOTE: path[n-1] must already be sampled

    double A[nstates];

    // recurse
    for (int i=n-2; i>=0; i--) {
        int k = path[i+1];
        for (int j=0; j<nstates; j++)
            A[j] = fw[i][j] + trans[j][k];
        double total = logsum(A, nstates);
        for (int j=0; j<nstates; j++)
            A[j] = exp(A[j] - total);
        path[i] = sample(A, nstates);
    }
}
Esempio n. 12
0
double log_prob_overlap(int x, int s1, int s2, int n) {
	assert(x <= s1);
	assert(x <= s2);
	assert(s1 <= n);
	assert(s2 <= n);

	if(x == 0)
		return 0;

	int m1 = min(s1, s2);
	int m2 = max(s1, s2);
	double ret = 0.0, lt;
	for(int i = x; i <= m1; i++) {
		lt = lnbico(m1, i) + lnbico(n - m1, m2 - i) - lnbico(n, m2);
		if(! ret)
			ret = lt;
		else
			ret = logsum(ret, lt);
		if(lt < ret - 5) break;
	}
	return ret;
}
Esempio n. 13
0
// Special logsum case for size == 2
double logsum(double a, double b){
  double buf[2];
  buf[0] = a;
  buf[1] = b;
  return logsum(buf, 2);
}
Esempio n. 14
0
var logsum(int n)
{
	if(n <= 1) return 0;
	else return log(n)+logsum(n-1);
}
Esempio n. 15
0
int main(){
	logsum(10000000);

	return 0;
}
Esempio n. 16
0
double inverse_epsilon(double E_length)
{
  double lambda_E = E_length - logsum(0,E_length);

  return lambda_E;
}