void backwardelimination(uint nvariables,uint nsamples, dmatrix x, dvector w, dvector y){ bool finished = false; uint leastinterestingmodel; double logLfull = likelihoodbyem(nvariables,nsamples,x,w,y); bvector model = newbvector(nvariables); double dropneeded = 2*inverseF(2,nsamples-nvariables,0.005); cout << "Likelihood of the full model: " << logLfull << endl; while((!finished) && modelsize(nvariables,model) > 1){ cout << "modelsize(model) = " << modelsize(nvariables,model) << "Drop " << dropneeded <<endl; dvector logL = newdvector(modelsize(nvariables,model)); for(uint todrop=0;todrop<modelsize(nvariables,model);todrop++){ bvector tempmodel = newbvector(nvariables); copybvector(nvariables,model,tempmodel); dropterm(nvariables,tempmodel,todrop); dmatrix designmatrix = createdesignmatrix(nvariables,nsamples,x,tempmodel); logL[todrop] = likelihoodbyem(modelsize(nvariables,tempmodel),nsamples,designmatrix,w,y); freematrix((void**)designmatrix,nsamples); freevector((void*)tempmodel); } leastinterestingmodel = lowestindex(modelsize(nvariables,model),logL); cout << "Least interesting model:" << leastinterestingmodel << " Difference to fullmodel:" << (logLfull - logL[leastinterestingmodel]) << endl; if(dropneeded > fabs(logLfull - logL[leastinterestingmodel])){ dropterm(nvariables,model,leastinterestingmodel); logLfull = logL[leastinterestingmodel]; cout << "Drop variable" << leastinterestingmodel << endl; cout << "Likelihood of the new full model: " << logLfull<< endl; }else{ for(uint x=0;x<nvariables;x++){ if(model[x]) cout << "Variable" << x << "In Model" << endl; } finished=true; } } }
PRIVATE void compile_main(FILE *conni, FILE *conno) { REPL_DATA rd = allocmem(sizeof(repl_data)); VMstate vms; rd->h1 = rd->h2 = NULL; protect(&rd->h1); protect(&rd->h2); rd->vmregs = (VMREGS) newvector(NUM_VMREGS); /* dodgy casting :-) */ vms.r = rd->vmregs; protect((OBJ *)(&rd->vmregs)); init_vm(&vms); vms.c.vm_state = VM_STATE_NOQUOTA; while (vms.c.vm_state != VM_STATE_DYING) { ScanInst si; char buf[16384]; rd->h1 = (OBJ) newbvector(0); while (1) { char *result; result = fgets(buf, 256, conni); if (result == NULL) break; while (1) { int l = strlen(buf); if (buf[l-1] == '\r' || buf[l-1] == '\n') buf[l-1] = '\0'; else break; } strcat(buf, "\n"); if (!strcmp(buf, ".\n")) break; rd->h2 = (OBJ) newstring(buf); rd->h1 = (OBJ) bvector_concat((BVECTOR) rd->h1, (BVECTOR) rd->h2); } gc_reach_safepoint(); rd->h2 = (OBJ) newstringconn((BVECTOR) rd->h1); fill_scaninst(&si, (OVECTOR) rd->h2); while (!conn_closed((OVECTOR) rd->h2)) { rd->h1 = (OBJ) parse(&vms, &si); gc_reach_safepoint(); if (rd->h1 == NULL) { sprintf(buf, "-->! the compiler returned NULL.\n"); } else { vms.c.vm_state = VM_STATE_NOQUOTA; ATPUT((OVECTOR) rd->h1, ME_OWNER, (OBJ) vms.r->vm_uid); vms.r->vm_effuid = vms.r->vm_uid; { OVECTOR c = newovector_noinit(CL_MAXSLOTINDEX, T_CLOSURE); ATPUT(c, CL_SELF, NULL); ATPUT(c, CL_METHOD, rd->h1); rd->h1 = (OBJ) c; } apply_closure(&vms, (OVECTOR) rd->h1, newvector_noinit(1)); while (!run_vm(&vms)) ; rd->h1 = (OBJ) newvector(2); ATPUT((VECTOR) rd->h1, 0, NULL); ATPUT((VECTOR) rd->h1, 1, vms.r->vm_acc); rd->h1 = lookup_prim(0x00001, NULL)(&vms, (VECTOR) rd->h1); rd->h1 = (OBJ) bvector_concat((BVECTOR) rd->h1, newbvector(1)); /* terminates C-string */ gc_reach_safepoint(); sprintf(buf, "--> %s\n", ((BVECTOR) rd->h1)->vec); } fputs(buf, conno); } } unprotect((OBJ *)(&rd->vmregs)); unprotect(&rd->h2); unprotect(&rd->h1); freemem(rd); }