import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.JLabel; import javax.swing.JTextField; public class PosteriorDistribution extends Applet implements ActionListener{ // variable set private static final long serialVersionUID = -6251476488227240006L; final static int StartingX=50,StartingY=527; final static String[] rulerX = {"",""," 0.2",""," 0.6",""," 1.0",""," 1.4",""," 1.8",""}; final static String[] rulerY = {"",""," -1.0",""," -0.5",""," 0",""," 0.5",""," 1.0",""}; final static String[] rulerZ = {"","","","",""," 0","","","","","","","",""}; final int EndingX=StartingX+transformation(120,120,120).x; final int EndingY=StartingY+transformation(120,-120,-120).y; public int[] SmallestInnerPart=new int[EndingX+1]; public int[] LargestInnerPart=new int[EndingX+1]; public static double exp = 1.0; public static double var = 1.0; public static double ratio = 3.0; JTextField yx = new JTextField("1.0"); JTextField yn = new JTextField("1.0"); JTextField yr = new JTextField("3.0"); JLabel label1 = new JLabel("The Posterior Distribution", JLabel.CENTER); JLabel label2 = new JLabel("
The Expectation of σ2 (0<E(σ2)≤2.0)", JLabel.CENTER); JLabel label3 = new JLabel("The Variance of σ2 (0<Var(σ2)≤108)", JLabel.CENTER); JLabel label4 = new JLabel("The Ratio of σ2 to Var(μ) (0<r≤108)", JLabel.CENTER); public void init(){ label1.setPreferredSize(new Dimension(400,24)); label1.setFont(new Font("Serif",Font.BOLD,15)); add(label1); label2.setPreferredSize(new Dimension(260,23)); label2.setFont(new Font("Serif",Font.BOLD,11)); add(label2); yx.setPreferredSize(new Dimension(120,23)); add(yx); label3.setPreferredSize(new Dimension(260,23)); label3.setFont(new Font("Serif",Font.BOLD,11)); add(label3); yn.setPreferredSize(new Dimension(120,23)); add(yn); label4.setPreferredSize(new Dimension(260,23)); label4.setFont(new Font("Serif",Font.BOLD,11)); add(label4); yr.setPreferredSize(new Dimension(120,23)); add(yr); yx.addActionListener(this); yn.addActionListener(this); yr.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==yx){exp = Double.valueOf(yx.getText()).doubleValue(); if(exp <= 0 || exp > 2.0){exp=1.0; } } if(e.getSource()==yn){var = Double.valueOf(yn.getText()).doubleValue(); if(var <= 0 || var > Math.pow(10.0,8)){var=1.0; } } if(e.getSource()==yr){ratio = Double.valueOf(yr.getText()).doubleValue(); if(ratio <= 0 || ratio > Math.pow(10.0,8)){ratio=3.0; } } yx.setText(""+exp); yn.setText(""+var); yr.setText(""+ratio); repaint(); } public void paint(Graphics g){ Point dot,dot1,dot2,dot3,dot4,dot5,dot6; Graphics2D g2 = (Graphics2D)g; GradientPaint gp1 = new GradientPaint(0, 0, new Color(154,181,228), 0,470,new Color(225,232,245), true); g2.setPaint(gp1); g2.fillRect(0,0,460,447); super.paint(g); // x axis g.setColor(new Color(128,128,128)); dot1=transformation(-120,-120,0); dot2=transformation(120,-120,0); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); for(int i=0;i<21;i++){ dot1=transformation((120/10*i-120),-120,0); if(i % 4==0){ dot2=transformation((120/10*i-120),(-120-10),0); g.setColor(new Color(128,128,128)); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); g2.setFont(new Font ("SanSerif",Font.PLAIN,12)); g.drawString(rulerX[i/2],dot2.x-48,dot2.y+15); if(i==8) g.drawString("σ",dot2.x+28,dot2.y+30); g2.setFont(new Font ("SanSerif",Font.PLAIN,8)); if(i==8) g.drawString("2",dot2.x+37,dot2.y+23); } } // y axis g.setColor(new Color(128,128,128)); dot1=transformation(-120,-120,0); dot2=transformation(-120,120,0); dot3=transformation(-120,120,0); dot4=transformation(-120,120,210); dot5=transformation(120,-120,0); dot6=transformation(120,120,0); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); g.drawLine(dot3.x,dot3.y,dot4.x,dot4.y); g.drawLine(dot5.x,dot5.y,dot6.x,dot6.y); for(int i=0;i<210;i=i+1){ dot3=transformation(-119,120,i); dot4=transformation(120,120,i); g.setColor(new Color(154,181,228)); g.drawLine(dot3.x,dot3.y,dot4.x,dot4.y); } g.setColor(new Color(128,128,128)); for(int i=3;i<10;i++){ dot3=transformation(-120,120,(12/4*(i+3)-15)*10); dot4=transformation(120,120,(12/4*(i+3)-15)*10); g.drawLine(dot3.x,dot3.y,dot4.x,dot4.y); } for(int i=0;i<21;i++){ dot1=transformation(120,(12*i-120),0); if(i % 4==0){ dot2=transformation((12+1)*10,(12*i-120),0); g.setColor(new Color(128,128,128)); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); g.setFont(new Font ("SanSerif",Font.PLAIN,12)); g.drawString(rulerY[i/2],dot2.x-10,dot2.y+13); if(i==8) g.drawString("μ",dot2.x+38,dot2.y+16); } } // z axis g.setColor(new Color(128,128,128)); dot1=transformation(-120,-120,0); dot2=transformation(-120,-120,210); dot3=transformation(-120,119,210); dot4=transformation(-120,119,0); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); g.drawLine(dot2.x,dot2.y,dot3.x,dot3.y); g.drawLine(dot3.x,dot3.y,dot4.x,dot4.y); for(int i=0;i<240;i=i+1){ g.setColor(new Color(154,181,228)); dot5=transformation(-120,(-119+i),210); dot6=transformation(-120,(-119+i),0); g.drawLine(dot5.x,dot5.y,dot6.x,dot6.y); } g.setColor(new Color(128,128,128)); for(int i=2;i<10;i++){ dot1=transformation(-120,-120,(30*(i+3)-150)); dot2=transformation(-130,-120,(30*(i+3)-150)); dot3=transformation(-120,-119,(30*(i+3)-150)); dot4=transformation(-120,120,(30*(i+3)-150)); g.drawLine(dot1.x,dot1.y,dot2.x,dot2.y); g.drawLine(dot3.x,dot3.y,dot4.x,dot4.y); g2.setFont(new Font ("SanSerif",Font.PLAIN,12)); g.drawString(rulerZ[(i+3)],dot2.x-38,dot2.y+5); if(i==5) g.drawString("L",dot2.x-25,dot2.y-110); } // Coloring for(int i=0;i<=EndingX;i++){ SmallestInnerPart[i]=EndingY; LargestInnerPart[i]=0; } dot=new Point(); for(double x=120;x>=-120;x-=0.2) for(double y=-120;y<=120;y+=0.2){ double transX=x/120+1; double transY=y/120; double transZ=function(transX,transY); double z=transZ*3.8; dot=transformation(x,y,z); if(dot.y