| 情報学部 | 菅沼ホーム | 目次 | 索引 | 
/****************************/
/* 二項分布の計算           */
/*      coded by Y.Suganuma */
/****************************/
#include <stdio.h>
/**************/
/* 組合せ nCr */
/**************/
double comb(int n, int r)
{
	double c = 1.0, x = 1.0, y = 1.0;
	int i1;
	if (r > 0) {
		if (r > n-r)
			r = n - r;
		for (i1 = n; i1 >= n-r+1; i1--)
			x *= i1;
		for (i1 = 2; i1 <= r; i1++)
			y *= i1;
		c = x / y;
	}
	return c;
}
/****************************************/
/* 二項分布の計算(P(X = x), P(X < x)) */
/*      x : データ                      */
/*      n,p : パラメータ                */
/*      pr : P(X = x)                   */
/*      return : P(X < x)               */
/****************************************/
#include <math.h>
double binomial(int x, int n, double p, double *pr)
{
	double f = 0.0, q;
	int i1;
	q   = 1.0 - p;
	*pr = comb(n, x) * pow(p, x) * pow(q, n-x);
	f   = *pr;
	for (i1 = 0; i1 < x; i1++)
		f += comb(n, i1) * pow(p, i1) * pow(q, n-i1);
	return f;
}
/********/
/* main */
/********/
int main()
{
	double p, pr, f;
	int n, x, sw;
	char file1[100], file2[100];
	FILE *out1, *out2;
	printf("n は? ");
	scanf("%d", &n);
	printf("p は? ");
	scanf("%lf", &p);
	printf("グラフ出力?(=1: yes,  =0: no) ");
	scanf("%d", &sw);
					// 密度関数と分布関数の値
	if (sw == 0) {
		printf("   データは? ");
		scanf("%d", &x);
		f = binomial(x, n, p, &pr);
		printf("P(X = %d) = %f,  P( X < %d) = %f (n = %d, p = %f)\n", x, pr, x, f, n, p);
	}
					// グラフ出力
	else {
		printf("   密度関数のファイル名は? ");
		scanf("%s", file1);
		printf("   分布関数のファイル名は? ");
		scanf("%s", file2);
		out1 = fopen(file1,"w");
		out2 = fopen(file2,"w");
		for (x = 0; x <= n; x++) {
			f = binomial(x, n, p, &pr);
			fprintf(out1, "%d %f\n", x, pr);
			fprintf(out2, "%d %f\n", x, f);
		}
	}
	return 0;
}
			
/****************************/
/* 二項分布の計算           */
/*      coded by Y.Suganuma */
/****************************/
import java.io.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test {
	static double p;
	static int n;
	/********/
	/* main */
	/********/
	public static void main(String args[]) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		double f;
		double pr[] = new double [1];
		int x, sw;
		System.out.print("n は? ");
		n = Integer.parseInt(in.readLine());
		System.out.print("p は? ");
		p = Double.parseDouble(in.readLine());
		System.out.print("グラフ出力?(=1: yes,  =0: no) ");
		sw = Integer.parseInt(in.readLine());
					// 密度関数と分布関数の値
		if (sw == 0) {
			System.out.print("   データは? ");
			x = Integer.parseInt(in.readLine());
			f = binomial(x, n, p, pr);
			System.out.println("P(X = " + x + ") = " + pr[0] + ",  P( X < " + x + ") = " + f + " (n = " + n + ", p = " + p + ")");
		}
					// グラフ出力
		else {
			String file1, file2;
			System.out.print("   密度関数のファイル名は? ");
			file1 = in.readLine();
			System.out.print("   分布関数のファイル名は? ");
			file2 = in.readLine();
			PrintStream out1 = new PrintStream(new FileOutputStream(file1));
			PrintStream out2 = new PrintStream(new FileOutputStream(file2));
							// データ取得
			ArrayList <Double> x1 = new ArrayList <Double> ();
			ArrayList <Double> y1 = new ArrayList <Double> ();
			ArrayList <Double> y2 = new ArrayList <Double> ();
			for (x = 0; x <= n; x++) {
				f = binomial(x, n, p, pr);
				out1.println(x + " " + pr[0]);
				out2.println(x + " " + f);
				x1.add((double)x);
				y1.add(pr[0]);
				y2.add(f);
			}
							// グラフの描画
			graph(x1, y1, y2);
		}
	}
	/**************/
	/* 組合せ nCr */
	/**************/
	static double comb(int n, int r)
	{
		double c = 1.0, x = 1.0, y = 1.0;
		int i1;
		if (r > 0) {
			if (r > n-r)
				r = n - r;
			for (i1 = n; i1 >= n-r+1; i1--)
				x *= i1;
			for (i1 = 2; i1 <= r; i1++)
				y *= i1;
			c = x / y;
		}
		return c;
	}
	/****************************************/
	/* 二項分布の計算(P(X = x), P(X < x)) */
	/*      x : データ                      */
	/*      n,p : パラメータ                */
	/*      pr : P(X = x)                   */
	/*      return : P(X < x)               */
	/****************************************/
	static double binomial(int x, int n, double p, double pr[])
	{
		double f = 0.0, q;
		int i1;
		q     = 1.0 - p;
		pr[0] = comb(n, x) * Math.pow(p, x) * Math.pow(q, n-x);
		f     = pr[0];
		for (i1 = 0; i1 < x; i1++)
			f += comb(n, i1) * Math.pow(p, i1) * Math.pow(q, n-i1);
		return f;
	}
	/*************************/
	/* グラフの描画          */
	/*      x1 : x座標データ */
	/*      y1 : 密度関数    */
	/*      y2 : 分布関数    */
	/*************************/
	static void graph(ArrayList <Double> x1, ArrayList <Double> y1, ArrayList <Double> y2)
	{
					// 密度関数
		String title1[];   // グラフ,x軸,及び,y軸のタイトル
		String g_title1[];   // 凡例(グラフの内容)
		double x_scale[];   // x軸目盛り
		double y_scale1[];   // y軸目盛り
		double data_x[][], data1_y[][];   // データ
							// グラフ,x軸,及び,y軸のタイトル
		title1 = new String [3];
		title1[0] = "密度関数(二項分布 n = " + n + ", p = " + p + ")";
		title1[1] = "x";
		title1[2] = "f(x)";
							// 凡例
		g_title1 = new String [1];
		g_title1[0] = "密度関数";
							// x軸目盛り
		x_scale = new double[3];
		double x_step = n / 10;
		if (x_step < 1.0)
			x_step = 1.0;
		double x_max = n;
		double t = 0.0;
		while (t < x_max-0.001)
			t += x_step;
		x_max = t;
		int x_p = 0;
		x_scale[0] = 0.0;   // 最小値
		x_scale[1] = x_max;   // 最大値
		x_scale[2] = x_step;   // 刻み幅
							// y軸目盛り
		y_scale1 = new double[3];
		double y_max = 0.0;
		for (int i1 = 0; i1 < n+1; i1++) {
			if ((y1.get(i1)).doubleValue() > y_max)
				y_max = (y1.get(i1)).doubleValue();
		}
		double y_step = y_max / 5;
		int y_p1 = 0;
		boolean ok = true;
		int k = 0;
		while (ok) {
			if (y_step < 1.0) {
				y_step *= 10;
				k++;
			}
			else if (y_step >= 10.0) {
				y_step /= 10;
				k--;
			}
			else {
				ok = false;
				if (y_step-(int)y_step > 1.0e-5)
					y_step = (int)y_step + 1;
				else
					y_step = (int)y_step;
				if (k != 0) {
					y_step = y_step * Math.pow(10, -k);
					if (k > 0)
						y_p1 = k;
				}
				t = 0.0;
				while (t < y_max-0.001*y_step)
					t += y_step;
				y_max = t;
			}
		}
		y_scale1[0] = 0.0;   // 最小値
		y_scale1[1] = y_max;   // 最大値
		y_scale1[2] = y_step;   // 刻み幅
							// データ
		data_x = new double [1][n+1];
		data1_y = new double [1][n+1];
		for (int i1 = 0; i1 < n+1; i1++)
			data_x[0][i1] = (x1.get(i1)).doubleValue();  
		for (int i1 = 0; i1 < n+1; i1++)
			data1_y[0][i1] = (y1.get(i1)).doubleValue();  
							// 作図
		LineGraph gp1 = new LineGraph(title1, g_title1, x_scale, x_p, y_scale1, y_p1, data_x, data1_y, true, false);
					// 分布関数
		String title2[];   // グラフ,x軸,及び,y軸のタイトル
		String g_title2[];   // 凡例(グラフの内容)
		double y_scale2[];   // y軸目盛り
		double data2_y[][];   // データ
							// グラフ,x軸,及び,y軸のタイトル
		title2 = new String [3];
		title2[0] = "分布関数(二項分布 n = " + n + ", p = " + p + ")";
		title2[1] = "x";
		title2[2] = "F(x)";
							// 凡例
		g_title2 = new String [1];
		g_title2[0] = "分布関数";
							// y軸目盛り
		y_scale2 = new double[3];
		int y_p2 = 1;
		y_scale2[0] = 0.0;   // 最小値
		y_scale2[1] = 1.0;   // 最大値
		y_scale2[2] = 0.2;   // 刻み幅
							// データ
		data2_y = new double [1][n+1];
		for (int i1 = 0; i1 < n+1; i1++)
			data2_y[0][i1] = (y2.get(i1)).doubleValue();  
							// 作図
		LineGraph gp2 = new LineGraph(title2, g_title2, x_scale, x_p, y_scale2, y_p2, data_x, data2_y, true, false);
	}
}
/****************************/
/* 折れ線グラフの描画       */
/*      coded by Y.Suganuma */
/****************************/
class LineGraph extends JFrame {
	Draw_line pn;
	/*********************************************************/
	/* コンストラクタ(折れ線グラフ2)                      */
	/*      title_i : グラフ,x軸,及び,y軸のタイトル     */
	/*      g_title_i : 凡例                                 */
	/*      x_scale_i : データの最小値,最大値,目盛幅(y) */
	/*      place_x_i : 小数点以下の桁数(x軸)             */
	/*      y_scale_i : データの最小値,最大値,目盛幅(y) */
	/*      place_y_i : 小数点以下の桁数(y軸)             */
	/*      data_x_i : グラフのデータ(x軸)                */
	/*      data_y_i : グラフのデータ(y軸)                */
	/*      d_t_i : タイトル表示の有無                       */
	/*      d_g_i : 凡例表示の有無                           */
	/*********************************************************/
	LineGraph(String title_i[], String g_title_i[], double x_scale_i[],
              int place_x_i, double y_scale_i[], int place_y_i,
              double data_x_i[][], double data_y_i[][], boolean d_t_i,
              boolean d_g_i)
	{
					// JFrameクラスのコンストラクタの呼び出し
		super("折れ線グラフ(2)");
					// Windowサイズと表示位置を設定
		int width = 900, height = 600;   // Windowの大きさ(初期サイズ)
		setSize(width, height);
		Toolkit tool = getToolkit();
		Dimension d  = tool.getScreenSize();
		setLocation(d.width / 2 - width / 2, d.height / 2 - height / 2);
					// 描画パネル
		Container cp = getContentPane();
		pn = new Draw_line(title_i, g_title_i, x_scale_i, place_x_i, y_scale_i, place_y_i, data_x_i, data_y_i, d_t_i, d_g_i, this);
		cp.add(pn);
					// ウィンドウを表示
		setVisible(true);
					// イベントアダプタ
		addWindowListener(new WinEnd());
		addComponentListener(new ComponentResize());
	}
	/**********************/
	/* Windowのサイズ変化 */
	/**********************/
	class ComponentResize extends ComponentAdapter
	{
		public void componentResized(ComponentEvent e)
		{
			pn.repaint();
		}
	}
	/************/
	/* 終了処理 */
	/************/
	class WinEnd extends WindowAdapter
	{
		public void windowClosing(WindowEvent e) {
			setVisible(false);
		}
	}
}
class Draw_line extends JPanel {
	String title[];   // グラフのタイトル
	String g_title[];   // 凡例(グラフの内容)
	String x_title[];   // x軸への表示
	double x_scale[];   // y軸目盛り
	double y_scale[];   // y軸目盛り
	double data_x[][], data_y[][];   // データ
	boolean d_t;   // タイトル表示の有無
	boolean d_g;   // 凡例表示の有無
	boolean type = true;   // 横軸が項目かデータか
	boolean ver = true;   // 縦か横か
	int place_x;   // 小数点以下の桁数(x軸)
	int place_y;   // 小数点以下の桁数(y軸)
	int width = 900, height = 600;   // Windowの大きさ(初期サイズ)
	int bx1, bx2, by1, by2;   // 表示切り替えボタンの位置
	LineGraph line;
	String change = "横 色";   // 表示切り替えボタン
	float line_w = 1.0f;   // 折れ線グラフ等の線の太さ
	boolean line_m = true;   // 折れ線グラフ等にマークを付けるか否か
	Color cl[] = {Color.black, Color.magenta, Color.blue, Color.orange, Color.cyan,
	              Color.pink, Color.green, Color.yellow, Color.darkGray, Color.red};   // グラフの色
	int n_g;   // グラフの数
	/*********************************************************/
	/* コンストラクタ(折れ線グラフ2)                      */
	/*      title_i : グラフ,x軸,及び,y軸のタイトル     */
	/*      g_title_i : 凡例                                 */
	/*      x_scale_i : データの最小値,最大値,目盛幅(y) */
	/*      place_x_i : 小数点以下の桁数(x軸)             */
	/*      y_scale_i : データの最小値,最大値,目盛幅(y) */
	/*      place_y_i : 小数点以下の桁数(y軸)             */
	/*      data_x_i : グラフのデータ(x軸)                */
	/*      data_y_i : グラフのデータ(y軸)                */
	/*      d_t_i : タイトル表示の有無                       */
	/*      d_g_i : 凡例表示の有無                           */
	/*********************************************************/
	Draw_line(String title_i[], String g_title_i[], double x_scale_i[],
              int place_x_i, double y_scale_i[], int place_y_i,
              double data_x_i[][], double data_y_i[][], boolean d_t_i,
              boolean d_g_i, LineGraph line_i)
	{
					// 背景色
		setBackground(Color.white);
					// テーブルデータの保存
		title   = title_i;
		g_title = g_title_i;
		x_scale = x_scale_i;
		place_x = place_x_i;
		y_scale = y_scale_i;
		place_y = place_y_i;
		data_x  = data_x_i;
		data_y  = data_y_i;
		d_t     = d_t_i;
		d_g     = d_g_i;
		type    = false;
		line    = line_i;
					// イベントアダプタ
		addMouseListener(new ClickMouse(this));
	}
	/********/
	/* 描画 */
	/********/
	public void paintComponent (Graphics g)
	{
		super.paintComponent(g);   // 親クラスの描画(必ず必要)
		double r, x1, y1, sp;
		int i1, i2, cr, k, k_x, k_y, k1, k2, kx, kx1, ky, ky1, han, len;
		int x_l, x_r, y_u, y_d;   // 描画領域
		int f_size;   // フォントサイズ
		int n_p;   // データの数
		String s1;
		Font f;
		FontMetrics fm;
		Graphics2D g2 = (Graphics2D)g;
					//
					// Windowサイズの取得
					//
		Insets insets = line.getInsets();
		Dimension d = line.getSize();
		width  = d.width - (insets.left + insets.right);
		height = d.height - (insets.top + insets.bottom);
		x_l    = insets.left + 10;
		x_r    = d.width - insets.right - 10;
		y_u    = 20;
		y_d    = d.height - insets.bottom - insets.top;
					//
					// グラフタイトルの表示
					//
		r      = 0.05;   // タイトル領域の割合
		f_size = ((y_d - y_u) < (x_r - x_l)) ? (int)((y_d - y_u) * r) : (int)((x_r - x_l) * r);
		if (f_size < 5)
			f_size = 5;
		if (d_t) {
			f = new Font("TimesRoman", Font.BOLD, f_size);
			g.setFont(f);
			fm  = g.getFontMetrics(f);
			len = fm.stringWidth(title[0]);
			g.drawString(title[0], (x_l+x_r)/2-len/2, y_d-f_size/2);
			y_d -= f_size;
		}
					//
					// 表示切り替えボタンの設置
					//
		f_size = (int)(0.8 * f_size);
		if (f_size < 5)
			f_size = 5;
		f  = new Font("TimesRoman", Font.PLAIN, f_size);
		fm = g.getFontMetrics(f);
		g.setFont(f);
		g.setColor(Color.yellow);
		len = fm.stringWidth(change);
		bx1 = x_r - len - 7 * f_size / 10;
		by1 = y_u - f_size / 2;
		bx2 = bx1 + len + f_size / 2;
		by2 = by1 + 6 * f_size / 5;
		g.fill3DRect(bx1, by1, len+f_size/2, 6*f_size/5, true);
		g.setColor(Color.black);
		g.drawString(change, x_r-len-f_size/2, y_u+f_size/2);
					//
					// 凡例の表示
					//
		n_g = g_title.length;
		if (d_g) {
			han = 0;
			for (i1 = 0; i1 < n_g; i1++) {
				len = fm.stringWidth(g_title[i1]);
				if (len > han)
					han = len;
			}
			han += 15;
			r    = 0.2;   // 凡例領域の割合
			k1   = (int)((x_r - x_l) * r);
			if (han > k1)
				han = k1;
			kx = x_r - han;
			ky = y_u + 3 * f_size / 2;
			k  = 0;
			g2.setStroke(new BasicStroke(7.0f));
			for (i1 = 0; i1 < n_g; i1++) {
				g.setColor(cl[k]);
				g.drawLine(kx, ky, kx+10, ky);
				g.setColor(Color.black);
				g.drawString(g_title[i1], kx+15, ky+2*f_size/5);
				k++;
				if (k >= cl.length)
					k = 0;
				ky += f_size;
			}
			g2.setStroke(new BasicStroke(1.0f));
			x_r -= (han + 10);
		}
		else
			x_r -= (int)(0.03 * (x_r - x_l));
					//
					// x軸及びy軸のタイトルの表示
					//
		if (ver) {   // 縦
			if (title[1].length() > 0 && !title[1].equals("-")) {
				len = fm.stringWidth(title[1]);
				g.drawString(title[1], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
				y_d -= 7 * f_size / 4;
			}
			else
				y_d -= f_size / 2;
			if (title[2].length() > 0 && !title[2].equals("-")) {
				g.drawString(title[2], x_l, y_u+f_size/2);
				y_u += f_size;
			}
		}
		else {   // 横
			if (title[2].length() > 0 && !title[2].equals("-")) {
				len = fm.stringWidth(title[2]);
				g.drawString(title[2], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
				y_d -= 7 * f_size / 4;
			}
			else
				y_d -= f_size / 2;
			if (title[1].length() > 0 && !title[1].equals("-")) {
				g.drawString(title[1], x_l, y_u+f_size/2);
				y_u += f_size;
			}
		}
					//
					// x軸,y軸,及び,各軸の目盛り
					//
		f_size = (int)(0.8 * f_size);
		if (f_size < 5)
			f_size = 5;
		f    = new Font("TimesRoman", Font.PLAIN, f_size);
		fm   = g.getFontMetrics(f);
		y_d -= 3 * f_size / 2;
		k_y  = (int)((y_scale[1] - y_scale[0]) / (0.99 * y_scale[2]));
		k_x  = 0;
		if (!type)
			k_x = (int)((x_scale[1] - x_scale[0]) / (0.99 * x_scale[2]));
		g.setFont(f);
		DecimalFormat df_x, df_y;
		df_x = new DecimalFormat("#");
		df_y = new DecimalFormat("#");
		if (!type) {
			if (place_x != 0) {
				s1 = "#.";
				for (i1 = 0; i1 < place_x; i1++)
					s1 += "0";
				df_x = new DecimalFormat(s1);
			}
		}
		if (place_y != 0) {
			s1 = "#.";
			for (i1 = 0; i1 < place_y; i1++)
				s1 += "0";
			df_y = new DecimalFormat(s1);
		}
						// 縦表示
		if (ver) {
							// y軸
			y1  = y_scale[0];
			len = 0;
			for (i1 = 0; i1 < k_y+1; i1++) {
				s1 = df_y.format(y1);
				k1 = fm.stringWidth(s1);
				if (k1 > len)
					len = k1;
				y1 += y_scale[2];
			}
			g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
			g.drawLine(x_r, y_u, x_r, y_d);
			y1 = y_scale[0];
			x1 = y_d;
			sp = (double)(y_d - y_u) / k_y;
			for (i1 = 0; i1 < k_y+1; i1++) {
				ky = (int)Math.round(x1);
				s1 = df_y.format(y1);
				k1 = fm.stringWidth(s1);
				g.drawString(s1, x_l+len-k1, ky+f_size/2);
				g.drawLine(x_l+len+5, ky, x_r, ky);
				y1 += y_scale[2];
				x1 -= sp;
			}
			x_l += (len + 5);
							// x軸
			if (type) {
				n_p = x_title.length;
				sp  = (double)(x_r - x_l) / n_p;
				x1  = x_l + sp / 2.0;
				for (i1 = 0; i1 < n_p; i1++) {
					kx = (int)Math.round(x1);
					k1 = fm.stringWidth(x_title[i1]);
					g.drawString(x_title[i1], kx-k1/2, y_d+6*f_size/5);
					g.drawLine(kx, y_d, kx, y_d-5);
					x1 += sp;
				}
			}
			else {
				x1 = x_scale[0];
				y1 = x_l;
				sp = (double)(x_r - x_l) / k_x;
				for (i1 = 0; i1 < k_x+1; i1++) {
					kx = (int)Math.round(y1);
					s1 = df_x.format(x1);
					k1 = fm.stringWidth(s1);
					g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
					g.drawLine(kx, y_d, kx, y_u);
					x1 += x_scale[2];
					y1 += sp;
				}
			}
		}
						// 横表示
		else {
							// y軸
			if (type) {
				n_p = x_title.length;
				len = 0;
				for (i1 = 0; i1 < n_p; i1++) {
					k1 = fm.stringWidth(x_title[i1]);
					if (k1 > len)
						len = k1;
				}
				g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
				g.drawLine(x_r, y_u, x_r, y_d);
				sp = (double)(y_d - y_u) / n_p;
				x1 = y_d - sp / 2.0;
				for (i1 = 0; i1 < n_p; i1++) {
					ky = (int)Math.round(x1);
					k1 = fm.stringWidth(x_title[n_p-1-i1]);
					g.drawString(x_title[n_p-1-i1], x_l+len-k1, ky+f_size/2);
					g.drawLine(x_l+len+5, ky, x_l+len+10, ky);
					x1 -= sp;
				}
				g.drawLine(x_l+len+5, y_u, x_r, y_u);
				g.drawLine(x_l+len+5, y_d, x_r, y_d);
				x_l += (len + 5);
			}
			else {
				y1  = x_scale[0];
				len = 0;
				for (i1 = 0; i1 < k_x+1; i1++) {
					s1 = df_x.format(y1);
					k1 = fm.stringWidth(s1);
					if (k1 > len)
						len = k1;
					y1 += x_scale[2];
				}
				g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
				g.drawLine(x_r, y_u, x_r, y_d);
				y1 = x_scale[0];
				x1 = y_d;
				sp = (double)(y_d - y_u) / k_x;
				for (i1 = 0; i1 < k_x+1; i1++) {
					ky = (int)Math.round(x1);
					s1 = df_x.format(y1);
					k1 = fm.stringWidth(s1);
					g.drawString(s1, x_l+len-k1, ky+f_size/2);
					g.drawLine(x_l+len+5, ky, x_r, ky);
					y1 += x_scale[2];
					x1 -= sp;
				}
				x_l += (len + 5);
			}
							// x軸
			x1 = y_scale[0];
			y1 = x_l;
			sp = (double)(x_r - x_l) / k_y;
			for (i1 = 0; i1 < k_y+1; i1++) {
				kx = (int)Math.round(y1);
				s1 = df_y.format(x1);
				k1 = fm.stringWidth(s1);
				g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
				g.drawLine(kx, y_d, kx, y_u);
				x1 += y_scale[2];
				y1 += sp;
			}
		}
					//
					// グラフの表示
					//
		g2.setStroke(new BasicStroke(line_w));
		cr = (int)line_w + 6;
						// 縦表示
		if (ver) {
			if (type) {
				n_p = x_title.length;
				sp  = (double)(x_r - x_l) / n_p;
				k1  = 0;
				for (i1 = 0; i1 < n_g; i1++) {
					g.setColor(cl[k1]);
					x1  = x_l + sp / 2.0;
					kx1 = 0;
					ky1 = 0;
					for (i2 = 0; i2 < n_p; i2++) {
						kx = (int)Math.round(x1);
						ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
						if (line_m)
							g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
						if (i2 > 0)
							g.drawLine(kx1, ky1, kx, ky);
						kx1  = kx;
						ky1  = ky;
						x1  += sp;
					}
					k1++;
					if (k1 >= cl.length)
						k1 = 0;
				}
			}
			else {
				n_p = data_x[0].length;
				k1  = 0;
				for (i1 = 0; i1 < n_g; i1++) {
					g.setColor(cl[k1]);
					kx1 = 0;
					ky1 = 0;
					for (i2 = 0; i2 < n_p; i2++) {
						kx = x_l + (int)((x_r - x_l) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
						ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
						if (line_m)
							g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
						if (i2 > 0)
							g.drawLine(kx1, ky1, kx, ky);
						kx1 = kx;
						ky1 = ky;
					}
					k1++;
					if (k1 >= cl.length)
						k1 = 0;
				}
			}
		}
						// 横表示
		else {
			if (type) {
				n_p = x_title.length;
				sp = (double)(y_d - y_u) / n_p;
				k1 = 0;
				for (i1 = 0; i1 < n_g; i1++) {
					g.setColor(cl[k1]);
					y1  = y_d - sp / 2.0;
					kx1 = 0;
					ky1 = 0;
					for (i2 = 0; i2 < n_p; i2++) {
						ky = (int)Math.round(y1);
						kx = x_l + (int)((x_r - x_l) * (data_y[i1][n_p-1-i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
						if (line_m)
							g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
						if (i2 > 0)
							g.drawLine(kx1, ky1, kx, ky);
						kx1  = kx;
						ky1  = ky;
						y1  -= sp;
					}
					k1++;
					if (k1 >= cl.length)
						k1 = 0;
				}
			}
			else {
				n_p = data_x[0].length;
				k1  = 0;
				for (i1 = 0; i1 < n_g; i1++) {
					g.setColor(cl[k1]);
					kx1 = 0;
					ky1 = 0;
					for (i2 = 0; i2 < n_p; i2++) {
						kx = x_l + (int)((x_r - x_l) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
						ky = y_d - (int)((y_d - y_u) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
						if (line_m)
							g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
						if (i2 > 0)
							g.drawLine(kx1, ky1, kx, ky);
						kx1 = kx;
						ky1 = ky;
					}
					k1++;
					if (k1 >= cl.length)
						k1 = 0;
				}
			}
		}
		g2.setStroke(new BasicStroke(1.0f));
	}
	/************************************/
	/* マウスがクリックされたときの処理 */
	/************************************/
	class ClickMouse extends MouseAdapter
	{
		Draw_line dr;
		ClickMouse(Draw_line dr1)
		{
			dr = dr1;
		}
		public void mouseClicked(MouseEvent e)
		{
			int xp = e.getX();
			int yp = e.getY();
					// 縦表示と横表示の変換
			if (xp > bx1 && xp < bx1+(bx2-bx1)/2 && yp > by1 && yp < by2) {
				if (ver) {
					ver    = false;
					change = "縦 色";
				}
				else {
					ver    = true;
					change = "横 色";
				}
				repaint();
			}
					// グラフの色,線の太さ等
			else if (xp > bx1+(bx2-bx1)/2 && xp < bx2 && yp > by1 && yp < by2) {
				Modify md = new Modify(dr.line, dr);
				md.setVisible(true);
			}
		}
	}
}
/****************************/
/* 色及び線の太さの変更     */
/*      coded by Y.Suganuma */
/****************************/
class Modify extends JDialog implements ActionListener, TextListener {
	Draw_line dr;   // 折れ線グラフ
	JButton bt_dr;
	TextField rgb[];
	TextField r[];
	TextField g[];
	TextField b[];
	JTextField tx;
	JRadioButton r1, r2;
	float line_w = 1.0f;   // 折れ線グラフ等の線の太さ
	boolean line_m = true;   // 折れ線グラフ等にマークを付けるか否か
	Color cl[];   // グラフの色
	int n_g;   // グラフの数
	int wd;   // 線の太さを変更するか
	int mk;   // マークを変更するか
	int n;
	JPanel jp[];
					// 折れ線グラフ
	Modify(Frame host, Draw_line dr1)
	{
		super(host, "色と線の変更", true);
							// 初期設定
		dr  = dr1;
		wd  = 1;
		mk  = 1;
		n_g = dr.n_g;
		if (n_g > 10)
			n_g = 10;
		n      = n_g + 3;
		line_w = dr.line_w;
		line_m = dr.line_m;
		cl     = new Color[n_g];
		for (int i1 = 0; i1 < n_g; i1++)
			cl[i1] = dr.cl[i1];
		set();
							// ボタン
		Font f = new Font("TimesRoman", Font.BOLD, 20);
		bt_dr = new JButton("OK");
		bt_dr.setFont(f);
		bt_dr.addActionListener(this);
		jp[n-1].add(bt_dr);
	}
					// 設定
	void set()
	{
		setSize(450, 60*(n));
		Container cp = getContentPane();
		cp.setBackground(Color.white);
		cp.setLayout(new GridLayout(n, 1, 5, 5));
		jp = new JPanel[n];
		for (int i1 = 0; i1 < n; i1++) {
			jp[i1] = new JPanel();
			cp.add(jp[i1]);
		}
		Font f = new Font("TimesRoman", Font.BOLD, 20);
							// 色の変更
		JLabel lb[][] = new JLabel[n_g][3];
		rgb = new TextField[n_g];
		r = new TextField[n_g];
		g = new TextField[n_g];
		b = new TextField[n_g];
		for (int i1 = 0; i1 < n_g; i1++) {
			rgb[i1] = new TextField(3);
			rgb[i1].setFont(f);
			rgb[i1].setBackground(new Color(cl[i1].getRed(), cl[i1].getGreen(), cl[i1].getBlue()));
			jp[i1].add(rgb[i1]);
			lb[i1][0] = new JLabel(" 赤");
			lb[i1][0].setFont(f);
			jp[i1].add(lb[i1][0]);
			r[i1] = new TextField(3);
			r[i1].setFont(f);
			r[i1].setBackground(Color.white);
			r[i1].setText(Integer.toString(cl[i1].getRed()));
			r[i1].addTextListener(this);
			jp[i1].add(r[i1]);
			lb[i1][1] = new JLabel("緑");
			lb[i1][1].setFont(f);
			jp[i1].add(lb[i1][1]);
			g[i1] = new TextField(3);
			g[i1].setFont(f);
			g[i1].setBackground(Color.white);
			g[i1].setText(Integer.toString(cl[i1].getGreen()));
			g[i1].addTextListener(this);
			jp[i1].add(g[i1]);
			lb[i1][2] = new JLabel("青");
			lb[i1][2].setFont(f);
			jp[i1].add(lb[i1][2]);
			b[i1] = new TextField(3);
			b[i1].setFont(f);
			b[i1].setBackground(Color.white);
			b[i1].setText(Integer.toString(cl[i1].getBlue()));
			b[i1].addTextListener(this);
			jp[i1].add(b[i1]);
		}
							// 線の変更
		if (wd > 0) {
			JLabel lb1 = new JLabel("線の太さ:");
			lb1.setFont(f);
			jp[n_g].add(lb1);
			tx = new JTextField(2);
			tx.setFont(f);
			tx.setBackground(Color.white);
			tx.setText(Integer.toString((int)line_w));
			jp[n_g].add(tx);
		}
		if (mk > 0) {
			JLabel lb2 = new JLabel("マーク:");
			lb2.setFont(f);
			jp[n-2].add(lb2);
			ButtonGroup gp = new ButtonGroup();
			r1 = new JRadioButton("付ける");
			r1.setFont(f);
			gp.add(r1);
			jp[n-2].add(r1);
			r2 = new JRadioButton("付けない");
			r2.setFont(f);
			gp.add(r2);
			jp[n-2].add(r2);
			if (line_m)
				r1.doClick();
			else
				r2.doClick();
		}
	}
					// TextFieldの内容が変更されたときの処理
	public void textValueChanged(TextEvent e)
	{
		for (int i1 = 0; i1 < n_g; i1++) {
			if (e.getSource() == r[i1] || e.getSource() == g[i1] || e.getSource() == b[i1]) {
				String str = r[i1].getText();
				int rc = str.length()>0 ? Integer.parseInt(str) : 0;
				str = g[i1].getText();
				int gc = str.length()>0 ? Integer.parseInt(str) : 0;
				str = b[i1].getText();
				int bc = str.length()>0 ? Integer.parseInt(str) : 0;
				rgb[i1].setBackground(new Color(rc, gc, bc));
			}
		}
	}
					// 値の設定
	public void actionPerformed(ActionEvent e)
	{
		for (int i1 = 0; i1 < n_g; i1++) {
			String str = r[i1].getText();
			int rc = str.length()>0 ? Integer.parseInt(str) : 0;
			str = g[i1].getText();
			int gc = str.length()>0 ? Integer.parseInt(str) : 0;
			str = b[i1].getText();
			int bc = str.length()>0 ? Integer.parseInt(str) : 0;
			dr.cl[i1] = new Color(rc, gc, bc);
		}
		dr.line_w = Integer.parseInt(tx.getText());
		if (r1.isSelected())
			dr.line_m = true;
		else
			dr.line_m = false;
		dr.repaint();
		setVisible(false);
	}
}
			
<!DOCTYPE HTML>
<HTML>
<HEAD>
	<TITLE>二項分布</TITLE>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
	<SCRIPT TYPE="text/javascript">
		method = 0;
		function main()
		{
			let n = parseInt(document.getElementById("n").value);
			let p = parseFloat(document.getElementById("p").value);
			let z = new Array();
					// 1点
			if (method == 0) {
				let x = parseInt(document.getElementById("x").value);
				let y = binomial(x, n, p, z);
				let str = "P(X = " + x + ") = " + z[0] + "\n";
				str = str + "P(X < " + x + ") = " + y;
				document.getElementById("xx").value = str;
			}
					// 複数点
			else {
							// データ取得
				let x1 = new Array();
				let y1 = new Array();
				let y2 = new Array();
				let mx = 0;
				for (let x = 0; x <= n; x++) {
					let y = binomial(x, n, p, z);
					x1[x] = x;
					y1[x] = z[0];
					y2[x] = y;
					if (z[0] > mx)
						mx = z[0];
				}
				let str1 = "密度関数\n";
				let str2 = "分布関数\n";
				for (let x = 0; x <= n; x++) {
					str1 = str1 + x1[x] + " " + y1[x] + "\n";
					str2 = str2 + x1[x] + " " + y2[x] + "\n";
				}
				document.getElementById("xx").value = str1;
				document.getElementById("yy").value = str2;
							// グラフの描画
				let gp1 = "2,二項分布(密度関数),x,f(x),1,グラフ1,0.0," + n;
				let sp1 = Math.floor(n / 5);
				let sp2 = Math.floor(mx / 5 * 100);
				if (sp2 % 5 > 0)
					sp2 = sp2 + 5 - sp2 % 5;
				sp2 /= 100;
				mx = sp2 * 5;
				gp1 = gp1 + "," + sp1 + ",0,0.0," + mx + "," + sp2 + ",2," + (n+1);
				for (let x = 0; x <= n; x++)
					gp1 = gp1 + "," + x1[x];
				for (let x = 0; x <= n; x++)
					gp1 = gp1 + "," + y1[x];
				gp1 = gp1 + ",1,0";
				let str = "graph_js.htm?gp=" + gp1;
				open(str, "density", "width=950, height=700");
				let gp2 = "2,二項分布(分布関数),x,F(x),1,グラフ1,0.0," + n;
				sp2 = 0.2;
				mx = 1.0;
				gp2 = gp2 + "," + sp1 + ",0,0.0," + mx + "," + sp2 + ",1," + (n+1);
				for (let x = 0; x <= n; x++)
					gp2 = gp2 + "," + x1[x];
				for (let x = 0; x <= n; x++)
					gp2 = gp2 + "," + y2[x];
				gp2 = gp2 + ",1,0";
				str = "graph_js.htm?gp=" + gp2;
				open(str, "distribution", "width=950, height=700");
			}
		}
		/****************************************/
		/* 二項分布の計算(P(X = x), P(X < x)) */
		/*      x : データ                      */
		/*      n,p : パラメータ                */
		/*      pr : P(X = x)                   */
		/*      return : P(X < x)               */
		/****************************************/
		function binomial(x, n, p, pr)
		{
			let f = 0.0;
			let q;
			let i1;
			q     = 1.0 - p;
			pr[0] = comb(n, x) * Math.pow(p, x) * Math.pow(q, n-x);
			f     = pr[0];
			for (i1 = 0; i1 < x; i1++)
				f += comb(n, i1) * Math.pow(p, i1) * Math.pow(q, n-i1);
			return f;
		}
		/**************/
		/* 組合せ nCr */
		/**************/
		function comb(n, r)
		{
			let c = 1.0;
			let x = 1.0;
			let y = 1.0;
			let i1;
			if (r > 0) {
				if (r > n-r)
					r = n - r;
				for (i1 = n; i1 >= n-r+1; i1--)
					x *= i1;
				for (i1 = 2; i1 <= r; i1++)
					y *= i1;
				c = x / y;
			}
			return c;
		}
		/********/
		/* 目的 */
		/********/
		function set_m()
		{
			let sel = document.getElementById("method");
			for (let i1 = 0; i1 < 2; i1++) {
				if (sel.options[i1].selected) {
					method = i1;
					break;
				}
			}
							// 1点
			if (method == 0) {
				document.getElementById("x_t").style.display = "";
				document.getElementById("yy").style.display = "none";
			}
							// 複数点
			else {
				document.getElementById("x_t").style.display = "none";
				document.getElementById("yy").style.display = "";
			}
		}
	</SCRIPT>
</HEAD>
<BODY STYLE="font-size: 130%; text-align:center; background-color: #eeffee;">
	<H2 STYLE="text-align:center"><B>二項分布</B></H2>
	n:<INPUT ID="n" STYLE="font-size: 100%" TYPE="text" SIZE="3" VALUE="30"> 
	p:<INPUT ID="p" STYLE="font-size: 100%" TYPE="text" SIZE="3" VALUE="0.2"> 
	<SPAN ID="x_t">x:<INPUT ID="x" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="5"> </SPAN>
	目的:<SELECT ID="method" onChange="set_m()" STYLE="font-size:100%">
		<OPTION SELECTED>確率(1点)
		<OPTION>確率(複数点)
	</SELECT> 
	<BUTTON STYLE="font-size: 100%; background-color: pink" onClick="main()">OK</BUTTON><BR><BR>
	<TEXTAREA ID="xx" COLS="30" ROWS="30" STYLE="font-size: 100%"></TEXTAREA>
	<TEXTAREA ID="yy" COLS="30" ROWS="30" STYLE="font-size: 100%; display: none"></TEXTAREA>
</BODY>
</HTML>
			
<!DOCTYPE HTML>
<HTML>
<HEAD>
	<TITLE>グラフの表示</TITLE>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
	<LINK REL="stylesheet" TYPE="text/css" HREF="../master.css">
	<SCRIPT TYPE="text/javascript" SRC="graph.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript">
		function GetParameter()
		{
			let result = new Array();
			if(1 < window.location.search.length) {
							// 最初の1文字 (?記号) を除いた文字列を取得する
				let str = window.location.search.substring(1);
							// 区切り記号 (&) で文字列を配列に分割する
				let param = str.split('&');
				for(let i1 = 0; i1 < param.length; i1++ ) {
							// パラメータ名とパラメータ値に分割する
					let element = param[i1].split('=');
					let Name = decodeURIComponent(element[0]);
					let Value = decodeURIComponent(element[1]);
							// パラメータ名をキーとして連想配列に追加する
					result[Name] = Value;
				}
			}
			return result;
		}
	</SCRIPT>
</HEAD>
<BODY CLASS="white" STYLE="text-align: center">
	<DIV ID="cl_line" STYLE="text-align: center; display: none">
		<FORM>
			<DIV ID="c0">
				<INPUT ID="rgb0" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)"> 
				緑<INPUT ID="g0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)"> 
				青<INPUT ID="b0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
			</DIV>
			<DIV ID="c1">
				<INPUT ID="rgb1" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)"> 
				緑<INPUT ID="g1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)"> 
				青<INPUT ID="b1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
			</DIV>
			<DIV ID="c2">
				<INPUT ID="rgb2" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)"> 
				緑<INPUT ID="g2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)"> 
				青<INPUT ID="b2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
			</DIV>
			<DIV ID="c3">
				<INPUT ID="rgb3" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)"> 
				緑<INPUT ID="g3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)"> 
				青<INPUT ID="b3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
			</DIV>
			<DIV ID="c4">
				<INPUT ID="rgb4" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)"> 
				緑<INPUT ID="g4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)"> 
				青<INPUT ID="b4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
			</DIV>
			<DIV ID="c5">
				<INPUT ID="rgb5" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)"> 
				緑<INPUT ID="g5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)"> 
				青<INPUT ID="b5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
			</DIV>
			<DIV ID="c6">
				<INPUT ID="rgb6" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)"> 
				緑<INPUT ID="g6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)"> 
				青<INPUT ID="b6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
			</DIV>
			<DIV ID="c7">
				<INPUT ID="rgb7" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)"> 
				緑<INPUT ID="g7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)"> 
				青<INPUT ID="b7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
			</DIV>
			<DIV ID="c8">
				<INPUT ID="rgb8" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)"> 
				緑<INPUT ID="g8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)"> 
				青<INPUT ID="b8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
			</DIV>
			<DIV ID="c9">
				<INPUT ID="rgb9" TYPE="text" SIZE="3" STYLE="font-size: 90%"> 
				赤<INPUT ID="r9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)"> 
				緑<INPUT ID="g9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)"> 
				青<INPUT ID="b9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
			</DIV>
			<DIV ID="line_m">
				マーク:<INPUT ID="l_m1" TYPE="radio" NAME="mark" STYLE="font-size: 90%" CHECKED>付ける 
				<INPUT ID="l_m2" TYPE="radio" NAME="mark" STYLE="font-size: 90%">付けない
			</DIV>
			<DIV ID="line_w">
				線の太さ:<INPUT ID="l_w" TYPE="text" SIZE="3" STYLE="font-size: 90%" VALUE="2">
			</DIV>
			<DIV>
				<SPAN STYLE="background-color: pink; font-size: 100%" onClick="D_Change()">OK</SPAN>
			</DIV>
		</FORM>
	</DIV>
	<BR>
	<DIV STYLE="text-align: center">
		<CANVAS ID="canvas_e" STYLE="background-color: #eeffee;" WIDTH="900" HEIGHT="600" onClick="Click(event)"></CANVAS>
	</DIV>
	<SCRIPT TYPE="text/javascript">
		let result = GetParameter();
		graph(result['gp']);
	</SCRIPT>
</BODY>
</HTML>
			
<?php
/****************************/
/* 二項分布の計算           */
/*      coded by Y.Suganuma */
/****************************/
/**************/
/* 組合せ nCr */
/**************/
function comb($n, $r)
{
	$c = 1.0;
	$x = 1.0;
	$y = 1.0;
	if ($r > 0) {
		if ($r > $n-$r)
			$r = $n - $r;
		for ($i1 = $n; $i1 >= $n-$r+1; $i1--)
			$x *= $i1;
		for ($i1 = 2; $i1 <= $r; $i1++)
			$y *= $i1;
		$c = $x / $y;
	}
	return $c;
}
/****************************************/
/* 二項分布の計算(P(X = x), P(X < x)) */
/*      x : データ                      */
/*      n,p : パラメータ                */
/*      pr : P(X = x)                   */
/*      return : P(X < x)               */
/****************************************/
function binomial($x, $n, $p, &$pr)
{
	$f  = 0.0;
	$q  = 1.0 - $p;
	$pr = comb($n, $x) * pow($p, $x) * pow($q, $n-$x);
	$f  = $pr;
	for ($i1 = 0; $i1 < $x; $i1++)
		$f += comb($n, $i1) * pow($p, $i1) * pow($q, $n-$i1);
	return $f;
}
/********/
/* main */
/********/
	printf("n は? ");
	fscanf(STDIN, "%d", $n);
	printf("p は? ");
	fscanf(STDIN, "%lf", $p);
	printf("グラフ出力?(=1: yes,  =0: no) ");
	fscanf(STDIN, "%d", $sw);
					// 密度関数と分布関数の値
	if ($sw == 0) {
		printf("   データは? ");
		fscanf(STDIN, "%d", $x);
		$f = binomial($x, $n, $p, $pr);
		printf("P(X = %d) = %f,  P( X < %d) = %f (n = %d, p = %f)\n", $x, $pr, $x, $f, $n, $p);
	}
					// グラフ出力
	else {
		printf("   密度関数のファイル名は? ");
		fscanf(STDIN, "%s", $file1);
		printf("   分布関数のファイル名は? ");
		fscanf(STDIN, "%s", $file2);
		$out1 = fopen($file1,"w");
		$out2 = fopen($file2,"w");
		for ($x = 0; $x <= $n; $x++) {
			$f = binomial($x, $n, $p, $pr);
			fwrite($out1, $x." ".$pr."\n");
			fwrite($out2, $x." ".$f."\n");
		}
	}
?>
			
############################
# 二項分布の計算
#      coded by Y.Suganuma
###########################
############
# 組合せ nCr
############
def comb(n, r)
	c = 1.0
	x = 1.0
	y = 1.0
	if r > 0
		if r > n-r
			r = n - r
		end
		i1 = n
		while i1 > n-r
			x  *= i1
			i1 -= 1
		end
		for i1 in 2 ... r+1
			y *= i1
		end
		c = x / y
	end
	return c
end
######################################
# 二項分布の計算(P(X = x), P(X < x))
#      x : データ
#      n,p : パラメータ
#      pr : P(X = x)
#      return : P(X < x)
#      coded by Y.Suganuma
######################################
def binomial(x, n, p, pr)
	q     = 1.0 - p
	pr[0] = comb(n, x) * (p ** x) * (q ** (n-x))
	f     = pr[0]
	for i1 in 0 ... x
		f += comb(n, i1) * (p ** i1) * (q ** (n-i1))
	end
	return f
end
print("n は? ")
n = Integer(gets())
print("p は? ")
p = Float(gets())
print("グラフ出力?(=1: yes,  =0: no) ")
sw = Integer(gets())
pr = Array.new(1)
			# 密度関数と分布関数の値
if sw == 0
	print("   データは? ")
	x = Integer(gets())
	f = binomial(x, n, p, pr)
	print("P(X = " + String(x) + ") = " + String(pr[0]) + ",  P( X < " + String(x) + ") = " + String(f) + " (n = " + String(n) + ", p = " + String(p) + ")\n")
			# グラフ出力
else
	print("   密度関数のファイル名は? ")
	file1 = gets().strip()
	print("   分布関数のファイル名は? ")
	file2 = gets().strip()
	out1  = open(file1, "w")
	out2  = open(file2, "w")
	for x in 0 ... n+1
		f = binomial(x, n, p, pr)
		out1.print(String(x) + " " + String(pr[0]) + "\n")
		out2.print(String(x) + " " + String(f) + "\n")
	end
	out1.close()
	out2.close()
end
			
# -*- coding: UTF-8 -*-
import numpy as np
import sys
from math import *
############
# 組合せ nCr
############
def comb(n, r) :
	c = 1.0
	x = 1.0
	y = 1.0
	if r > 0 :
		if r > n-r :
			r = n - r
		for i1 in range(n, n-r, -1) :
			x *= i1
		for i1 in range(2, r+1) :
			y *= i1
		c = x / y
	return c
######################################
# 二項分布の計算(P(X = x), P(X < x))
#      x : データ
#      n,p : パラメータ
#      pr : P(X = x)
#      return : P(X < x)
#      coded by Y.Suganuma
######################################
def binomial(x, n, p, pr) :
	q     = 1.0 - p
	pr[0] = comb(n, x) * pow(p, x) * pow(q, n-x)
	f     = pr[0]
	for i1 in range(0, x) :
		f += comb(n, i1) * pow(p, i1) * pow(q, n-i1)
	return f
############################
# 二項分布の計算
#      coded by Y.Suganuma
###########################
s  = input("n は? ")
n  = int(s)
s  = input("p は? ")
p  = float(s)
s  = input("グラフ出力?(=1: yes,  =0: no) ")
sw = int(s)
pr = np.empty(1, np.float)
			# 密度関数と分布関数の値
if sw == 0 :
	s = input("   データは? ")
	x = int(s)
	f = binomial(x, n, p, pr)
	print("P(X = " + str(x) + ") = " + str(pr[0]) + ",  P( X < " + str(x) + ") = " + str(f) + " (n = " + str(n) + ", p = " + str(p) + ")")
			# グラフ出力
else :
	file1 = input("   密度関数のファイル名は? ")
	file2 = input("   分布関数のファイル名は? ")
	out1  = open(file1, "w")
	out2  = open(file2, "w")
	for x in range(0, n+1) :
		f = binomial(x, n, p, pr)
		out1.write(str(x) + " " + str(pr[0]) + "\n")
		out2.write(str(x) + " " + str(f) + "\n")
	out1.close()
	out2.close()
			
/****************************/
/* 二項分布の計算           */
/*      coded by Y.Suganuma */
/****************************/
using System;
using System.IO;
class Program
{
	static void Main()
	{
		Test1 ts = new Test1();
	}
}
class Test1
{
	/********/
	/* main */
	/********/
	public Test1()
	{
		Console.Write("n は? ");
		int n = int.Parse(Console.ReadLine());
		Console.Write("p は? ");
		double p = double.Parse(Console.ReadLine());
		Console.Write("グラフ出力?(=1: yes,  =0: no) ");
		int sw = int.Parse(Console.ReadLine());
					// 密度関数と分布関数の値
		double pr = 0.0, f;
		if (sw == 0) {
			Console.Write("   データは? ");
			int x = int.Parse(Console.ReadLine());
			f     = binomial(x, n, p, ref pr);
			Console.WriteLine("P(X = " + x + ") = " + pr + ",  P( X < " + x + ") = " + f + " (n = " + n + ", p = " + p + ")");
		}
					// グラフ出力
		else {
			Console.Write("   密度関数のファイル名は? ");
			String file1 = Console.ReadLine();
			Console.Write("   分布関数のファイル名は? ");
			String file2 = Console.ReadLine();
			StreamWriter out1 = new StreamWriter(file1);
			StreamWriter out2 = new StreamWriter(file2);
			for (int x = 0; x <= n; x++) {
				f = binomial(x, n, p, ref pr);
				out1.WriteLine(x + " " + pr);
				out2.WriteLine(x + " " + f);
			}
			out1.Close();
			out2.Close();
		}
	}
	/**************/
	/* 組合せ nCr */
	/**************/
	double comb(int n, int r)
	{
		double c = 1.0, x = 1.0, y = 1.0;
		if (r > 0) {
			if (r > n-r)
				r = n - r;
			for (int i1 = n; i1 >= n-r+1; i1--)
				x *= i1;
			for (int i1 = 2; i1 <= r; i1++)
				y *= i1;
			c = x / y;
		}
		return c;
	}
	/****************************************/
	/* 二項分布の計算(P(X = x), P(X < x)) */
	/*      x : データ                      */
	/*      n,p : パラメータ                */
	/*      pr : P(X = x)                   */
	/*      return : P(X < x)               */
	/****************************************/
	double binomial(int x, int n, double p, ref double pr)
	{
		double q = 1.0 - p;
		pr       = comb(n, x) * Math.Pow(p, x) * Math.Pow(q, n-x);
		double f = pr;
		for (int i1 = 0; i1 < x; i1++)
			f += comb(n, i1) * Math.Pow(p, i1) * Math.Pow(q, n-i1);
		return f;
	}
}
			
'**************************'
' 二項分布の計算           '
'      coded by Y.Suganuma '
'**************************'
Imports System.IO
Module Test
	Sub Main()
		Console.Write("n は? ")
		Dim n As Integer = Integer.Parse(Console.ReadLine())
		Console.Write("p は? ")
		Dim p As Double = Double.Parse(Console.ReadLine())
		Console.Write("グラフ出力?(=1: yes,  =0: no) ")
		Dim sw As Integer = Integer.Parse(Console.ReadLine())
					' 密度関数と分布関数の値
		Dim pr As Double = 0.0
		Dim f As Double
		If sw = 0
			Console.Write("   データは? ")
			Dim x As Integer = Integer.Parse(Console.ReadLine())
			f = binomial(x, n, p, pr)
			Console.WriteLine("P(X = " & x & ") = " & pr & ",  P( X < " & x & ") = " & f & " (n = " & n & ", p = " & p & ")")
					' グラフ出力
		Else
			Console.Write("   密度関数のファイル名は? ")
			Dim file1 As String = Console.ReadLine().Trim()
			Console.Write("   分布関数のファイル名は? ")
			Dim file2 As String = Console.ReadLine().Trim()
			Dim out1 As StreamWriter = new StreamWriter(file1)
			Dim out2 As StreamWriter = new StreamWriter(file2)
			For x As Integer = 0 To n
				f = binomial(x, n, p, pr)
				out1.WriteLine(x & " " & pr)
				out2.WriteLine(x & " " & f)
			Next
			out1.Close()
			out2.Close()
		End If
	End Sub
	'************'
	' 組合せ nCr '
	'************'
	Function comb(n As Integer, r As Integer)
		Dim c As Double = 1.0
		Dim x As Double = 1.0
		Dim y As Double = 1.0
		If r > 0
			If r > n-r
				r = n - r
			End If
			For i1 As Integer = n To n-r+1 Step -1
				x *= i1
			Next
			For i1 As Integer = 2 To r
				y *= i1
			Next
			c = x / y
		End If
		Return c
	End Function
	'**************************************'
	' 二項分布の計算(P(X = x), P(X < x)) '
	'      x : データ                      '
	'      n,p : パラメータ                '
	'      pr : P(X = x)                   '
	'      return : P(X < x)               '
	'**************************************'
	Function binomial(x As Integer, n As Integer, p As Double, ByRef pr As Double)
		Dim q As Double = 1.0 - p
		pr              = comb(n, x) * Math.Pow(p, x) * Math.Pow(q, n-x)
		Dim f As Double = pr
		For i1 As Integer = 0 To x-1
			f += comb(n, i1) * Math.Pow(p, i1) * Math.Pow(q, n-i1)
		Next
		Return f
	End Function
End Module
			
| 情報学部 | 菅沼ホーム | 目次 | 索引 |