LDA learning 4-Implementation of Markov chains and their stable distribution in java

Source: Internet
Author: User

Learn the mathematical principles behind the LDA model. Refer to the LDA mathematical gossip article, which describes Markov chains and their stable distribution. It is interesting and easy to understand, the author's data and conclusions are verified using Java. The code is now posted for future reference.

Public class Main {private static final double [] [] tm = {0.65, 0.28, 0.07}, {0.15, 0.67, 0.18}, {0.12, 0.36 }}; private static final double [] pi = {0.21, 0.68, 0.11 }; /** calculate the distribution of each class after n generations based on the initial probability and transition matrix **/public static double [] nGen (int n) {double [] [] temp = new double [2] [3]; temp [0] = pi. clone (); int cur = 1; int last = 0; while (-- n> = 0) {for (int j = 0; j <3; j ++) {temp [cur] [j] = 0.0f; for (int k = 0; k <3; k ++) {temp [cur] [j] + = temp [last] [k] * tm [k] [j];} last = cur; cur = (cur + 1) % 2;} return temp [last];} public static void main (String [] args) {double [] d = nGen (1); for (int I = 0; I <d. length; I ++) {System. out. println (d [I]) ;}}

In fact, there is nothing complicated: matrix multiplication in linear algebra, plus a little bit of coding techniques in rolling arrays!

This article from the "every day progress a little" blog, please be sure to keep this source http://sbp810050504.blog.51cto.com/2799422/1297929

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.