码迷,mamicode.com
首页 > 其他好文 > 详细

[从头学数学] 第197节 统计案例

时间:2016-04-26 20:00:55      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

剧情提要:
[机器小伟]在[工程师阿伟]的陪同下进入了[九转金丹]之第五转的修炼。
这次要研究的是[统计案例]。

正剧开始:


星历2016年04月26日 16:34:30, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[统计案例]。


技术分享


技术分享

技术分享

技术分享

技术分享

<span style="font-size:18px;">X= [165, 165, 157, 170, 175, 165, 155, 170]
Y= [48, 57, 50, 54, 64, 61, 43, 59]
拟合结果: y =    0.84848 x +  -85.71212 , r=   0.79847

def linefit(x , y):    
    N = float(len(x))    
    sx,sy,sxx,syy,sxy=0,0,0,0,0    
    for i in range(0,int(N)):    
        sx  += x[i]    
        sy  += y[i]    
        sxx += x[i]*x[i]    
        syy += y[i]*y[i]    
        sxy += x[i]*y[i]    
    a = (sy*sx/N -sxy)/( sx*sx/N -sxx)    
    b = (sy - a*sx)/N    
    r = abs(sy*sx/N-sxy)/math.sqrt((sxx-sx*sx/N)*(syy-sy*sy/N))    
    return a,b,r    
    
    
def tmp():    
    X=[ 165, 165, 157, 170, 175, 165, 155, 170];   
    Y=[ 48, 57, 50, 54, 64, 61, 43, 59];        
    a,b,r=linefit(X,Y)    
    print("X=",X)    
    print("Y=",Y)    
    print("拟合结果: y = %10.5f x + %10.5f , r=%10.5f" % (a,b,r));    
	</span>

<span style="font-size:18px;">	 if (1) {    
        var r = 20;          
        config.setSector(10,5,9,1);            
        config.graphPaper2D(0, 0, r);          
        config.axis2D(0, 0, 320, 1.6);            
              
        //坐标轴设定      
        var scaleX = 2*r, scaleY = 2*r;        
        var spaceX = 2, spaceY = 10;         
        var xS = 150, xE = 180;        
        var yS = 0, yE = 70;        
        config.axisSpacing(xS, xE, spaceX, scaleX, 'X');          
        config.axisSpacing(yS, yE, spaceY, scaleY, 'Y');       
            
            
        var X=[ 165, 165, 157, 170, 175, 165, 155, 170],     
            Y=[ 48, 57, 50, 54, 64, 61, 43, 59];    
                
        var array = [];    
        var size = X.length;    
            
        for (var i = 0; i < size; i++) {    
            array.push([X[i], Y[i]]);    
        }    
                
        var transform = new Transform();    
        var tmp = [];    
            
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);        
        tmp = [].concat(array);          
        shape.pointDraw(tmp, 'green');         
  
        array = [];  
        for (var i = 0; i < size; i++) {    
            array.push([X[i], taskFun(X[i])]);    
        }         
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);    
        tmp = [].concat(array);          
        shape.multiLineDraw(tmp, 'red');       
  
        plot.setFillStyle('blue');  
        plot.fillText('拟合结果: y = 0.84848x -85.71212', 30, -270, 200);  
                
        
    } 
      
      
}  
  
function taskFun(x) {  
    return 0.84848*x-85.71212;
}</span>

技术分享

技术分享

技术分享

技术分享

<span style="font-size:18px;">[21, 23, 25, 27, 29, 32, 35]
[1.9459101490553132, 2.3978952727983707, 3.044522437723423, 3.1780538303479458, 4.189654742026425, 4.74493212836325, 5.783825182329737]

X= [21, 23, 25, 27, 29, 32, 35]
Y= [1.9459101490553132, 2.3978952727983707, 3.044522437723423, 3.1780538303479458, 4.189654742026425, 4.74493212836325, 5.783825182329737]
拟合结果: y =    0.27203 x +   -3.84917 , r=   0.99260</span>

技术分享

<span style="font-size:18px;">	 if (1) {    
        var r = 20;          
        config.setSector(10,5,9,1);            
        config.graphPaper2D(0, 0, r);          
        config.axis2D(0, 0, 320, 1.6);            
              
        //坐标轴设定      
        var scaleX = 2*r, scaleY = 2*r;        
        var spaceX = 1.5, spaceY = 1;         
        var xS = 20, xE = 36;        
        var yS = 0, yE = 7;        
        config.axisSpacing(xS, xE, spaceX, scaleX, 'X');          
        config.axisSpacing(yS, yE, spaceY, scaleY, 'Y');       
            
            
		var X = [21, 23, 25, 27, 29, 32, 35];
		var Y = [1.9459101490553132, 2.3978952727983707, 3.044522437723423, 3.1780538303479458, 4.189654742026425, 4.74493212836325, 5.783825182329737];
			
		
                
        var array = [];    
        var size = X.length;    
            
        for (var i = 0; i < size; i++) {    
            array.push([X[i], Y[i]]);    
        }    
                
        var transform = new Transform();    
        var tmp = [];    
            
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);        
        tmp = [].concat(array);          
        shape.pointDraw(tmp, 'green');         
  
        array = [];  
        for (var i = 0; i < size; i++) {    
            array.push([X[i], taskFun(X[i])]);    
        }         
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);    
        tmp = [].concat(array);          
        shape.multiLineDraw(tmp, 'red');       
  
        plot.setFillStyle('blue');  
        plot.fillText('拟合结果: y = 0.27203x-3.84917', 30, -270, 200);  
                
        
    } 
      
      
}  
  
function taskFun(x) {  
    return 0.27203*x-3.84917;
}
</span>

技术分享


<span style="font-size:18px;">>>> 
置信度 > 99.9%, K^2 = 56.631879146114834

#独立性检验
def tmp3():
    #数据
    #[a, b]
    #[c, d]
    a = 7775;
    b = 42;
    c = 2099;
    d = 49;

    kSquare = (a+b+c+d)*(a*d-b*c)**2/(a+b)/(c+d)/(a+c)/(b+d);

    #置信度查对表
    trust = [[0.5,0.455],[0.4,0.708],[0.25,1.323],[0.15,2.072],[0.1,2.706],             [0.025,5.024],[0.01,6.635],[0.005,7.879],[0.001,10.828]];

    size = len(trust);

    for i in range(size-1, -1, -1):
        if kSquare >= trust[i][1]:
            print('置信度 > {0}%, K^2 = {1}'.format(round((1-trust[i][0])*100, 3), kSquare));
            return trust[i][0];</span>

技术分享

技术分享

技术分享

<span style="font-size:18px;">>>> 
置信度 > 99.9%, K^2 = 16.37320688824579

#独立性检验
#例1
def tmp3():
    #数据
    #[a, b]
    #[c, d]
    a = 214
    b = 175
    c = 451
    d = 597

    kSquare = (a+b+c+d)*(a*d-b*c)**2/(a+b)/(c+d)/(a+c)/(b+d);

    #置信度查对表
    trust = [[0.5,0.455],[0.4,0.708],[0.25,1.323],[0.15,2.072],[0.1,2.706],             [0.025,5.024],[0.01,6.635],[0.005,7.879],[0.001,10.828]];

    size = len(trust);

    for i in range(size-1, -1, -1):
        if kSquare >= trust[i][1]:
            print('置信度 > {0}%, K^2 = {1}'.format(round((1-trust[i][0])*100, 3), kSquare));
            return trust[i][0];</span>

技术分享

技术分享

<span style="font-size:18px;">>>> 
置信度 > 97.5%, K^2 = 6.109090909090909

#独立性检验
#题1
def tmp3():
    #数据
    #[a, b]
    #[c, d]
    a = 10
    b = 45
    c = 20
    d = 30

    kSquare = (a+b+c+d)*(a*d-b*c)**2/(a+b)/(c+d)/(a+c)/(b+d);

    #置信度查对表
    trust = [[0.5,0.455],[0.4,0.708],[0.25,1.323],[0.15,2.072],[0.1,2.706],             [0.025,5.024],[0.01,6.635],[0.005,7.879],[0.001,10.828]];

    size = len(trust);

    for i in range(size-1, -1, -1):
        if kSquare >= trust[i][1]:
            print('置信度 > {0}%, K^2 = {1}'.format(round((1-trust[i][0])*100, 3), kSquare));
            return trust[i][0];
			</span>


技术分享

技术分享

<span style="font-size:18px;">>>> 
X= [126.974, 96.933, 86.656, 63.438, 55.264, 50.976, 39.069, 36.156, 35.209, 32.416]
Y= [4.224, 3.835, 3.51, 3.758, 3.939, 1.809, 2.946, 0.359, 2.48, 2.413]
拟合结果: y =    0.02556 x +    1.33452 , r=   0.67615</span>

技术分享

<span style="font-size:18px;">	 if (1) {    
        var r = 20;          
        config.setSector(10,5,9,1);            
        config.graphPaper2D(0, 0, r);          
        config.axis2D(0, 0, 320, 1.6);            
              
        //坐标轴设定      
        var scaleX = 2*r, scaleY = 2*r;        
        var spaceX = 10, spaceY = 1;         
        var xS = 0, xE = 150;        
        var yS = 0, yE = 10;        
        config.axisSpacing(xS, xE, spaceX, scaleX, 'X');          
        config.axisSpacing(yS, yE, spaceY, scaleY, 'Y');       
            
            
		var X = [126.974, 96.933,86.656,63.438,55.264,50.976,39.069,36.156,35.209,32.416];
		var Y = [4.224,3.835,3.510,3.758,3.939,1.809,2.946,0.359,2.480,2.413];
			
		
                
        var array = [];    
        var size = X.length;    
            
        for (var i = 0; i < size; i++) {    
            array.push([X[i], Y[i]]);    
        }    
                
        var transform = new Transform();    
        var tmp = [];    
            
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);        
        tmp = [].concat(array);          
        shape.pointDraw(tmp, 'green');         
  
        array = [];  
        for (var i = 0; i < size; i++) {    
            array.push([X[i], taskFun(X[i])]);    
        }         
        array = transform.scale(transform.translate(array, -xS, -yS), scaleX/spaceX, scaleY/spaceY);    
        tmp = [].concat(array);          
        shape.multiLineDraw(tmp, 'red');       
  
        plot.setFillStyle('blue');  
        plot.fillText('拟合结果: y = 0.02556*x + 1.33452', 30, -270, 200);  
                
        
    } </span>


技术分享

<span style="font-size:18px;">置信度 > 90.0%, K^2 = 3.6889201613659814

#独立性检验
#题3
def tmp3():
    #数据
    #[a, b]
    #[c, d]
    a = 24
    b = 31
    c = 8
    d = 26

    kSquare = (a+b+c+d)*(a*d-b*c)**2/(a+b)/(c+d)/(a+c)/(b+d);

    #置信度查对表
    trust = [[0.5,0.455],[0.4,0.708],[0.25,1.323],[0.15,2.072],[0.1,2.706],             [0.025,5.024],[0.01,6.635],[0.005,7.879],[0.001,10.828]];

    size = len(trust);

    for i in range(size-1, -1, -1):
        if kSquare >= trust[i][1]:
            print('置信度 > {0}%, K^2 = {1}'.format(round((1-trust[i][0])*100, 3), kSquare));
            return trust[i][0];
</span>


技术分享

<span style="font-size:18px;">>>> 
X= [126.974, 96.933, 86.656, 63.438, 55.264, 50.976, 39.069, 36.156, 35.209, 32.416]
Y= [4.224, 3.835, 3.51, 3.758, 3.939, 1.809, 2.946, 0.359, 2.48, 2.413]
拟合结果: y =    0.02556 x +    1.33452 , r=   0.67615
SSG = 12.870180099999999, SSE = 6.986174058384116, SSR = 5.884006041615882
残差: [-0.356302026825019, 0.02262360033670774, -0.039669709794743824, 0.8018423973734929, 1.1917909702046225, -0.8285966637200308, 0.6127770403049579, -1.8997591485062628, 0.24544861984106747, 0.24984492078520049]
回归: [1.653002026825019, 0.885076399663292, 0.6223697097947434, 0.02885760262650683, -0.18009097020462272, -0.28970333627996947, -0.594077040304958, -0.6685408514937374, -0.6927486198410677, -0.7641449207852009]

def tmp():    
    X = [126.974, 96.933,86.656,63.438,55.264,50.976,39.069,36.156,35.209,32.416];
    Y = [4.224,3.835,3.510,3.758,3.939,1.809,2.946,0.359,2.480,2.413];
    a,b,r=linefit(X,Y)    
    print("X=",X)    
    print("Y=",Y)    
    print("拟合结果: y = %10.5f x + %10.5f , r=%10.5f" % (a,b,r));

    size = len(X);
    #平均值
    average = sum(Y)/size;
    SST = 0;
    #残差
    residual = [];
    SSE = 0;
    #回归
    regression = [];
    SSR = 0;

    for i in range(size):
        SST += (Y[i]-average)**2;
        value = a*X[i]+b;
        residual.append(Y[i]-value);
        SSE += (Y[i]-value)**2;
        regression.append(value-average);
        SSR += (value-average)**2;
        
    print('SSG = {0}, SSE = {1}, SSR = {2}'.format(SST, SSE, SSR));
    print('残差:', residual);
    print('回归:', regression);
	
</span>


本节到此结束,欲知后事如何,请看下回分解。

[从头学数学] 第197节 统计案例

标签:

原文地址:http://blog.csdn.net/mwsister/article/details/51251598

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!