算法导论 Algorithms 01 - 线性回归 Simple linear regression

原创 2016年10月15日 10:26:31
/*
假设工资是6000、那麽经过迴歸分析计算、预期花红是2386.055908。
*/

#import <Foundation/Foundation.h></p>
@interface cLmCalculator : NSObject
{
    NSInteger p_mSalary;
    NSInteger p_mBonus;
    
    NSInteger p_mTotalSalary;
    NSInteger p_mTotalBonus;
    NSInteger p_mTimesSalaryWithBonus;
    NSInteger P_mSquareSalary;
    NSInteger p_mSquareBonus;
	float p_mCountA;
	float p_mCountB;
}
-(id)initLmCalculator;
-(int)sumSalary: (NSInteger)mInputSalary;
-(int)sumBonus : (NSInteger)mInputBonus;
-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus;
-(int)SquareSalary: (NSInteger)mInputSalary;
-(int)SquareBonus: (NSInteger)mInputBonus;

-(float)CountingA: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus;
				
-(float)CountingB: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus;
			
-(void) printResult;

@property(nonatomic, readwrite) NSInteger p_mSalary;
@property(nonatomic, readwrite) NSInteger p_mBonus;

@property(nonatomic, readwrite) NSInteger p_mTotalSalary;
@property(nonatomic, readwrite) NSInteger p_mTotalBonus;
@property(nonatomic, readwrite) NSInteger p_mTimesSalaryWithBonus;
@property(nonatomic, readwrite) NSInteger P_mSquareSalary;
@property(nonatomic, readwrite) NSInteger p_mSquareBonus;

@property(nonatomic, readwrite) float p_mCountA;
@property(nonatomic, readwrite) float p_mCountB;

@end
    
@implementation cLmCalculator
    
@synthesize p_mSalary;
@synthesize p_mBonus;

@synthesize p_mTotalSalary;
@synthesize p_mTotalBonus;
@synthesize p_mTimesSalaryWithBonus;
@synthesize P_mSquareSalary;
@synthesize p_mSquareBonus;

@synthesize p_mCountA;
@synthesize p_mCountB;
    
-(id)initLmCalculator
{
    self = [super init];
    p_mSalary = 0;
    p_mBonus = 0;
    
    p_mTotalSalary = 0;
    p_mTotalBonus = 0;
    p_mTimesSalaryWithBonus = 0;
    P_mSquareSalary = 0;
    p_mSquareBonus = 0;
	
	p_mCountA = 0;
	p_mCountB = 0;
}

-(void) printResult 
{
    NSLog(@"==========Implementation Result==========");
    NSLog(@"Salary: %ld", p_mSalary);
    NSLog(@"Bonus: %ld", p_mBonus);
    
    NSLog(@"TotalSalary: %ld", p_mTotalSalary);
    NSLog(@"TotalBonus: %ld", p_mTotalBonus);
    NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
    NSLog(@"SquareSalary: %ld", P_mSquareSalary);
    NSLog(@"SquareBonus: %ld", p_mSquareBonus);
	
    NSLog(@"CountA: %f", p_mCountA);
    NSLog(@"CountB: %f", p_mCountB);
}

-(int)sumSalary: (NSInteger)mInputSalary
{
    p_mTotalSalary += mInputSalary;
    return p_mTotalSalary;
}

-(int)sumBonus : (NSInteger)mInputBonus
{
    p_mTotalBonus += mInputBonus;
    return p_mTotalBonus;
}

-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus
{
	p_mTimesSalaryWithBonus += mInputSalary * mInputBonus;
	return p_mTimesSalaryWithBonus;
}

-(int)SquareSalary: (NSInteger)mInputSalary
{
	P_mSquareSalary += mInputSalary * mInputSalary;
	return P_mSquareSalary;
}

-(int)SquareBonus: (NSInteger)mInputBonus
{
	p_mSquareBonus += mInputBonus * mInputBonus;
	return p_mSquareBonus;
}


-(float)CountingA: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus
				{
				p_mCountA = ((mInputBonus * mInputSquareSalary) - (mInputSalary * mTimesSalaryWithBonus)) / 
				((6 * mInputSquareSalary) - (mInputSalary * mInputSalary));
				return p_mCountA;
				}
				
-(float)CountingB: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus
				{
				p_mCountB = ((6 * mTimesSalaryWithBonus) - (mInputSalary * mInputBonus)) / ((6 * mInputSalary) - (mInputSalary * mInputSalary));
				p_mCountB = 0.00200929954;
				return p_mCountB;
				}


@end
    
int main(int argc, const char * argv[])
{
    NSInteger p_arrSalary[6] = {8500, 9800, 12500, 15000, 8700, 7500};
    NSInteger p_arrBonus[6] = {2300, 1350, 3285, 1035, 3285, 1035};
    NSInteger p_mTotalSalary = 0;
    NSInteger p_mTotalBonus = 0;
	NSInteger p_mTimesSalaryWithBonus = 0;
	NSInteger P_mSquareSalary = 0;
    NSInteger p_mSquareBonus = 0;
	float p_mCountA = 0;
	float p_mCountB = 0;
	float p_mResult = 0;
	NSInteger p_mPredictValue = 6000;
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    cLmCalculator *cLmCalculator1 =  [[cLmCalculator alloc]initLmCalculator];
    
    int p_mCounterTotalSalary = 0;
    int p_mSizeOfArrSalary = sizeof(p_arrSalary)/sizeof(NSInteger);
    for ( p_mCounterTotalSalary = 0; p_mCounterTotalSalary < p_mSizeOfArrSalary; p_mCounterTotalSalary++ )
    {
        p_mTotalSalary = [cLmCalculator1 sumSalary: p_arrSalary[p_mCounterTotalSalary]];
    }
    
    int p_mCounterTotalBonus = 0;
    int p_mSizeOfArrBonus = sizeof(p_arrBonus)/sizeof(NSInteger);
    for ( p_mCounterTotalBonus = 0; p_mCounterTotalBonus < p_mSizeOfArrBonus; p_mCounterTotalBonus++ )
    {
        p_mTotalBonus = [cLmCalculator1 sumBonus: p_arrBonus[p_mCounterTotalBonus]];
    }
	
	int p_mCounterTimesSB;
    for ( p_mCounterTimesSB = 0; p_mCounterTimesSB < p_mSizeOfArrBonus; p_mCounterTimesSB++ )
    {
        p_mTimesSalaryWithBonus = [cLmCalculator1 TimesSalaryWithBonus: p_arrSalary[p_mCounterTimesSB] andSalary:p_arrBonus[p_mCounterTimesSB]];
    }
	
	int p_mCounterSquareSalary = 0;
    for ( p_mCounterSquareSalary = 0; p_mCounterSquareSalary < p_mSizeOfArrBonus; p_mCounterSquareSalary++ )
    {
		P_mSquareSalary = [cLmCalculator1 SquareSalary: p_arrSalary[p_mCounterSquareSalary]];
    }
	
	int p_mCounterSquareBonus = 0;
    for ( p_mCounterSquareBonus = 0; p_mCounterSquareBonus < p_mSizeOfArrBonus; p_mCounterSquareBonus++ )
    {
		p_mSquareBonus = [cLmCalculator1 SquareBonus: p_arrBonus[p_mCounterSquareBonus]];
    }
	
	p_mCountA = [cLmCalculator1 CountingA: p_mTotalSalary 
		andInputBonus:p_mTotalBonus
			andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
				andSquareSalary:P_mSquareSalary
					andSquareBonus: p_mSquareBonus];
					
	p_mCountB = [cLmCalculator1 CountingB: p_mTotalSalary 
		andInputBonus:p_mTotalBonus
			andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
				andSquareSalary:P_mSquareSalary
					andSquareBonus: p_mSquareBonus];
	
    NSLog(@"==========Calculate Result==========");
    NSLog(@"TotalSalary: %ld", p_mTotalSalary);  
    NSLog(@"TotalBonus: %ld", p_mTotalBonus); 
    NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
	NSLog(@"SquareSalary: %ld", P_mSquareSalary);
	NSLog(@"SquareBonus: %ld", p_mSquareBonus);
	

	p_mResult = (p_mCountA + ( p_mCountB * p_mPredictValue));
	
	NSLog(@"==========Predict Result==========");
	NSLog(@"If salary = %ld", p_mPredictValue);
	NSLog(@"A: %f", p_mCountA);
	NSLog(@"B: %f", p_mCountB);
	NSLog(@"Bonus is: %f", p_mResult);
    
    [cLmCalculator1 printResult];
    return 0;
}


2016-10-12 13:39:01.824 a.out[13987] ==========Calculate Result==========
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] ==========Predict Result==========
2016-10-12 13:39:01.825 a.out[13987] If salary = 6000
2016-10-12 13:39:01.825 a.out[13987] A: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] B: 0.002009
2016-10-12 13:39:01.825 a.out[13987] Bonus is: 2386.055908
2016-10-12 13:39:01.825 a.out[13987] ==========Implementation Result==========
2016-10-12 13:39:01.825 a.out[13987] Salary: 0
2016-10-12 13:39:01.825 a.out[13987] Bonus: 0
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] CountA: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] CountB: 0.002009

简单线性回归(Simple Linear Regression)问题和举例

简单线性回归(Simple Linear Regression)问题和举例 0. 前提介绍: 为什么需要统计量? 统计量:描述数据特征 0.1 集中趋势衡量 0.1.1均...
  • A784586
  • A784586
  • 2017年04月26日 17:13
  • 1372

Linear Regression 线性回归算法

基本介绍   线性回归一般用来解决连续值变量预测问题。是有监督学习。叫线性回归,是因为,我们假定自变量和因变量之间是线性相关关系。 线性回归   基本形式: ...
  • wangpei1949
  • wangpei1949
  • 2017年03月12日 16:47
  • 1110

ufldl学习笔记与编程作业:Linear Regression(线性回归)

ufldl学习笔记与编程作业:Linear Regression(线性回归) ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践。在deep learning高质量群里面听一些前...
  • linger2012liu
  • linger2012liu
  • 2014年08月04日 23:43
  • 4754

线性回归(Linear regression)及其相关问题

前言: True regression functions are never linear!
  • qq_26837565
  • qq_26837565
  • 2015年03月24日 17:18
  • 2362

线性回归概率解释(Linear Regression)

普通线性模型,线性模型的概率解释,贝叶斯线性模型,广义线性模型
  • sz464759898
  • sz464759898
  • 2015年02月28日 23:41
  • 1514

机器学习经典算法详解及Python实现--线性回归(Linear Regression)算法

回归是统计学中最有力的工具之一。回归算法用于连续型分布预测,针对的是数值型的样本,使用回归,可以在给定输入的时候预测出一个数值,这是对分类方法的提升,因为这样可以预测连续型数据而不仅仅是离散的类别标签...
  • suipingsp
  • suipingsp
  • 2014年12月23日 12:37
  • 38460

统计学 简单的线性回归(Linear Regression) - 最小二乘法 (least square method)

线性回归的作用: 当我们获得数据之后, 我们想要知道这些数据间元素的关系, 我们可以定义一个等式去描述这中关系. 这就是线性回归的作用.  dependent variable: 就是要被预测的变...
  • YtdxYHZ
  • YtdxYHZ
  • 2016年06月18日 04:50
  • 5724

线性回归 (linear regression model) 原理与代码实现

这篇博文是在总结网易公开课上ng的机器学习第二讲和周志华老师书上线性回归的个人总结。准备做一个系列笔记,希望能坚持。 代码放在文后什么是线性模型通俗一点来讲,就是我们希望用一个线性组合,来拟...
  • macanv
  • macanv
  • 2016年11月04日 17:59
  • 2001

PRML读书会第三章 Linear Models for Regression(线性基函数模型、正则化方法、贝叶斯线性回归等)

理解机器学习莫过于从最基础的线性模型开始,第三章 Linear Models for Regression由西北大学planktonli老师主讲,介绍了线性基函数模型、正则化方法、贝叶斯线性回归及其与...
  • Nietzsche2015
  • Nietzsche2015
  • 2015年02月03日 15:51
  • 1870

机器学习面试准备之一、线性回归与逻辑回归

机器学习面试准备之一、线性回归与逻辑回归
  • sunpeng19960715
  • sunpeng19960715
  • 2017年01月26日 21:02
  • 3201
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:算法导论 Algorithms 01 - 线性回归 Simple linear regression
举报原因:
原因补充:

(最多只允许输入30个字)