My program are divided into 4 files.My object is to calculate betas of a series of stocks.
The stats program,which contains 6 calculation functions:
Code:
#include <math.h>
static double sum(double *p,int length){
double sum=0;
int i=0;
for(;i<length;i++)
sum=sum+*(p++);
return sum;
}
static double avg(double *p,int length){
double sum=0;
int i=0;
double avg;
for(;i<length;i++)
sum=sum+*(p++);
avg=sum/length;
return avg;
}
static double sumSq(double *p,int length){
double sumSq=0;
int i=0;
for(;i<length;i++)
sumSq=sumSq+pow(*(p++),2);
return sumSq;
}
static double sumxy(double *p1,double *p2,int length){
double sumxy=0;
int i=0;
for(;i<length;i++)
sumxy=sumxy+*(p1++)**(p2++);
return sumxy;
}
void regression(double *x,double *y,int length,double *beta,double *intercept,double *RSquare){
*beta=(length*sumxy(x,y,length)-sum(x,length)*sum(y,length))/(length*sumSq(x,length)-sum(x,length)*sum(x,length));
*intercept=avg(y,length)-*beta*avg(x,length);
double *x1,*y1; //Build two arrays:x-avg(x),y-avg(y) to calculate R
x1=(double *)malloc(length*sizeof(double));
y1=(double *)malloc(length*sizeof(double));
int i=0;
for(;i<length;i++){
x1[i]=x[i]-avg(x,length);
y1[i]=y[i]-avg(y,length);
}
*RSquare=sumxy(x1,y1,length)*sumxy(x1,y1,length)/(sumSq(x1,length)*sumSq(y1,length));
}
Next is a file defines a function to get prices data of stocks:
Code:
#include <stdio.h>
#include "stock.h"
#include <string.h>
#include <stdlib.h>
stock *getData(int *size){ //Size refers to the number of stocks.
stock *stockarray=(stock*)malloc(*size*sizeof(stock));
int i=0;
int j=0;
for(;i<*size;i++){
scanf("%s %d",stockarray[i].name,&stockarray[i].length); //Read the name and the number of closing prices.
stockarray[i].price=(double *)malloc(stockarray[i].length*sizeof(double));
stockarray[i].retu=(double *)malloc(stockarray[i].length*sizeof(double));
for(;j<stockarray[i].length;j++){
scanf("%f",&stockarray[i].price[j]);
printf("price%f",stockarray[i].price[j]);
}
}
return stockarray;
}
And the following is the main function and two other functions:
Code:
#include <stdio.h>
#include "stock.h"
#include <stdlib.h>
void setReturn(stock stock);
void printStock(stock mystock,int printPrice,int printReturns);
stock *getData(int *size);
void regression(double *x,double *y,int length,double *slope,double *intercept,double *RSquare);
int main(){
int *stockno; //Define a pointer pointing to an interger denoting the number of stocks.
int size;
scanf("%d",&size);
stockno=&size;
printf("%d",*stockno);
stock* stockarray;
stockarray=getData(stockno);
int i=0;
for(;i<*stockno;i++){
setReturn(stockarray[i]);
} //Set return array in every structure
int j=0;
for(;j<*stockno-1;j++){
regression(stockarray[i].retu,stockarray[*stockno-1].retu,stockarray[i].length,&stockarray[i].beta,&stockarray[i].intercept,&stockarray[i].RSquare);
printStock(stockarray[i],0,0);
}
return 0;
}
void setReturn(stock stock){ //The argument is a pointer to a structure.
int i=0;
for(;i<stock.length-1;i++){
*stock.retu=(*(stock.price+1)-(*stock.price))/(*stock.price);
printf("return1=%f",stock.price[i]);
stock.retu++;
stock.price++;
}
}
void printStock(stock mystock,int printPrice,int printReturns){
int i=0;
printf("The Stock Name\t\tBeta\t\tR-Squared\t\tIntercept\n");
printf("%s%\t\t%f\t\t%f\t\t%f\n",mystock.name,mystock.beta,mystock.RSquare,mystock.intercept);
if (printPrice==1&mystock.price[0]!='\0'){
printf("The stock price series:\n");
for(;i<mystock.length;i++)
printf("%f\n",mystock.price[i]);
}
if (printReturns==1&mystock.retu[0]!='\0'){
printf("The return series:\n");
for(;i<mystock.length-1;i++)
printf("%f%%\n",100*mystock.retu[i]);
}
}
Finally the header file:
Code:
#ifndef STOCK_H_
#define STOCK_H_
#endif /* STOCK_H_ */
typedef struct stock{
char *name;
int length;
double beta;
double RSquare;
double intercept;
double *price; //define the array of closing price
double *retu; //define the array of return
} stock;
I have been working on debuging for a whole day but never figured out what is wrong with my program.Hope somebody could help me.Thank you very much!