using the same function but on different rows - matlab

I want to estimate the co(variance) matrix of two assets classes (with different derivatives) of every month.. So let's assume that each month is 25 days. I will get the following code,
covar=cov([elec(1:25,:) ngas(1:25,:)])
However, I have like 5 years of data so rewrite everything seems like a waste of time, and I think there has to be an easier way to fix this problem.
ps. I do think the answer to my question is already answered somewhere, but I do not know the words the search on. Thanks for your reply

Sounds like you just need a for loop?
counter = 0;
cover{floor(size(elec,1)/25)} = []; %//Pre-allocation
for day = 1:25:size(elec,1)
counter = counter + 1;
covar{counter}=cov([elec(day:day+25-1,:) ngas(day:day+25-1,:)])
end

Related

yahoo finance API stopped working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Anyone else having problems with this API? I have just recently started working with it but suddenly everything that was working both mine and others is no longer working, first the CSV interface and now the YQL interface. Surprised I don't see any mention of this on this board or a google search.
You can still back out the periods since the interval is in seconds (60*60*24), one day = 86,400.
The cookie is the problem. This is how I did it in VBA: https://stackoverflow.com/a/44055850/8027976
Example for period 1:
Dim baseDate As Date: baseDate = #1/1/1970#
Dim period1 As Long: period1 = (startDate - baseDate) * 86400
Dim period2 As Long: period2 = (endDate - baseDate + 0.8) * 86400
You need the ".8", otherwise, it will not pull the end-of-day price.
Yes, it's not working anymore. They changed the coding so now it reads something like
https://query1.finance.yahoo.com/v7/finance/download/IWM?period1=1492465014&period2=1495057014&interval=1d&events=history&crumb=oL864EniL6D
evidently they want to plant a cookie so it's not possible to automate it. Also there is no clear equivalence between the period1 and period2 numbers and the dates (in this case 5/17/2016 and 5/17/2017 respectively) so you couldn't even program the dates.
I am not sure why. I am grateful for the years of downloading data, and I would love to have this ability restored.
The periods seem to be the number of seconds between epoch and the start of the day in UTC. In JavaScript the way to compute the periods is as follows:
new Date(2017,4,18).valueOf()/1000 - new Date(2017,4,18).getTimezoneOffset()*60

Creating a function on matlab that adds numbers to the end of words

Said question
Dear Guys,
I have only just started using matlab and as such am not too sure how to answer this question for my problem set.
Would anyone be able to help me out with this?
Cheers
Because it's your lucky day, I'll post an answer which solves it in a non-obvious way, but from which you can learn the most.
Or, you know, get a good grade but fail the course.
Next time, please do an attempt yourself first; SO is not a coding service.
function string = uniqueword(string)
persistent calls
if isempty(calls)
calls = 0; end
maxcalls = 5;
assert(ischar(string),...
[mfilename ':invalid_argument'],...
'%s takes a single string argument.',...
mfilename);
assert(calls <= maxcalls,...
[mfilename ':maxcalls_exceeded'],...
'%d words have already been created.',...
maxcalls);
calls = calls + 1;
string = [string calls+48];
end

How can I calculate business/SLA hours with iterating over each second?

Before I spend a lot of time writing the only solution I can think of I was wondering if I'm doing it an inefficient way.
Once a support ticket is closed, a script is triggered, the script is passed an array of 'status-change-events' that happened from call open to close. So you might have 5 changes: new, open, active, stalled, resolved. Each one of these events has a timestamp associated with it.
What I need to do is calculate how much time the call was with us (new, open, active) and how much time it was with the customer (stalled). I also need to figure out how much of the 'us' time was within core hours 08:00 - 18:00 (and how many were non-core), and weekends/bank holidays count towards non-core hours.
My current idea is to for each status change, iterate over every second that occurred and check for core/non_core, and log it.
Here's some pseudo code:
time_since_last = ticket->creation_date
foreach events as event {
time_now = time_since_last
while (time_now < ticket->event_date) {
if ticket->status = stalled {
customer_fault_stalled++
} else {
work out if it was our fault or not
add to the appropriate counter etc
}
time_now++
}
}
Apologies if it's a little unclear, it's a fairly longwinded problem. Also I'm aware this may be slightly off of SO question guidelines, but I can't think of a better way of wording it and I need some advice before I spend days writing it this way.
I think you have the right idea, but recalculating the status of every ticket for every second of elapsed time will take a lot processing, and nothing will have changed for the vast majority of those one-second intervals
The way event simulations work, and the way I think you should write your application, is to create a list of all events where the status might change. So you will want to include all of the status change events for every ticket as well as the start and end of core time on all non-bank-holiday weekdays
That list of events is sorted by timestamp, after which you can just process each event as if your per-second counter has reached that time. The difference is that you no longer have to count through the many intervening seconds where nothing changes, and you should end up with a much more efficient application
I hope that's clear. You may find it easier to process each ticket separately, but the maximum gain will be achieved by processing all tickets simultaneously. You will still have a sorted sequence of events to process, but you will avoid having to reprocess the same core time start and end events over and over again
One more thing I noticed is that you can probably ignore any open status change events. I would guess that tickets either go from new to open and then active, or straight from new to resolved. So a switch between with your company and with the customer will never be made at an open event, and so they can be ignored. Please check this as I am only speaking from my intuition, and clearly know nothing about how your ticketing system has been designed
I would not iterate over the seconds. Depending on the cost of your calculations that could be quite costly. It would be better to calculate the borders between core/outside core.
use strict;
use warnings;
my $customer_time;
my $our_time_outside;
my $our_time_core;
foreach my $event ($ticket->events) {
my $current_ts = $event->start_ts;
while ($current_ts < $event->end_ts) {
if ($event->status eq 'stalled') {
$customer_time += $event->end_ts - $current_ts;
$current_ts = $event->end_ts;
}
elsif (is_core_hours($current_ts)) {
my $next_ts = min(end_of_core_hours($current_ts), $event->end_ts);
$our_time_core += $next_ts - $current_ts;
$current_ts = $next_ts;
}
else {
my $next_ts = min(start_of_core_hours($current_ts), $event->end_ts);
$our_time_outside += $next_ts - $current_ts;
$current_ts = $next_ts;
}
}
}
I can't see why you'd want to iterate over every second. That seems very wasteful.
Get a list of all of the events for a given ticket.
Add to the list any boundaries between core and non-core times.
Sort this list into chronological order.
For each consecutive pair of events in the list, subtract the later from the earlier to get a duration.
Add that duration to the appropriate bucket.
And the usual caveats for dealing with dates and times apply here:
Use a library (I recommend DateTime together with DateTime::Duration)
Convert all of your timestamps to UTC as soon as you get them. Only convert back to local time just before displaying them to the user.

How can I get statements within functions to execute in a randomly chosen order?

I'm relatively new to Matlab, and I've been trying to solve my problem for ages but I'm just continuously arriving at a dead end.
I have a code which should, in theory, play 3 sounds in a random order (each order being different for each trial). Upon each sound playing the participant will be asked which sound they heard and then given feedback. I have all the code complete and working up until the random order part. I have created code that on each trial will randomly order 1,2 and 3.
Order = [1, 2, 3];
PhonemeOrder = randperm (numel(Order));
I then have a function which plays the sound/asks the questions etc. within this I have attempted switch cases statements and if else statements depending on the number that PhonemeOrder produces but the order doesnt change even when phoneme order does. I believe my problem is however that PhonemeOrder comes out like [1,2,3] or [3,1,2] which is what i wanted. but Im not sure how to get my sounds to play in the order that it shows because I am using code like...
if/ PhonemeOrder = 1;
then do this...
elseif phonemeorder = 2;
then do this...
else
do this...
Or I've tried code like
switch cases
case 1
do this
case 2
do this
case 3
do this
I'm guessing this is where i am going wrong, but i just dont know how to change it and make it work! I hope this makes sense? I just need it to play in the order that phonemeorder specifies, with the order changing on each trial.
Any help will be greatly appreciated :D
bexG,
I think you are on the right track.
The only thing you need is to use a "for-loop" to go through the array of PhonemeOrder.
for i=1:length(PhonemeOrder)
switch PhonemeOrder(i)
case 1
play the first song
case 2
play the second song
case 3
play the thrid song
end
end
I hope this will help.
Please let me know if you have any further question.

Editing Timeline from CCB file in cocos

I did some research into this and couldn't really find anything, so if this is a repetitive question I apologize. but anyway I have made a CCB file in CocosBuilder and I would like to start the timeline, for example, at one second instead of playing from the beginning. Is there a way to do this? Thanks for the help guys.
Edit: i would like this to be done in the code.
I am using 2.2.1 Cocos2DX version. I think there is no option to play it from given interval. But you can tweak yourself to get it done. (Not simple one)
You have to go to CCBAnimationManager and there you get "mNodeSequences".
It is dictionary and you get difference properties there like "rotation position etc..."
values there.
Internally AnimationManager reads this value (These values are specified in your CCB)
and puts in runAction queue.
So you have to break it as you want.(Ex. 5 min timeline you have. But you want to start
from 1 min then you have run first 1 min Actions without delay and for remaining you
have properly calculate tween intervals.
It's long procedure and needs calculation. If you don't know any other simpler way try this. If you know pls let us know (Post it).