time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

init.js (1697B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var randu = require( '@stdlib/random/base/randu' );
     24 var floor = require( '@stdlib/math/base/special/floor' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Randomly assign topics to words and keep track of the counts.
     31 *
     32 * @private
     33 */
     34 function init() {
     35 	/* eslint-disable no-invalid-this */
     36 	var topic;
     37 	var newz;
     38 	var len;
     39 	var wt;
     40 	var d;
     41 	var i;
     42 
     43 	this.z = [];
     44 	for ( d = 0; d < this.D; d++ ) {
     45 		this.z.push( [] );
     46 		len = this.w[ d ].length;
     47 
     48 		// Initialize random topics...
     49 		for ( i = 0; i < len; i++ ) {
     50 			newz = floor( randu() * this.K );
     51 			this.z[ d ].push( newz );
     52 		}
     53 		this.ndSum[ d ] = len;
     54 		for ( i = 0; i < len; i++ ) {
     55 			wt = this.w[ d ][ i ];
     56 			topic = this.z[ d ][ i ];
     57 
     58 			// Number of instances of word i assigned to topic j:
     59 			this.nw.set( wt, topic, this.nw.get( wt, topic ) + 1 );
     60 
     61 			// Number of words in document i assigned to topic j:
     62 			this.nd.set( d, topic, this.nd.get( d, topic ) + 1 );
     63 
     64 			// Total number of words assigned to topic j:
     65 			this.nwSum[ topic ] = this.nwSum[ topic ] + 1;
     66 		}
     67 	}
     68 }
     69 
     70 
     71 // EXPORTS //
     72 
     73 module.exports = init;