Cytnx v0.9.1
Loading...
Searching...
No Matches
stat.hpp
Go to the documentation of this file.
1#ifndef _stat_H_
2#define _stat_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include "Storage.hpp"
7#include "Tensor.hpp"
8#include <algorithm>
9#include <iostream>
10
11namespace cytnx {
12 namespace stat {
13
15 class Histogram {
16 public:
17 double min;
18 double max;
19 uint64_t bins;
22
23 // std::vector<double> vars;
24 // std::vector<double> x;
25
27
31 Histogram(const unsigned long long &Nbins, const double &min_val, const double &max_val);
32
34 Histogram(const Histogram &rhs) {
35 this->min = rhs.min;
36 this->max = rhs.max;
37 this->bins = rhs.bins;
38 this->vars = rhs.vars.clone();
39 this->x = rhs.x.clone();
40 this->total_count = rhs.total_count;
41 }
42
43 Histogram &operator=(const Histogram &rhs) {
44 this->min = rhs.min;
45 this->max = rhs.max;
46 this->bins = rhs.bins;
47 this->vars = rhs.vars.clone();
48 this->x = rhs.x.clone();
49 this->total_count = rhs.total_count;
50 return *this;
51 }
53
54 void clear_vars() {
55 total_count = 0;
56 memset(this->vars.data(), 0, sizeof(double) * this->vars.size());
57 }
58
59 template <class T>
60 void accumulate(const std::vector<T> &data) {
61 std::vector<T> tmp = data;
62 std::sort(tmp.begin(), tmp.end());
63
64 uint64_t cntr = 0;
65 double curr_x = 0;
66 double dx = double(max - min) / bins;
67
68 // for each elem in data, compare
69 for (unsigned long long i = 0; i < tmp.size(); i++) {
70 while (cntr <= this->bins) {
71 if (tmp[i] < curr_x) {
72 if (cntr) {
73 vars.at<double>(cntr - 1) += 1;
74 total_count += 1;
75 }
76 break;
77 }
78 cntr++;
79 curr_x = cntr * dx;
80 }
81 }
82 }
83
84 void normalize();
85 void print() const;
86
87 const Storage &get_x() const {
88 // get x
89 return this->x;
90 }
92 // get x
93 return this->x;
94 }
95
96 cytnx_uint64 size() { return this->x.size(); }
97
98 }; // class histogram
99
102 public:
103 double minx, miny;
104 double maxx, maxy;
105 uint64_t binx, biny;
109
110 // std::vector<double> vars;
111 // std::vector<double> x;
112
114
118 Histogram2d(const unsigned long long &Nbinx, const unsigned long long &Nbiny,
119 const double &min_x, const double &max_x, const double &min_y,
120 const double &max_y);
121
123 Histogram2d(const Histogram2d &rhs) {
124 this->minx = rhs.minx;
125 this->maxx = rhs.maxx;
126 this->miny = rhs.miny;
127 this->maxy = rhs.maxy;
128 this->binx = rhs.binx;
129 this->biny = rhs.biny;
130 this->vars = rhs.vars.clone();
131 this->x = rhs.x.clone();
132 this->y = rhs.y.clone();
133 this->total_count = rhs.total_count;
134 }
135
136 Histogram2d &operator=(const Histogram2d &rhs) {
137 this->minx = rhs.minx;
138 this->maxx = rhs.maxx;
139 this->miny = rhs.miny;
140 this->maxy = rhs.maxy;
141 this->binx = rhs.binx;
142 this->biny = rhs.biny;
143 this->vars = rhs.vars.clone();
144 this->x = rhs.x.clone();
145 this->y = rhs.y.clone();
146 this->total_count = rhs.total_count;
147 return *this;
148 }
150
151 void clear_vars() {
152 total_count = 0;
153 memset(this->vars.data(), 0, sizeof(double) * this->vars.size());
154 }
155
156 template <class T>
157 void accumulate(const std::vector<T> &data_x, const std::vector<T> &data_y) {
158 //[not fin!]
160 data_x.size() != data_y.size(),
161 "[ERROR][Histogram2d::accumulate] data_x and data_y should have same size!%s", "\n");
162
163 double dx = double(maxx - minx) / binx;
164 double dy = double(maxy - miny) / biny;
165
166 unsigned int nx, ny;
167
168 // for each elem in data, compare
169 for (unsigned long long i = 0; i < data_x.size(); i++) {
170 nx = floor(data_x[i] / dx);
171 ny = floor(data_y[i] / dy);
172 vars.at<double>(ny * binx + nx) += 1;
173 total_count += 1;
174 }
175 }
176
177 void normalize();
178 void print() const;
179
180 const Storage &get_x() const {
181 // get x
182 return this->x;
183 }
185 // get x
186 return this->x;
187 }
188
189 const Storage &get_y() const {
190 // get y
191 return this->y;
192 }
194 // get y
195 return this->y;
196 }
197
198 std::vector<cytnx_uint64> size() {
199 return std::vector<cytnx_uint64>({this->x.size(), this->y.size()});
200 }
201
202 }; // class histogram
203
204 // function, statistical:
205 // class Boostrap{
206
207 //};//class bootstrap
208
209 } // namespace stat
210} // namespace cytnx
211
212#endif
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:1051
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1357
Storage clone() const
return a deep copy of the current storage.
Definition Storage.hpp:1350
2D, real value histogram
Definition stat.hpp:101
uint64_t binx
Definition stat.hpp:105
double minx
Definition stat.hpp:103
uint64_t biny
Definition stat.hpp:105
Storage & get_x()
Definition stat.hpp:184
double maxy
Definition stat.hpp:104
void clear_vars()
Definition stat.hpp:151
double miny
Definition stat.hpp:103
std::vector< cytnx_uint64 > size()
Definition stat.hpp:198
const Storage & get_x() const
Definition stat.hpp:180
Storage & get_y()
Definition stat.hpp:193
Histogram2d(const unsigned long long &Nbinx, const unsigned long long &Nbiny, const double &min_x, const double &max_x, const double &min_y, const double &max_y)
initialize a histogram
double total_count
Definition stat.hpp:113
cytnx::Storage y
Definition stat.hpp:108
cytnx::Storage vars
Definition stat.hpp:106
cytnx::Storage x
Definition stat.hpp:107
const Storage & get_y() const
Definition stat.hpp:189
void accumulate(const std::vector< T > &data_x, const std::vector< T > &data_y)
Definition stat.hpp:157
double maxx
Definition stat.hpp:104
1D, real value histogram
Definition stat.hpp:15
cytnx::Storage x
Definition stat.hpp:21
cytnx::Storage vars
Definition stat.hpp:20
void clear_vars()
Definition stat.hpp:54
const Storage & get_x() const
Definition stat.hpp:87
Histogram(const unsigned long long &Nbins, const double &min_val, const double &max_val)
initialize a histogram
uint64_t bins
Definition stat.hpp:19
double min
Definition stat.hpp:17
double total_count
Definition stat.hpp:26
void accumulate(const std::vector< T > &data)
Definition stat.hpp:60
cytnx_uint64 size()
Definition stat.hpp:96
double max
Definition stat.hpp:18
Storage & get_x()
Definition stat.hpp:91
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint64_t cytnx_uint64
Definition Type.hpp:45