有理數加法的幾種設計: 結果回傳或覆蓋 vs 類別或物件方法 (1) public static Rational add(Ratinoal r1, Rational r2) // 用法: Rational r3 = Rational.add(r1,r2); // r3=r1+r2 (2) public static void add(Ratinoal r1, Rational r2) // 用法: Rational.add(r1,r2); // r1=r1+r2 (3) public Rational add(Rational r2) // 用法: Rational r3 = r1.add(r2); // r3=r1+r2 (4) public void add(Rational r2) // 用法: r1.add(r2); // r1=r1+r2
2016年5月24日 星期二
four kinds of design for adding operation of two rationals
2016年5月23日 星期一
Canonical Weighted Sum (CWS) Gates for Simplifying Bayesian Network Inference
decampos-10-ijar-combining content-based and collaborative recommendations- a hybrid approach based on bayesian networks The following will explain the examples in pp793-794 of the paper. Assume that I and F nodes in the bayesian network are binary random variables. If the set of parent nodes of node I6 is Pa(I6) = { F6, F7, F8}, then the 8 possible configurations of conditional joint probabilities are pa1(I6) = (f6,1 & f7,1 & f8,1) pa2(I6) = (f6,0 & f7,1 & f8,1) pa3(I6) = (f6,1 & f7,0 & f8,1) pa4(I6) = (f6,1 & f7,1 & f8,0) pa5(I6) = (f6,0 & f7,0 & f8,1) pa6(I6) = (f6,0 & f7,1 & f8,0) pa7(I6) = (f6,1 & f7,0 & f8,0) pa8(I6) = (f6,0 & f7,0 & f8,0) By use of Canonical Weighted Sum (CWS) Gates, which consist of 2x2x3=12 weights, computation of 16 conditional joint probabilities (having 8 degrees of freedom) can be decomposed into the summation of 12 weights (having 3 degrees of freedom): w(f6,1&i6,1) = w(f6,0&i6,0) = 0.3 w(f7,1&i6,1) = w(f7,0&i6,0) = 0.4 w(f8,1&i6,1) = w(f8,0&i6,0) = 0.3 and w(f6,1&i6,0) = w(f6,0&i6,1) = 0 w(f7,1&i6,0) = w(f7,0&i6,1) = 0 w(f8,1&i6,0) = w(f8,0&i6,1) = 0 Pr(i6,0 | pa1(I6))= Pr(i6,0 | f6,1 & f7,1 & f8,1) = w(f6,1&i6,0) + w(f7,1&i6,0) + w(f8,1&i6,0) = 0 Pr(i6,1 | pa1(I6))= Pr(i6,1 | f6,1 & f7,1 & f8,1) = w(f6,1&i6,1) + w(f7,1&i6,1) + w(f8,1&i6,1) = 0.3 + 0.4 + 0.3 = 1 Pr(i6,0 | pa2(I6))= Pr(i6,0 | f6,0 & f7,1 & f8,1) = w(f6,0&i6,0) + w(f7,1&i6,0) + w(f8,1&i6,0) = 0.3 Pr(i6,1 | pa2(I6))= Pr(i6,1 | f6,0 & f7,1 & f8,1) = w(f6,0&i6,1) + w(f7,1&i6,1) + w(f8,1&i6,1) = 0 + 0.4 + 0.3 = 0.7 Pr(i6,0 | pa3(I6))= Pr(i6,0 | f6,1 & f7,0 & f8,1) = w(f6,1&i6,0) + w(f7,0&i6,0) + w(f8,1&i6,0) = 0.4 Pr(i6,1 | pa3(I6))= Pr(i6,1 | f6,1 & f7,0 & f8,1) = w(f6,1&i6,1) + w(f7,0&i6,1) + w(f8,1&i6,1) = 0.3 + 0 + 0.3 = 0.6 Pr(i6,0 | pa4(I6))= Pr(i6,0 | f6,1 & f7,1 & f8,0) = w(f6,1&i6,0) + w(f7,1&i6,0) + w(f8,0&i6,0) = 0.3 Pr(i6,1 | pa4(I6))= Pr(i6,1 | f6,1 & f7,1 & f8,0) = w(f6,1&i6,1) + w(f7,1&i6,1) + w(f8,0&i6,1) = 0.3 + 0.4 + 0 = 0.7 Pr(i6,0 | pa5(I6))= Pr(i6,0 | f6,0 & f7,0 & f8,1) = w(f6,0&i6,0) + w(f7,0&i6,0) + w(f8,1&i6,0) = 0.3 + 0.4 = 0.7 Pr(i6,1 | pa5(I6))= Pr(i6,1 | f6,0 & f7,0 & f8,1) = w(f6,0&i6,1) + w(f7,0&i6,1) + w(f8,1&i6,1) = 0 + 0 + 0.3 = 0.3 Pr(i6,0 | pa6(I6))= Pr(i6,0 | f6,0 & f7,1 & f8,0) = w(f6,0&i6,0) + w(f7,1&i6,0) + w(f8,0&i6,0) = 0.3 + 0 + 0.3 = 0.6 Pr(i6,1 | pa6(I6))= Pr(i6,1 | f6,0 & f7,1 & f8,0) = w(f6,0&i6,1) + w(f7,1&i6,1) + w(f8,0&i6,1) = 0 + 0.4 + 0 = 0.4 Pr(i6,0 | pa7(I6))= Pr(i6,0 | f6,1 & f7,0 & f8,0) = w(f6,1&i6,0) + w(f7,0&i6,0) + w(f8,0&i6,0) = 0 + 0.4 + 0.3 = 0.7 Pr(i6,1 | pa7(I6))= Pr(i6,1 | f6,1 & f7,0 & f8,0) = w(f6,1&i6,1) + w(f7,0&i6,1) + w(f8,0&i6,1) = 0.3 + 0 + 0 = 0.3 Pr(i6,0 | pa8(I6))= Pr(i6,0 | f6,0 & f7,0 & f8,0) = w(f6,0&i6,0) + w(f7,0&i6,0) + w(f8,0&i6,0) = 0.3 + 0.4 + 0.3 = 1 Pr(i6,1 | pa8(I6))= Pr(i6,1 | f6,0 & f7,0 & f8,0) = w(f6,0&i6,1) + w(f7,0&i6,1) + w(f8,0&i6,1) = 0 1 = Pr(i6,0 | pa(I6)) + Pr(i6,1 | pa(I6)) = w(f6,1&i6,0) + w(f7,1&i6,0) + w(f8,1&i6,0) + w(f6,1&i6,1) + w(f7,1&i6,1) + w(f8,1&i6,1) = 1 ============================ Given Pa(U4) = { I1, I3, I6, I7, I8, I10 } with six-value (0,1,~,5) random variable U, the network consists of 2x6x6=72 weights. By the following 12 nonzero weights having 6 degrees of freedom (with all other weights being 0): w(i1,1&u4,1) = w(i3,1&u4,1) = w(i6,1&u4,2) = w(i7,1&u4,1) = w(i8,1&u4,3) = w(i10,1&u4,5) = 0.166 and w(i1,0&u4,0) = w(i3,0&u4,0) = w(i6,0&u4,0) = w(i7,0&u4,0) = w(i8,0&u4,0) = w(i10,0&u4,0) = 0.166 we can compute any Pr(U4 | pa(U4)) conditional probabilities by summation of the 12 nonzero weights. (1) pa(U4) = { i1,1 & i3,1 & i6,1 & i7,1 & i8,1 & i10,1 } Pr(u4,0 | pa(U4)) = w(i1,1&u4,0) + w(i3,1&u4,0) + w(i6,1&u4,0) + w(i7,1&u4,0) + w(i8,1&u4,0) + w(i10,1&u4,0) = 0 + 0 + 0 + 0 + 0 + 0 = 0 Pr(u4,1 | pa(U4)) = w(i1,1&u4,1) + w(i3,1&u4,1) + w(i6,1&u4,1) + w(i7,1&u4,1) + w(i8,1&u4,1) + w(i10,1&u4,1) = 0.166 + 0.166 + 0 + 0.166 + 0 + 0 = 0.5 Pr(u4,2 | pa(U4)) = w(i1,1&u4,2) + w(i3,1&u4,2) + w(i6,1&u4,2) + w(i7,1&u4,2) + w(i8,1&u4,2) + w(i10,1&u4,2) = 0 + 0 + 0.166 + 0 + 0 + 0 = 0.166 Pr(u4,3 | pa(U4)) = w(i1,1&u4,3) + w(i3,1&u4,3) + w(i6,1&u4,3) + w(i7,1&u4,3) + w(i8,1&u4,3) + w(i10,1&u4,3) = 0 + 0 + 0 + 0 + 0.166 + 0 = 0.166 Pr(u4,4 | pa(U4)) = w(i1,1&u4,4) + w(i3,1&u4,4) + w(i6,1&u4,4) + w(i7,1&u4,4) + w(i8,1&u4,4) + w(i10,1&u4,4) = 0 + 0 + 0 + 0 + 0 + 0 = 0 Pr(u4,5 | pa(U4)) = w(i1,1&u4,5) + w(i3,1&u4,5) + w(i6,1&u4,5) + w(i7,1&u4,5) + w(i8,1&u4,5) + w(i10,1&u4,5) = 0 + 0 + 0 + 0 + 0 + 0.166 = 0.166 1 = Pr(u4,1 | pa(U4)) + Pr(u4,2 | pa(U4)) + Pr(u4,3 | pa(U4)) + Pr(u4,4 | pa(U4)) + Pr(u4,5 | pa(U4)) = 0.5 + 0.166 + 0.166 + 0 + 0.166 (2) pa(U4) = { i1,0 & i3,0 & i6,1 & i7,1 & i8,0 & i10,0 } Pr(u4,0 | pa(U4)) = w(i1,0&u4,0) + w(i3,0&u4,0) + w(i6,1&u4,0) + w(i7,1&u4,0) + w(i8,0&u4,0) + w(i10,0&u4,0) = 0.166 + 0.166 + 0 + 0 + 0.166 + 0.166 = 0.666 Pr(u4,1 | pa(U4)) = w(i1,0&u4,1) + w(i3,0&u4,1) + w(i6,1&u4,1) + w(i7,1&u4,1) + w(i8,0&u4,1) + w(i10,0&u4,1) = 0 + 0 + 0 + 0.166 + 0 + 0 = 0.166 Pr(u4,2 | pa(U4)) = w(i1,0&u4,2) + w(i3,0&u4,2) + w(i6,1&u4,2) + w(i7,1&u4,2) + w(i8,0&u4,2) + w(i10,0&u4,2) = 0 + 0 + 0.166 + 0 + 0 + 0 = 0.166 Pr(u4,3 | pa(U4)) = w(i1,0&u4,3) + w(i3,0&u4,3) + w(i6,1&u4,3) + w(i7,1&u4,3) + w(i8,0&u4,3) + w(i10,0&u4,3) = 0 + 0 + 0 + 0 + 0 + 0 = 0 Pr(u4,4 | pa(U4)) = w(i1,0&u4,4) + w(i3,0&u4,4) + w(i6,1&u4,4) + w(i7,1&u4,4) + w(i8,0&u4,4) + w(i10,0&u4,4) = 0 + 0 + 0 + 0 + 0 + 0 = 0 Pr(u4,5 | pa(U4)) = w(i1,0&u4,5) + w(i3,0&u4,5) + w(i6,1&u4,5) + w(i7,1&u4,5) + w(i8,0&u4,5) + w(i10,0&u4,5) = 0 + 0 + 0 + 0 + 0 + 0 = 0 1 = Pr(u4,1 | pa(U4)) + Pr(u4,2 | pa(U4)) + Pr(u4,3 | pa(U4)) + Pr(u4,4 | pa(U4)) + Pr(u4,5 | pa(U4)) = 0.666 + 0.166 + 0.166 + 0 + 0 ============================ Suppose that Acf = A4 or user 4. Given Pa(Acf) = { Ux, Uy, Uz } with with six-value (0,1,~,5) random variable Acf, the network consists of 6x6x3=108 weights. By the following ? nonzero weights having ? degrees of freedom (with all other weights being 0): w(ux,5&acf,4) = 0.54 w(uy,1&acf,4) = 0.15 w(uz,4&acf,4) = 0.09 and w(ux,0&acf,0) = 0.6 w(uy,0&acf,0) = 0.3 w(uz,0&acf,0) = 0.1 we can compute any Pr(Acf | pa(Acf)) conditional probabilities by summation of the ? nonzero weights. (1) pa(Acf) = { ux,5 & uy,1 & uz,4 } Pr(acf,4 | pa(U4)) = w(ux,5&acf,4) + w(uy,1&acf,4) + w(uz,4&acf,4) = RSim(Ux,Acf) * Pr(A=4|Ux=5) + RSim(Uy,Acf) * Pr(A=4|Uy=1) + RSim(Uz,Acf) * Pr(A=4|Uz=4) = 0.6 * 0.9 + 0.3 * 0.5 + 0.1 * 0.9 = 0.78 (2) pa(Acf) = { ux,0 & uy,1 & uz,4 } Pr(acf,4 | pa(U4)) = w(ux,0&acf,4) + w(uy,1&acf,4) + w(uz,4&acf,4) = 0 + RSim(Uy,Acf) * Pr(A=4|Uy=1) + RSim(Uz,Acf) * Pr(A=4|Uz=4) = 0 + 0.3 * 0.5 + 0.1 * 0.9 = 0.24 (3) pa(Acf) = { ux,0 & uy,1 & uz,4 } Pr(acf,0 | pa(U4)) = w(ux,0&acf,0) + w(uy,1&acf,0) + w(uz,4&acf,0) = RSim(Ux,Acf) + 0 + 0 = 0.6 + 0 + 0 = 0.6
2016年5月3日 星期二
why object-oriented programming provides encapsulation, inheritance, and polymorphism
一般完整的物件導向語言提供封裝、繼承、多型三大功能,允許以合成或繼承方式建構新物件。 其中,封裝(encapsulation)旨在保護私密資料的安全、完整性,其存取唯有透過公開的屬性存取(窗口)或運算方法達成之。 合成(composition)是一種利用物件建構物件的方法,物件之間的擁有(has-a)關係可形成階層樹狀圖。 繼承(inheritance)旨在提供新用舊的機制,讓後代需要用到親代原來的屬性及方法時,直接沿用,不必重造車輪。 繼承也是一種利用物件建構物件的方法,物件之間的屬於(is-a)關係可形成階層樹狀圖。 多型(polymorphism)旨在繼承架構下提供舊用新的機制,讓親代也有機會用到後代創新的新行為、新功能。
find the unique second smallest number in excel
一般 Excel 找最小或最大值可以使用 MIN(range) 或 MAX(range)函數,其中,range表示儲存格範圍。 如果想要找第2小或第2大也可以使用 SMALL(range, 2) 或 LARGE(range, 2),其中,2表示順序第2。 但是以找最小為例,有時候遇到第1小及第2小值一樣,就看不出值分布情形。若希望能看到不同值的第2小數字,可以使用如下函數:
= SMALL(range, COUNTIF(range, MIN(range)) + 1)
其中, SMALL(range, n) 可回傳 range 範圍內順序第 n 小的數字。 COUNTIF(range, MIN(range)) 可回傳 range 範圍內符合最小值的儲存格個數。 此個數加1之後,即為不同值第2小數字之順序。 因此可餵給 SMALL 函數,回傳不同值第2小數字出來。
至於不同值第3或第n小數字,可能就需要更複雜的 FREQUENCY 陣列函數或自己寫巨集計算了。
訂閱:
文章 (Atom)