INGOR
Loading...
Searching...
No Matches
BNRC.h
1/*
2 score/BNRC.{h,c} : BNRC network score function.
3 Copyright (C) 2018-2021, Yoshinori Tamada <tamada A T ytlab.jp>
4 All rights reserved.
5
6 THE FEATURES OF ECv CALCULATION ARE PROTECTED BY PATENT.
7 KYOTO UNIVERSITY HOLDS THE COMMERCIAL RIGHTS TO THIS SOFTWARE.
8 DO NOT COPY, MODIFY OR REDISTRIBUTE WITHOUT PERMISSION.
9
10 See LICENSE.txt for details of the licensing agreement.
11*/
12
13#ifndef __INGOR_BNRC_H
14#define __INGOR_BNRC_H
15
16#include "util/ytData.h"
17#include "util/ytKeyValues.h"
18#include "net/ytNetwork.h"
19#include "net/ytNode.h"
20#include "net/ytEdge.h"
21
22/* In mega bytes */
23//#define BNRC_DEFAULT_MAX_MEM 500
24#define BNRC_DEFAULT_MAX_MEM 1000
25#ifdef INGOR_PUB
26#define BNRC_DEF {"BNRC",\
27 BNRC_init,\
28 BNRC_score,\
29 BNRC_reinit,\
30 Score_default_cache,\
31 BNRC_edgeProp,\
32 BNRC_nodeProp,\
33 BNRC_finalize,\
34 BNRC_partialResidual,\
35 NULL,\
36 BNRC_setEdgeProp,\
37 BNRC_setNodeProp,\
38 BNRC_status,\
39 BNRC_setParam,\
40 }
41#else
42#define BNRC_DEF {"BNRC",\
43 BNRC_init,\
44 BNRC_score,\
45 BNRC_reinit,\
46 Score_default_cache,\
47 BNRC_edgeProp,\
48 BNRC_nodeProp,\
49 BNRC_finalize,\
50 BNRC_partialResidual,\
51 BNRC_calcEdgeContrib,\
52 BNRC_setEdgeProp,\
53 BNRC_setNodeProp,\
54 BNRC_status,\
55 BNRC_setParam,\
56 }
57#endif /* INGOR_PUB */
58
59typedef struct {
60 int p; /* cols (variables) of data matrices X and Y */
61 int N; /* Column length (stride width) of X, Y, Xm, Ym */
62 double * X; /* Column-major (n, p) input (explanatory) matrix */
63 double * Y; /* Column-major (n, p) output (target) matrix */
64 int * Yn; /* The number of non-missing samples in Y. */
65 char * Xm; /* Column-major (n, p) input missing value matrix.
66 A value of 1 represents non-missing, and 0 missing values. */
67 char * Ym; /* Column-major (n, p) output matrix */
68 double * xl; /* left-most minimum value vector of X */
69 double * xr; /* right-most maximum value vector of X */
70 double * B; /* p x (4 * n) row-major B-spline design matrix.
71 B + j * 4 * n corresponds to the design matrix for the j-th
72 variable */
73 int * Bindex; /* p x n idndex vector for design matrices.
74 'Bindex + j * m' corresponds to the index vector for the
75 j-th variable */
76} BNRC_Data;
77
78typedef struct {
79 int M; /* The number of B-splines */
80 int H; /* The number of hyperparamter (length of 'beta') */
81
82 double * beta; /* B-spline hyperparameters to optimize. */
83 double * log_beta; /* log of 'beta' */
84
85 double * K; /* M x M matrix to store K (= Dk' Dk). */
86 double log_det_K_plus; /* |K|_+ : product of non zero eigenvalues. */
87
88 int * pa; /* dummy parents */
89
90 /* level 2 only: */
91 /*** LambdaInv[j] points to 'h' of 'M x M' matrices. */
92 /*** (LambdaInv = (B'B + n beta K)^-1 */
93 double ** LambdaInv;
94
95 /* level 2 & 3; log|Lambda| (= B'B + n beta K) */
96 double ** logDetLambda; /* log|Lambda_jk| : p * H */
97
98 /* level 3 */
99 /*** LambdaInvBt = (B'B + n beta K)^-1 B' (M x n)*/
100 /*** LambdaInvBt[j] points to h of (M x n) matrices. */
101 double ** LambdaInvBt;
102
103 int maxLoops; /* max loops for optimization */
104 int stop; /* stops if parameter estimation is not converged. */
105 double outer; /* outer region of the input value range */
107
110typedef struct {
111 double ** tmp_LambdaInvBt; /* Map to LambdaInvBt */
112 double ** tmp_logDetLambda; /* Map to logDetLambda or logDetLambdaWork. */
113 double * T1; /* Working memory 1 ; Dk : (M - 2) * M, B : (M + 1) + n * (deg + 1),
114 BtB : M * M, S : n * n */
115 double * T2; /* Working memory 2 : M * M */
116 int * S1; /* Integer working memory for a pivot table */
117 double * dsytrf_work;
118 int dsytrf_work_size;
119
120 double * RS; /* n-residual vector */
121 double * beta_gKg; /* mp-vector for beta gKg */
122 double * B_gamma; /* B_gamma + k * n : B_jk gamma : n * mp */
123 double * tmp_gamma; /* gamma : M * 1 */
124 double * tmp_B_gamma; /* B_gamma : n * 1 */
125 int * beta_jk; /* selection of beta[h] : max_parents */
126
127 double * LambdaInvBtWork; /* mp x H x (M x n) matrices. */
128 double * logDetLambdaWork; /* mp x H */
129
130 int * pa; /* copy of parents */
131 int q; /* number of parents */
132} BNRC_Work;
133
136typedef struct {
137 double * gamma; /* M vector of gamma for q_j parents */
138 double * xl; /* q_j min range */
139 double * xr; /* q_j max range */
140 double mean;
141 double var;
142} BNRC_Model;
143
144void * BNRC_init(ytData * data, ytKeyValues * args);
145
146void BNRC_reinit(void * buff, ytData * data);
147double BNRC_score(void * buff, const int j, const int * parents, const int q);
148void BNRC_edgeProp(void * buff, const int j, const int * parents, const int q,
149 const int k, ytEdge * edge);
150void BNRC_nodeProp(void * buff, int j, ytNode * node);
151void BNRC_finalize(void * buff);
152void BNRC_partialResidual(void * buff, const int j, const int * parents, const int q,
153 FILE ** fp, double * ll);
154void BNRC_setEdgeProp(void * buff, const int j, const int * parents, const int q,
155 const int k, const ytEdge * edge);
156void BNRC_setNodeProp(void * buff, const int j, const ytNode * node);
157void BNRC_calcRelContrib(void * buff, int j,
158 const int * parents, int q, double * ar);
159#ifndef INGOR_PUB
160void BNRC_calcEdgeContrib(void * buff, ytNetwork * network, int j,
161 const int * parents, int q,
162 int mode, double * ar);
163#endif /* INGOR_PUB */
164void BNRC_status(void * buff);
165int BNRC_debug(int argc, char * argv[]);
166
167
168void BNRC_prepare_beta(double hyper_bg, double hyper_inc, int hyper_num,
169 double * beta, double * log_beta, int verbose);
170
171size_t BNRC_Data_alloc(BNRC_Data * D, int p, int n,
172 int allocXY, int dynamic, int level, int dry);
173void BNRC_Data_finalize(BNRC_Data * D, int allocXY);
174
175size_t BNRC_Global_alloc(BNRC_Global * G, int p, int n, int mp, int level,
176 int dry);
177void BNRC_Global_finalize(BNRC_Global * G);
178
179size_t BNRC_Work_alloc(BNRC_Work * W, int p, int n, int mp, int H,
180 int M, int level, int T1_size, int dry);
181void BNRC_Work_finalize(BNRC_Work * W);
182
183size_t BNRC_Model_alloc(BNRC_Model * P, int mp, int M, int dry);
184void BNRC_Model_finalize(BNRC_Model * P);
185
186void BNRC_range(BNRC_Data * D,
187 const ytDoubleArray * xlar, const ytDoubleArray * xrar,
188 const char * type, double outer);
189void BNRC_proc_mv(BNRC_Data * D, int level);
190void BNRC_calc_B(BNRC_Data * D, int ofs, const char * type,
191 int n, int M, BNRC_Work * W);
192void BNRC_calc_Lambda(BNRC_Data * D, BNRC_Global * G, int p, int n,
193 const char * type, BNRC_Work * W, int level);
194
195double BNRC_calc(BNRC_Data * D, int ofs, int j, const int * parents, int q,
196 int n, int nn, int level, BNRC_Work * W, BNRC_Model * P, BNRC_Global * G);
197
198size_t BNRC_get_T1_size(int n, int M);
199
200void BNRC_setParam(void * buff, const char * key, ytObject * value);
201
202#endif /* __INGOR_BNRC_H */
Expandable array.
Network edge.
key-value pairs.
Network abstraction.
The basis class.
Definition BNRC.h:59
Definition BNRC.h:78
Model parameters for a single node.
Definition BNRC.h:136
Structure for working memory.
Definition BNRC.h:110
General data container.
Definition ytData.h:46
Network node.
Definition ytNode.h:15