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.yLargestInnerPart[dot.x]){ LargestInnerPart[dot.x]=dot.y; g.setColor(new Color(128,128,128)); g.drawRect(dot.x,dot.y,1,1); } } } public static double function(double x, double y){ double z; double k0; double k1; double u; double d0=1; double[] d1= new double[21]; double[] d2={0.58,-2.23,0.86,0.16,-0.20,0.54,1.52,-0.69,0.66,-1.75,-1.24,0.32,-0.58,-0.83,-0.74,-2.01,-0.71,-0.05,0.47,2.30}; for(int i=0;i<20;i++){ d1[i]=Math.pow(2*Math.PI*x,(-0.5))*Math.exp(-Math.pow((d2[i]-y),2)/(2*x)); d0=d0*d1[i]; } u=(Math.pow((2*Math.PI)*(x/ratio),(-0.5)))*Math.exp(-Math.pow((y-0),2)/(2*x/ratio)); k0=Math.pow(exp,2)/var+2; k1=Math.pow(exp,3)/var+exp; z=160000000*Math.pow(x,(-k0-1))*Math.exp((-k1)/(x))*u; if(x>=0){ if(z>=0){ if(u>=0) return 100000000*d0*z; else return 0; } else return 0; } else return 0; } public Color gradation(double z){ int d,r,g,b; z=z*5.2; if(z>=0) d=(int)z % 256; else d=255-(-(int)z % 256); int c=(int)(d/85.334); switch(c){ case 0: r=79+76*d/86; g=129+58*d/86; b=189-100*d/86; break; case 1: r=155+37*(d-86)/85; g=187-99*(d-86)/85; b=89-12*(d-86)/85; break; case 2: r=192-113*(d-171)/84; g=88+41*(d-171)/84; b=77+112*(d-171)/84; break; default: r=0; g=0; b=0; break; } Color color=new Color(r,g,b); return color; } public Point transformation(double x,double y,double z){ Point dot=new Point(); dot.x=StartingX+(int)((x+120)+Math.cos(Math.toRadians(30))*(y+120)/2); dot.y=StartingY-(int)(Math.sin(Math.toRadians(30))*(y+120)/2+(z+120)); return dot; } }