Blame view

src/ExternLib/StatisticProcesses/MinVarFunction.hh 1.9 KB
fbe3c2bb   Benjamin Renard   First commit
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * MinVarFunction.hh
 *
 *  Created on: 09 nov. 2014
 *      Author: AKKA
 */
 
#ifndef MINVARFUNCTION_HH_
#define MINVARFUNCTION_HH_

#include <vector>
#include <iostream>

namespace AMDA {
namespace Statistic {
namespace MinVar {

template<typename Type>
class MinVarFunction
{
	public:
	MinVarFunction(void) : _dim(0), _nbDataProcessed(0)
	{
	}
	
	bool pushVector(std::vector<Type> v)
	{
		if (_nbDataProcessed == 0)
		{
			//initialize
			_dim = v.size();
			reset();
		}
		
		//check dimension
		if (v.size() != _dim)
			return false;
		
		//update sum of components
		for (unsigned int i = 0; i < _dim; ++i)
			_v[i] += v[i];
		
		//update working matrix
		for (unsigned int i = 0; i < _dim; ++i)
			for (unsigned int j = 0; j < _dim; ++j)
				_a[i][j] += v[i]*v[j];
				
		++_nbDataProcessed;
				
		return true;
	}
	
	bool computeMinVar(std::vector<std::vector<Type>>& result)
	{
		if (_nbDataProcessed == 0)
		{
			reset();
			return false;
		}
		
		result.clear();
		
		//compute min var
		for (unsigned int i = 0; i < _dim; ++i)
		{
			result.push_back(_a[i]);
			for (unsigned int j = 0; j < _dim; ++j)
				result[i][j] = result[i][j] / _nbDataProcessed - (_v[i] / _nbDataProcessed) * (_v[j] / _nbDataProcessed);
		}
	
		//re-init for next computation
		reset();
	
		return true;
	}
	
	void reset(void)
	{
		_nbDataProcessed = 0;
		//init working matrix
		_a.clear();
		std::vector<Type> An;
		for (unsigned int i = 0; i < _dim; ++i)
			An.push_back(0);
		for (unsigned int i = 0; i < _dim; ++i)
			_a.push_back(An);
		//init working vector
		_v.clear();
		for (unsigned int i = 0; i < _dim; ++i)
			_v.push_back(0);
	}
	
	private:
	unsigned int _dim;
	
	int _nbDataProcessed;
	
	std::vector<std::vector<Type>> _a;
	
	std::vector<Type> _v;
};

} /* namespace MinVar */
} /* namespace Statistic */
} /* AMDA */

#endif