INGOR
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 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12
13 * Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
16 distribution.
17
18 * Neither the name of Hirosaki University, Kyoto University, The
19 University of Tokyo, nor the names of its contributors may be
20 used to endorse or promote products derived from this software
21 without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35*/
36
37#ifndef __INGOR_BNRC_H
38#define __INGOR_BNRC_H
39
40#include "util/ytData.h"
41#include "util/ytKeyValues.h"
42#include "net/ytNetwork.h"
43#include "net/ytNode.h"
44#include "net/ytEdge.h"
45
46/* In mega bytes */
47//#define BNRC_DEFAULT_MAX_MEM 500
48#define BNRC_DEFAULT_MAX_MEM 1000
49#define BNRC_DEF {"BNRC",\
50 BNRC_init,\
51 BNRC_score,\
52 BNRC_reinit,\
53 Score_default_cache,\
54 BNRC_edgeProp,\
55 BNRC_nodeProp,\
56 BNRC_finalize,\
57 BNRC_partialResidual,\
58 BNRC_setEdgeProp,\
59 BNRC_setNodeProp,\
60 BNRC_status,\
61 BNRC_setParam,\
62 }
63
64typedef struct {
65 int p; /* cols (variables) of data matrices X and Y */
66 int N; /* Column length (stride width) of X, Y, Xm, Ym */
67 double * X; /* Column-major (n, p) input (explanatory) matrix */
68 double * Y; /* Column-major (n, p) output (target) matrix */
69 int * Yn; /* The number of non-missing samples in Y. */
70 char * Xm; /* Column-major (n, p) input missing value matrix.
71 A value of 1 represents non-missing, and 0 missing values. */
72 char * Ym; /* Column-major (n, p) output matrix */
73 double * xl; /* left-most minimum value vector of X */
74 double * xr; /* right-most maximum value vector of X */
75 double * B; /* p x (4 * n) row-major B-spline design matrix.
76 B + j * 4 * n corresponds to the design matrix for the j-th
77 variable */
78 int * Bindex; /* p x n idndex vector for design matrices.
79 'Bindex + j * m' corresponds to the index vector for the
80 j-th variable */
81} BNRC_Data;
82
83typedef struct {
84 int M; /* The number of B-splines */
85 int H; /* The number of hyperparamter (length of 'beta') */
86
87 double * beta; /* B-spline hyperparameters to optimize. */
88 double * log_beta; /* log of 'beta' */
89
90 double * K; /* M x M matrix to store K (= Dk' Dk). */
91 double log_det_K_plus; /* |K|_+ : product of non zero eigenvalues. */
92
93 int * pa; /* dummy parents */
94
95 /* level 2 only: */
96 /*** LambdaInv[j] points to 'h' of 'M x M' matrices. */
97 /*** (LambdaInv = (B'B + n beta K)^-1 */
98 double ** LambdaInv;
99
100 /* level 2 & 3; log|Lambda| (= B'B + n beta K) */
101 double ** logDetLambda; /* log|Lambda_jk| : p * H */
102
103 /* level 3 */
104 /*** LambdaInvBt = (B'B + n beta K)^-1 B' */
105 /*** LambdaInvBt[j] points to h of (M x n) matrices. */
106 double ** LambdaInvBt;
107
108 int maxLoops; /* max loops for optimization */
109 int stop; /* stops if parameter estimation is not converged. */
110 double outer; /* outer region of the input value range */
112
115typedef struct {
116 double ** tmp_LambdaInvBt; /* Map to LambdaInvBt */
117 double ** tmp_logDetLambda; /* Map to logDetLambda or logDetLambdaWork. */
118 double * T1; /* Working memory 1 */
119 double * T2; /* Working memory 2 */
120 int * S1; /* Integer working memory for a pivot table */
121 double * dsytrf_work;
122 int dsytrf_work_size;
123
124 double * RS; /* n-residual vector */
125 double * beta_gKg; /* mp-vector for beta gKg */
126 double * B_gamma; /* B_gamma + k * n : B_jk gamma : n * mp */
127 double * tmp_gamma; /* gamma : M * 1 */
128 double * tmp_B_gamma; /* B_gamma : n * 1 */
129 int * beta_jk; /* selection of beta[h] : max_parents */
130
131 double * LambdaInvBtWork; /* mp x H x (M x n) matrices. */
132 double * logDetLambdaWork; /* mp x H */
133
134 int * pa; /* copy of parents */
135 int q; /* number of parents */
136} BNRC_Work;
137
140typedef struct {
141 double * gamma; /* M vector of gamma for q_j parents */
142 double * xl; /* q_j min range */
143 double * xr; /* q_j max range */
144 double mean;
145 double var;
146} BNRC_Model;
147
148void * BNRC_init(ytData * data, ytKeyValues * args);
149
150void BNRC_reinit(void * buff, ytData * data);
151double BNRC_score(void * buff, const int j, const int * parents, const int q);
152void BNRC_edgeProp(void * buff, const int j, const int * parents, const int q,
153 const int k, ytEdge * edge);
154void BNRC_nodeProp(void * buff, int j, ytNode * node);
155void BNRC_finalize(void * buff);
156void BNRC_partialResidual(void * buff, const int j, const int * parents, const int q,
157 FILE ** fp, double * ll);
158void BNRC_setEdgeProp(void * buff, const int j, const int * parents, const int q,
159 const int k, const ytEdge * edge);
160void BNRC_setNodeProp(void * buff, const int j, const ytNode * node);
161void BNRC_status(void * buff);
162int BNRC_debug(int argc, char * argv[]);
163
164
165void BNRC_prepare_beta(double hyper_bg, double hyper_inc, int hyper_num,
166 double * beta, double * log_beta, int verbose);
167
168size_t BNRC_Data_alloc(BNRC_Data * D, int p, int n,
169 int allocXY, int dynamic, int level, int dry);
170void BNRC_Data_finalize(BNRC_Data * D, int allocXY);
171
172size_t BNRC_Global_alloc(BNRC_Global * G, int p, int n, int mp, int level,
173 int dry);
174void BNRC_Global_finalize(BNRC_Global * G);
175
176size_t BNRC_Work_alloc(BNRC_Work * W, int p, int n, int mp, int H,
177 int M, int level, int T1_size, int dry);
178void BNRC_Work_finalize(BNRC_Work * W);
179
180size_t BNRC_Model_alloc(BNRC_Model * P, int mp, int M, int dry);
181void BNRC_Model_finalize(BNRC_Model * P);
182
183void BNRC_range(BNRC_Data * D,
184 const ytDoubleArray * xlar, const ytDoubleArray * xrar,
185 const char * type, double outer);
186void BNRC_proc_mv(BNRC_Data * D, int level);
187void BNRC_calc_B(BNRC_Data * D, int ofs, const char * type,
188 int n, int M, BNRC_Work * W);
189void BNRC_calc_Lambda(BNRC_Data * D, BNRC_Global * G, int p, int n,
190 const char * type, BNRC_Work * W, int level);
191
192double BNRC_calc(BNRC_Data * D, int ofs, int j, const int * parents, int q,
193 int n, int nn, int level, BNRC_Work * W, BNRC_Model * P, BNRC_Global * G);
194
195size_t BNRC_get_T1_size(int n, int M);
196
197void BNRC_setParam(void * buff, const char * key, ytObject * value);
198
199#endif /* __INGOR_BNRC_H */
size_t BNRC_Work_alloc(BNRC_Work *W, int p, int n, int mp, int H, int M, int level, int T1_size, int dry)
Allocates memory for BNRC_Work.
Definition: BNRC.c:580
void BNRC_range(BNRC_Data *D, const ytDoubleArray *xlar, const ytDoubleArray *xrar, const char *type, double outer)
Sets the value range xl and xr.
Definition: BNRC.c:1602
void BNRC_calc_B(BNRC_Data *D, int ofs, const char *type, int n, int M, BNRC_Work *W)
Calculates B-spline design matrices for all the variables.
Definition: BNRC.c:915
void * BNRC_init(ytData *data, ytKeyValues *args)
Initializes the BNRC score.
Definition: BNRC.c:194
void BNRC_prepare_beta(double hyper_bg, double hyper_inc, int hyper_num, double *beta, double *log_beta, int verbose)
Prepares the hyperparamters.
Definition: BNRC.c:817
double BNRC_calc(BNRC_Data *D, int ofs, int j, const int *parents, int q, int n, int nn, int level, BNRC_Work *W, BNRC_Model *P, BNRC_Global *G)
Calculates BNRC score.
Definition: BNRC.c:1115
double BNRC_score(void *buff, const int j, const int *parents, const int q)
BNRC score function.
Definition: BNRC.c:354
void BNRC_calc_Lambda(BNRC_Data *D, BNRC_Global *G, int p, int n, const char *type, BNRC_Work *W, int level)
Calculates Λ related values for all the variables.
Definition: BNRC.c:1012
void BNRC_setEdgeProp(void *buff, const int j, const int *parents, const int q, const int k, const ytEdge *edge)
Sets parameters from ytEdge.
Definition: BNRC.c:1465
void BNRC_Data_finalize(BNRC_Data *D, int allocXY)
Releases memory allocated by BNRC_Data_alloc().
Definition: BNRC.c:453
void BNRC_reinit(void *buff, ytData *data)
Re-initializes the score function.
Definition: BNRC.c:308
Expandable array.
Network edge.
key-value pairs.
The basis class.
Definition: BNRC.h:64
Definition: BNRC.h:83
Model parameters for a single node.
Definition: BNRC.h:140
Structure for working memory.
Definition: BNRC.h:115
General data container.
Definition: ytData.h:73
Network node.
Definition: ytNode.h:42