Ta-Lib for Python - version:0.4.7.
Issue: Talib.MACD - Histo plotting line instead of histogram
Python 3.3.2
Matplotlib 1.2.1, Numpy 1.7.1!
OS: Windows 7
I am trying to add MACD, using Ta-lib. The histogram is plotting as Line instead of histogram. Need your guidance to sort it out.
(I tried to attach the image, but unable to do so due to lack of reputations.)
I am very much a fresher to programming and to Python as well as to other packages I am using. Your guidance will help me improve further.
My code:
Calculation:
macd = macd, macdsignal, macdhist = talib.MACD(r.close, 12, 26, 9)
Plotting:
ax1 = fig1.add_subplot(211, frameon=True)
macd = ax1.plot(macd, color='green')
macd = ax1.plot(macdsignal, color='red')
macd = ax1.plot(macdhist, color='blue')
Thanks in advance
Regards
Suresh
I'm not sure what Talib or MACD are, but I think you just need to replace your ax1.plot() with ax1.hist(macd, bins=50). There are a bunch of options but provided macd is just the set of data you want to bin and put into a histogram this should work.
The matplotlib documentation has enough to get you going an example:
http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot
you will need to Ctrl-f to find matplotlib.pyplot.hist.
If instead your macd is already binned then you may need to use ax1.bar() instead:
http://matplotlib.org/examples/api/barchart_demo.html
Related
I am working on pedestrian step detection (acceleration). I want to calculate statistical features from my filtered signal. I have already calculated some and now I want to calculate gradient.
My data is of 1x37205 double. I calculated features using for loop with moving window size=2samples and 50% overlap of previous window. Below I am attaching the code I tried to calculate the gradient.
I am not sure if it is the right way to calculate or not? In addition, I am also unable to understand that what is the purpose to use gradient, how it can be useful for step detection and how to work with gradient? Could some one guide me or provide any code help in matlab?
%%Here M is mean and V is variance i already calculated from filtered data
G = zeros(length(window:length(M)), 2);
for i = window:length(M)
temp = gradient(M(i+1-window:i),V(i+1-window:i));
G(i, 1) = temp(2, 1); % "c1"
G(i, 2) = temp(2, 1); % "c2"
end
One of the best features of Matlab is its documentation. If you are unfamiliar on how to get specific function documentation, enter the following in the command line:
doc functionName
Alternatively, for 'brief' documentation that displays in the command line, you can enter:
help functionName
Also see the documentation link here.
Your question is worded poorly, so I will summarize what I understand and answer accordingly:
You have a step detection data (1*37205) double, let us call it stepSignal
stepSignal is position data
You want your window to be 2 steps with 50% overlap. This is the default behavior for the gradient function.
You do not need a "for" loop to achieve your goal. According to the documentation, "gradient" can take one input.
See the code below, and if need be add clarifications to the original question.
%% Assume that stepSignal is already imported into the workspace
velocity = gradient(stepSignal);
One last note, when you give "gradient" two inputs, it automatically assumes the second input is a uniform spacing value.
What is the MATLAB equivalent command of robust standard error in linear regression in Stata (e.g. reg y x, robust)?
I guess HAC might be the answer (http://www.mathworks.com/help/econ/hac.html).
Can anyone show a simple example of MATLAB that can generate the same result as generated with Stata code listed below.
webuse iris, clear
reg seplen sepwid
reg seplen sepwid, r
In MATLAB, I found robustfit (http://www.mathworks.com/help/stats/robustfit.html), but it should not be the equivalent command as it will affect the value of the estimated beta value, what's the relation between robustfit and robust standard error?
As stated by Nick Cox in a comment, you're not going to use robustfit. Instead you're going to estimate the robust standard errors separately like in the following little piece of code using hac. The coefficient estimates are found using the fitlm command.
% Load/define data
load fisheriris;
sepwid = meas(:,2);
seplen = meas(:,1);
% Estimates
fit = fitlm(sepwid,seplen);
[~,SE,coef] = hac(fit,'type','HC','weights','HC1','display','off');
% Output non-robust
fit.Coefficients(:,1:2)
% Output Robust
[coef SE]
Note that MATLAB places the constant/intercept in the top rather than in the bottom (like Stata). Alternatively you can use regstats2 by Oleg Komarov (*), which will give you p-values etc. as well.
% Estimates
fit2 = regstats2(seplen,sepwid,'linear','all');
% Output
[fit2.beta fit2.hc1.se]
(*) http://www.mathworks.com/matlabcentral/fileexchange/26169-regstats2
I want to draw a line on a graph to find the intersection point with another line. However, there's no response after I executed the script below. May I know what is the problem and how can I solve it?
x=1:2^20;
y2=2^24;
plot(x,y2);
Thanks!
What you want is to plot a line on 2^24. However, there are too many points for you computer probably, and you run out of memory
I am guessing that you'll need to plot your other inequality as well.
Something like
x=1:100:2^20;
% As Zoran and others suggested, You may not want all the points!
% It is too much memory
y2=2^24*ones(size(x)); % This ones is optional, but its good to know what you are doing (personal opinion)
plot(x,y2);
hold on
y1=(x+1).*log(x);
plot(x,y1);
However, you are still not there!
Another solution, which does not rely on plotting:
>> f = #(x) (x+1)*log(x)-2^24;
>> soln = fzero(f,1e6)
soln = 1.1987e+006
>> f(soln)
ans = 3.7253e-009
So your intersection point is at 1.1987e6.
Apparently,you have too many points for x, 2^20
Have to wait program to calculate, or plot,for example, every 100th point
This solution works for Matlab
x=1:100:2^20;
y2=2^2;
plot(x,y2,'o');
There is one more and maybe a bit smarter way: if you want to solve ((k+1)(ln k)<2^24) as you've commented above, use fsolve function to get just solution of an equation(!). Then use that solution to specify the area of your interest, so you won't have to plot the domain of 2^20.
(All functions are continuous so you don't have to worry about any wild singularities. Just examine the neigborhood of ks for which (k+1)(ln k)-2^24=0.)
When facing data in the time domain i seem to run into problems setting the exact limits on the x-axis with matlab.
Thus i plot my data and give the xlim in matlab time and set the limits:
minTT = datenum(2008,10,31,17,12,00);
maxTT = datenum(2008,10,31,17,19,00);
xlim = ([minTT maxTT]);
Then i use the date tick option to convert the matlab timing to "real clock time".
datetick('x',13,'keepticks');
The 'keep ticks' option is still better than none and i tried with both.
But what i get out is a plot that is going from: 17:12:28 to 17:17:58.
I have tried editing the ticks on my own as suggested from another post at StackOverflow like this:
ticks = get(gca, 'xtick')
newTicks = linspace(ticks(1), ticks(end), 8);
set(gca,'Xtick', newTicks)
but even that doesn't work out and gives me limits from 17:12:28 to 17:17:31.
Is there any way to really force matlab to use specific times on the axis whether or not the data doesn't exactly start there?
You can use the 'keeplimits' flag to datetick() - it does just what it says, much like 'keepticks'
I have a question of an annoying fact. I'm using libsvm with matlab and I'am able to predict using:
predicted_label = svmpredict(Ylabel, Xlabel, model);
but it happen that every time I make a predictions appears this:
Accuracy = X% (y/n) (classification)
Which I find annoying because I am repeating this procedure a lot of times and also makes it slow because its displaying in screen.
I think what I want is to avoid that svmpredict being verbose.
Can anyone help me with this? Thanks in advance.
-Jessica
I found a much better approach than editing the source code of the c library was to use matlabs evalc which places any output to the first output argument.
[~ predicted_label] = evalc('svmpredict(Ylabel, Xlabel, model)');
Because the string to be evaluated is fixed should be no performance decrease.
svmpredict(Ylabel, Xlabel, model, '-q');
From the manual:
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
[predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
Parameters:
model: SVM model structure from svmtrain.
libsvm_options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
-q : quiet mode (no outputs)
If you are using matlab, just find the line of code that is displaying this information (usually using 'disp', 'sprintf', or 'fprintf') and comment it out using the commenting operator %.
example:
disp(['Accuracy= ' num2str(x)]);
change it to:
% disp(['Accuracy= ' num2str(x)]);
If you are using the main libsvm library then you need to modify it before making.
1- Open the file 'svmpredict.c'
2- find this line of code:
info("Accuracy = %g%% (%d/%d) (classification)\n",
(double)correct/total*100,correct,total);
3- just comment it out using // operator
4- save and close the file
5- make the project