This paper looks at the generation of electrocardiogram (ECG) as a generative process and represents it with a hierarchical graphical model. This allows incorporation of several prior information like knowledge of dynamics of transmembrane potential, possibe error in the dynamic representation, sparsity of the errors, etc. in a common framework of probabilistic graphical model. Then, combining ideas like variational inference and expectation maximization, we propose algorithm to estimate transmembrane potential (TMP) together with possible error. We then go on to analyze the performance of the algorithm by connecting it with machine learning algorithms like relevance vector machine. In addition to principled formulation and rigorous mathematical analysis, this paper has several interesting ideas like using Fenchel duality to obtain lower bound variational approximation of generalized Gaussian distribution, using matrix identities and algebra to obtain time-optimized update equations and analysis of performance in terms of singular values of forward matrix.