Calculate Beta or Slope !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate Beta or Slope !
# 1  
Old 09-28-2012
Question Calculate Beta or Slope !

Hello Group,

I would request your help to build a shell script that can calculate the beta or slope between a two series of investment instruments (i.e S&P 500 and Silver).
I have a source files of data (First - Date, Second (Value1) and Third (Value 2) columns)
Code:
Date    S&P 500    S&P 500    Silver    Silver
9/4/2012    140.28        31.36    
9/5/2012    140.16    -0.085616438    31.27    -0.287815798
9/6/2012    143.01    1.992867632    31.67    1.263024945
9/7/2012    143.56    0.383115074    32.64    2.971813725
9/10/2012    142.75    -0.567425569    32.29    -1.083926912
9/11/2012    143.15    0.279427174    32.41    0.370256094
9/12/2012    143.62    0.327252472    32.21    -0.620925179
9/13/2012    145.81    1.501954598    33.61    4.165426956
9/14/2012    146.46    0.443807183    33.6    -0.029761905
9/17/2012    145.96    -0.342559605    32.99    -1.849045165
9/18/2012    145.84    -0.082281953    33.71    2.135864729
9/19/2012    145.92    0.054824561    33.57    -0.417039023
9/20/2012    145.93    0.006852601    33.58    0.029779631
9/21/2012    145.87    -0.041132515    33.48    -0.298685783
9/24/2012    145.65    -0.151047031    32.93    -1.670209535
9/25/2012    144.1    -1.075641915    32.68    -0.76499388
9/26/2012    143.29    -0.56528718    32.87    0.578034682
9/27/2012    144.64    0.93335177    33.58    2.114353782
9/28/2012    143.97    -0.465374731    33.48    -0.298685783
                
        Slope or Beta        0.293057926


Attached you will find the spreadsheet of this example

Thanks in advance

Last edited by csierra; 09-29-2012 at 12:01 AM..
# 2  
Old 09-29-2012
Code:
cat testfile
04/09/2012      140.28          31.36
05/09/2012      140.16          31.27
06/09/2012      143.01          31.67
07/09/2012      143.56          32.64
10/09/2012      142.75          32.29
11/09/2012      143.15          32.41
12/09/2012      143.62          32.21
13/09/2012      145.81          33.61
14/09/2012      146.46           33.6
17/09/2012      145.96          32.99
18/09/2012      145.84          33.71
19/09/2012      145.92          33.57
20/09/2012      145.93          33.58
21/09/2012      145.87          33.48
24/09/2012      145.65          32.93
25/09/2012      144.1           32.68
26/09/2012      143.29          32.87
27/09/2012      144.64          33.58
28/09/2012      143.97          33.48

awk 'NR!=1{perc1[++i]=(($2-prev1)/$2)*100
perc2[i]=(($3-prev2)/$3)*100
sum1+=perc1[i]
sum2+=perc2[i]
max=i}
{prev1=$2;prev2=$3}
END{
avg1=sum1/max
avg2=sum2/max
for(i=1;i<=max;i++)
{
 numsum+=((perc1[i]-avg1)*(perc2[i]-avg2))
 densum+=((perc2[i]-avg2)**2)
}
print "Slope : " numsum/densum
}' testfile
Slope : 0.293058

# 3  
Old 10-01-2012
Hi elixir sinari,

Thanks a lot for your response.
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies
Login or Register to Ask a Question