File: sampling.cpp

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (63 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  sampling.cpp
//  cufflinks
//
//  Created by Cole Trapnell on 12/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include "sampling.h"
#include <limits>

using namespace std;

void generate_importance_samples(multinormal_generator<double>& generator,
                                 std::vector<boost::numeric::ublas::vector<double> >& samples, 
                                 int num_samples,
                                 bool no_zeros)
{
	for (int i = 0; i < num_samples; ++i)
	{
		boost::numeric::ublas::vector<double> r = generator.next_rand();
        
		boost::numeric::ublas::vector<double> scaled_sample = r;
		
		for (size_t j = 0; j < scaled_sample.size(); ++j) {
            //			if (scaled_sample(j) < 0)
            //				scaled_sample(j) = 1e-10;
            if (scaled_sample(j) < 0)
                scaled_sample(j) = -scaled_sample(j);
		}
		
		double m = sum(scaled_sample);
		if (m && !isnan(m))
		{
			for (size_t j = 0; j < scaled_sample.size(); ++j) 
            {
				scaled_sample(j) = scaled_sample(j) / m;
			}
			if (no_zeros)
            {
                bool has_zero = false;
                for (size_t j = 0; j < scaled_sample.size(); ++j)
                {
                    if (scaled_sample[j] == 0)
                    {
                        has_zero = true;
                        break;
                    }
                }
                
                if (has_zero)
                    continue;
            }
			samples.push_back(scaled_sample);
		}
		else
		{
            samples.push_back(boost::numeric::ublas::zero_vector<double>(scaled_sample.size()));
			//cerr << r << endl;
			//cerr << scaled_sample << endl;
		}
	}
}