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#define BNRC_DEF {"BNRC",\
26 BNRC_init,\
27 BNRC_score,\
28 BNRC_reinit,\
29 Score_default_cache,\
30 BNRC_edgeProp,\
31 BNRC_nodeProp,\
32 BNRC_finalize,\
33 BNRC_partialResidual,\
34 BNRC_calcEdgeContrib,\
35 BNRC_setEdgeProp,\
36 BNRC_setNodeProp,\
37 BNRC_status,\
38 BNRC_setParam,\
39 }
40
41typedef struct {
42 int p; /* cols (variables) of data matrices X and Y */
43 int N; /* Column length (stride width) of X, Y, Xm, Ym */
44 double * X; /* Column-major (n, p) input (explanatory) matrix */
45 double * Y; /* Column-major (n, p) output (target) matrix */
46 int * Yn; /* The number of non-missing samples in Y. */
47 char * Xm; /* Column-major (n, p) input missing value matrix.
48 A value of 1 represents non-missing, and 0 missing values. */
49 char * Ym; /* Column-major (n, p) output matrix */
50 double * xl; /* left-most minimum value vector of X */
51 double * xr; /* right-most maximum value vector of X */
52 double * B; /* p x (4 * n) row-major B-spline design matrix.
53 B + j * 4 * n corresponds to the design matrix for the j-th
54 variable */
55 int * Bindex; /* p x n idndex vector for design matrices.
56 'Bindex + j * m' corresponds to the index vector for the
57 j-th variable */
58} BNRC_Data;
59
60typedef struct {
61 int M; /* The number of B-splines */
62 int H; /* The number of hyperparamter (length of 'beta') */
63
64 double * beta; /* B-spline hyperparameters to optimize. */
65 double * log_beta; /* log of 'beta' */
66
67 double * K; /* M x M matrix to store K (= Dk' Dk). */
68 double log_det_K_plus; /* |K|_+ : product of non zero eigenvalues. */
69
70 int * pa; /* dummy parents */
71
72 /* level 2 only: */
73 /*** LambdaInv[j] points to 'h' of 'M x M' matrices. */
74 /*** (LambdaInv = (B'B + n beta K)^-1 */
75 double ** LambdaInv;
76
77 /* level 2 & 3; log|Lambda| (= B'B + n beta K) */
78 double ** logDetLambda; /* log|Lambda_jk| : p * H */
79
80 /* level 3 */
81 /*** LambdaInvBt = (B'B + n beta K)^-1 B' (M x n)*/
82 /*** LambdaInvBt[j] points to h of (M x n) matrices. */
83 double ** LambdaInvBt;
84
85 int maxLoops; /* max loops for optimization */
86 int stop; /* stops if parameter estimation is not converged. */
87 double outer; /* outer region of the input value range */
89
92typedef struct {
93 double ** tmp_LambdaInvBt; /* Map to LambdaInvBt */
94 double ** tmp_logDetLambda; /* Map to logDetLambda or logDetLambdaWork. */
95 double * T1; /* Working memory 1 ; Dk : (M - 2) * M, B : (M + 1) + n * (deg + 1),
96 BtB : M * M, S : n * n */
97 double * T2; /* Working memory 2 : M * M */
98 int * S1; /* Integer working memory for a pivot table */
99 double * dsytrf_work;
100 int dsytrf_work_size;
101
102 double * RS; /* n-residual vector */
103 double * beta_gKg; /* mp-vector for beta gKg */
104 double * B_gamma; /* B_gamma + k * n : B_jk gamma : n * mp */
105 double * tmp_gamma; /* gamma : M * 1 */
106 double * tmp_B_gamma; /* B_gamma : n * 1 */
107 int * beta_jk; /* selection of beta[h] : max_parents */
108
109 double * LambdaInvBtWork; /* mp x H x (M x n) matrices. */
110 double * logDetLambdaWork; /* mp x H */
111
112 int * pa; /* copy of parents */
113 int q; /* number of parents */
114} BNRC_Work;
115
118typedef struct {
119 double * gamma; /* M vector of gamma for q_j parents */
120 double * xl; /* q_j min range */
121 double * xr; /* q_j max range */
122 double mean;
123 double var;
124} BNRC_Model;
125
126void * BNRC_init(ytData * data, ytKeyValues * args);
127
128void BNRC_reinit(void * buff, ytData * data);
129double BNRC_score(void * buff, const int j, const int * parents, const int q);
130void BNRC_edgeProp(void * buff, const int j, const int * parents, const int q,
131 const int k, ytEdge * edge);
132void BNRC_nodeProp(void * buff, int j, ytNode * node);
133void BNRC_finalize(void * buff);
134void BNRC_partialResidual(void * buff, const int j, const int * parents, const int q,
135 FILE ** fp, double * ll);
136void BNRC_setEdgeProp(void * buff, const int j, const int * parents, const int q,
137 const int k, const ytEdge * edge);
138void BNRC_setNodeProp(void * buff, const int j, const ytNode * node);
139void BNRC_calcRelContrib(void * buff, int j,
140 const int * parents, int q, double * ar);
141void BNRC_calcEdgeContrib(void * buff, ytNetwork * network, int j,
142 const int * parents, int q,
143 int mode, double * ar);
144void BNRC_status(void * buff);
145int BNRC_debug(int argc, char * argv[]);
146
147
148void BNRC_prepare_beta(double hyper_bg, double hyper_inc, int hyper_num,
149 double * beta, double * log_beta, int verbose);
150
151size_t BNRC_Data_alloc(BNRC_Data * D, int p, int n,
152 int allocXY, int dynamic, int level, int dry);
153void BNRC_Data_finalize(BNRC_Data * D, int allocXY);
154
155size_t BNRC_Global_alloc(BNRC_Global * G, int p, int n, int mp, int level,
156 int dry);
157void BNRC_Global_finalize(BNRC_Global * G);
158
159size_t BNRC_Work_alloc(BNRC_Work * W, int p, int n, int mp, int H,
160 int M, int level, int T1_size, int dry);
161void BNRC_Work_finalize(BNRC_Work * W);
162
163size_t BNRC_Model_alloc(BNRC_Model * P, int mp, int M, int dry);
164void BNRC_Model_finalize(BNRC_Model * P);
165
166void BNRC_range(BNRC_Data * D,
167 const ytDoubleArray * xlar, const ytDoubleArray * xrar,
168 const char * type, double outer);
169void BNRC_proc_mv(BNRC_Data * D, int level);
170void BNRC_calc_B(BNRC_Data * D, int ofs, const char * type,
171 int n, int M, BNRC_Work * W);
172void BNRC_calc_Lambda(BNRC_Data * D, BNRC_Global * G, int p, int n,
173 const char * type, BNRC_Work * W, int level);
174
175double BNRC_calc(BNRC_Data * D, int ofs, int j, const int * parents, int q,
176 int n, int nn, int level, BNRC_Work * W, BNRC_Model * P, BNRC_Global * G);
177
178size_t BNRC_get_T1_size(int n, int M);
179
180void BNRC_setParam(void * buff, const char * key, ytObject * value);
181
182#endif /* __INGOR_BNRC_H */
Expandable array.
Network edge.
key-value pairs.
Network abstraction.
The basis class.
Definition BNRC.h:41
Definition BNRC.h:60
Model parameters for a single node.
Definition BNRC.h:118
Structure for working memory.
Definition BNRC.h:92
General data container.
Definition ytData.h:46
Network node.
Definition ytNode.h:15