/* Draws a histogram with N bars where N = dym_rows(freqs) Each bar is compartmented into K subbars, on top of each other, where K = dym_cols(freqs) and the j'th subcomponent of the i'th bar has height = dym_ref(freqs,i,j) and is on top of the j-1'th subcomponent. The i'th bar is centered on x=xlo + (i+0.5) * (xhi - xlo) / num_bars */ void ongr_plot_hist(frame *fr,ongr *on,dym *freqs,bool ratio) { int i; int num_bars = dym_rows(freqs); int num_classes = dym_cols(freqs); double xlo = on -> x_axis.lo; double xhi = on -> x_axis.hi; double bar_width = 0.9 * (xhi - xlo) / num_bars; for ( i = 0 ; i < num_bars ; i++ ) { double xmid = xlo + (i + 0.5) * (xhi - xlo) / num_bars; double x1 = xmid - bar_width/2; double x2 = xmid + bar_width/2; int j; double sumy = 0.0; double total_sum_y = real_max(1e-5,dym_sum_row(freqs,i)); for ( j = 0 ; j < num_classes ; j++ ) { double y1 = sumy; double dy = dym_ref(freqs,i,j) / ((ratio) ? (total_sum_y/100.0) : 1.0); double y2 = sumy + dy; int amut_col = color_code_to_ag_color(j); ongr_colored_bordered_rectangle(fr,on,x1,y1,x2,y2,amut_col); sumy = y2; } } }
/* Creates two new dyms, both with the same number of columns as the input x. *r_test will have "num_test_rows" rows and *r_train will have "dym_rows(x) - num_test_rows" rows. *r_test will consist of "num_test_rows" rows of x selected at random without replacement. *r_train will contain the rest. The rows will appear in the same order as they did in the original. */ void break_dym_into_train_and_test(dym *x,int num_test_rows, dym **r_train,dym **r_test) { ivec *train_rows; ivec *test_rows; make_train_and_test_rows(/* train_and_test_rows = */ NULL /* denoting ALL */, dym_rows(x),num_test_rows,&train_rows,&test_rows); *r_train = mk_dym_from_subset_of_rows(x,train_rows); *r_test = mk_dym_from_subset_of_rows(x,test_rows); free_ivec(train_rows); free_ivec(test_rows); }
void lr_compute_n_from_dym( const dym *M, double b0, dyv *b, dyv *n) { int numrows, numgood, row, j; double sum; numrows = dym_rows( M); numgood = dyv_size(b); for (row=0; row < numrows; ++row) { sum = 0.0; for (j=0; j<numgood; ++j) sum += dym_ref( M, row, j) * dyv_ref( b, j); sum += b0; dyv_set( n, row, sum); } return; }
double lr_deviance_from_dym_b( const dym *M, dyv *y, double b0, dyv *b) { int numrows; double dev; dyv *n, *u; numrows = dym_rows( M); n = mk_dyv( numrows); u = mk_dyv( numrows); lr_compute_n_from_dym( M, b0, b, n); lr_compute_u_from_n( n, u); dev = lr_deviance_basic( y, u); free_dyv( n); free_dyv( u); return dev; }
lr_train *mk_lr_train_from_dym( dym *factors, dyv *outputs, lr_options *opts) { /* Set rows to NULL if you want all rows from ds to be used. */ int numrows, numatts; lr_train *lrt; numrows = dym_rows( factors); numatts = dym_cols( factors)+1; /* Number of factors including constant. */ /* Create lr lrt structure. */ lrt = AM_MALLOC(lr_train); /* Copy in opts. */ lrt->opts = mk_copy_lr_options( opts); /* Assign factors and outputs into lr structure. */ lrt->X = NULL; lrt->M = factors; /* Outputs. */ lrt->y = mk_copy_dyv( outputs); if (!dyv_is_binary( outputs)) { my_error( "mk_lr_train: Error: outputs are not binary.\n"); } /* Set log likelihood of saturated model. */ lrt->likesat = 0.0; /* Initialize remainder of lr struct */ lrt->numatts = numatts; lrt->numrows = numrows; /* Create lr_state member. */ lrt->lrs = mk_lr_state( lrt, opts); /* Now that the structure is complete, update n and u to prepare for iterations. */ lr_train_update_n(lrt); lr_train_update_u(lrt); return lrt; }