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