ok, I have an excel sheet contaning three columns of data for 250 stocks .
time stock close
2005 stock A 200
2006 stock A 300
2005 stock B 100
2006 stock B 150
etc etc etc
1) The first question is how can I get the close organised in a column matrix
stock_price:= < <Stock A]> | <Stock B> | <Stock c > | <Stock i > >;
that have the structure
stock A Stock B Stock C
close t close t close t
close t+1 close t+1 close t+1
I would assume that we would need some sort of loop that checks if the name in the stock column is different and
if it is different starts a new column. I have not figured out how to do this.
2) My second question is: How can we calculate the returns P(t+1)-P(t) in an easy way
and also display that it in a column matrix
sorry the time column should
sorry the time column should be
time
2006
2006
2005
2005
etc
I know that I can load the
I know that I can load the data from excel into Maple in three column arrays (time, stock, close) by using
A:=ExcelTools:-Import("C:\\...................data.xls", "Sheet1", "A1:C20000");
but as I explained previously the data organized in this way feels very strange and it is very
hard to calculate correlation and covariance matricies under such conditions.
an alternative way would be
an alternative way would be if there would be a way that i could load 250 separete txt (acii) files into maple
and organize the output from each txt file as column arrays
these individual txt files have the general structure
stock i
close t
close t+1
close t+2
some possibilities for working with arrays of data
This could be a situation where Maple's flexible table indexing can be used effectively.
I'm not going to make too many comments about the importing of data from the Excel spreadsheet. Let's start with the data being represented as follows:
From here we can create a Maple Matrix with each stock in a column and each year in a row:
S := < < ``, seq( yy, yy=map2(op,2,{indices(STOCK)}) ) > | seq( < ss, seq( STOCK[ss,yy], yy=map2(op,2,{indices(STOCK)}) ) >, ss=map2(op,1,{indices(STOCK)}) ) >;
[ A B ]
[ 2005 200 100 ]
[ 2006 300 150 ]You also talk about computing the return. Here's one way that this can be done:
If you now add some more data to the system, the same commands can be used to update the matrix.
S := < < ``, seq( yy, yy=map2(op,2,{indices(STOCK)} ) ) > | seq( < ss, seq( STOCK[ss,yy], yy=map2(op,2,{indices(STOCK)}) ) >, ss=map2(op,1,{indices(STOCK)}) ) >; S := < S, < `return` | S[3,2..]-S[2,2..] > >;More rows can be appended in a similar manner. With a little work you can control how empty entries are displayed and processed.
I wish it was possible to easily include matrix output in MaplePrimes.
Doug
ok, thanx Doug. I will
ok, thanx Doug. I will spend some time try to understand what you have done.
As always I appreciate your input ! :-)
what a sweet trick! My
what a sweet trick! My hat is of for you.
That was really cool that the table updated itself when you add more stocks without having fiddle around with the parameters. Nice!
your solution was nice Doug
your solution was nice Doug but I think my problem remains because
I am not sure how I can define 250 stocks individually.
I have attached the maple file so you can see how it looks .
The first column holds the close price and the second column the stock number
again I want it to be
stock 1 stock (2) stock i
p (t) p(t) p(t)
p(t+1) p(t+1) p(t+1)
any help appreciated
View 8342_Import Data.mw on MapleNet or Download 8342_Import Data.mw
View file details
more info, please
I can't see your matrix because I do not have the xls file.
The matrix is 5000 x 2. Your description says each row is a close price and the stock number. How do you get the year from this? Is the first occurrence 2005, the second 2006, etc?
If you can show us a little more of your problem, I'm sure we can find a good solution.
Doug
ok, I will upload the excel
ok, I will upload the excel file. wait a second.....
Download
Download 8342_My-Data.zip
View file details
no, I just removed the time
no, I just removed the time column because everything becomes so complicated
You will see that all 250 stocks dont start at the same month. but you can include it if you find a
way around it. I thing the precis time of the series is not that important when we are going to calculate
the covariance and correlation matricies (as long as they are intact) all though we have to make sure we know which way is the most
resent observation for that stock otherwise we can buy a stock that is in a down trend which is not good
ok, I think I found
ok, I think I found something useful
V := Vector(10,i->3*i+11);
V1,V2 := V[1..5],V[6..-1];
which splits the vector into two separete verctors
Making Progress (I hope)
Hmm. A response that I wrote late last night did not appear. I'll be I just didn't hit Post comment. That's just as well, as this morning I came up with a much better response. The ideas are all the same as what I posted yesterday. There are some details that required some work. I think you will like what I have been able to do. Download the ZIP archive attached to this message.
Download 178_My-Data.zip
View file details
Extract the contents (2 files) and open the Maple worksheet. It might take about 30 seconds to execute the entire worksheet. If you do so, double-click on the final matrix (S5) to see what I have created.
When you go back and read the worksheet's details you will see that I have included some timings of different implementations of some of the steps.
I will post more snippets of code, but none of the matrix output is easily viewable through these posts. Bummer!
Here's the general approach:
Read in the data from an excel file
A:=ExcelTools:-Import("My-Data.xls", "FTSE-250");Create a price table and construct the matrix with each stock in a separate column. If there is no price for a specific month, that stock's entry will be a 0.
myS := (ss,yy,default) -> `if`(assigned(STOCK[ss,yy]),STOCK[ss,yy],default): S := < < ``, seq( yy, yy=map2(op,2,{indices(STOCK)}) ) > | seq( < ss, seq( myS(ss,yy,0), yy=map2(op,2,{indices(STOCK)}) ) >, ss=map2(op,1,{indices(STOCK)}) ) >;Add a row with the total return for each stock.
The details are all in the worksheet. You should download the ZIP archive as it also contains the Excel spreadsheet from which all of the data is obtained. If you keep the worksheet and spreadsheet in the same folder, you should not have to change anything in the worksheet.
I'm sure you could now export the matrix I have constructed back into an Excel spreadsheet. I have not tried this, yet.
One suggestion I hope the developers will hear is that it would be very empowering if it was possible to do work in the worksheet while the matrix browser was running. As it is, you have to close the matrix browser window to pass control back to the spreadsheet. This appears to be very similar to the limitation that control cannot be passed between Maplet windows, but I digress.
Comments, questions, and suggestions are greatly appreciated.
Doug
idea
[edited, after further reflection]
It is possible to construct an alternate Matrix Browser that is not implemented within a Maplet.
There are a variety of possibilities about how to do it. I hope to find time to post some prototypes to illustrate what (tricks) I have in mind.
How satisfactory these might be would be subjective -- each individual may put differing value of qualities of a Matrix Browser. Those could include such things as non-locking control, separate floating window occurence, dynamic interaction with data, different tab, and flexible ability to switch on-the-fly between any of the above. And so on.
It also raises these questions: is it better to get an alternative non-Maplet Matrix browser right now, or would it be better to implement a sophisticated "array data" analyzer? (The DataMatic, folks. It slices, it dices...) Or would a few simple context-menu driven pointplots suffice?
acer
thanx Doug! As i have said
thanx Doug! As i have said previously I am quite overwhelmed with your guys willingness to help
and do some serious work! I am quite sure this is a unique trade to the helpful people posting at the maple forum and
I am very greatful for it. I will have a look at your work and post if I have any questions. Again thanx for your time and effort.
wow! I am just amazed that
wow! I am just amazed that you managed to do all that work in such a short period and with such a precision and insight !
That work for me would have taken about 1 year, ha ha and then I am rounding down with about 10 years. Sweet !
Glad I could help
Only 10 years? It's taken me 20 years to get to this level. Of course, Maple could not have done this 20 years ago either. It's all a matter of evolution - of software and user knowledge. If you hang around the Maple community for a reasonable period of time you'll be surprised how much you will learn.
I'm glad you found my work useful. I'm just waiting for someone else to show me even better ways to get the same results. I am, in fact, hoping someone can suggest some improvements - that's how I'll maximize my knowledge gain from this exercise.
Doug
Doug if you are interested
Doug if you are interested check out my uploads at
http://www.maplesoft.com/applications/app_center_advanced_search.aspx?ABA=87231
you might pick up something there. But it is not related to programming but rather economic and finance.
There are more to come...have loads of stuff...:-)