How to plot Total Return? - reactjs

I'm looking at various libraries to see how I can plot the total return of a stock within the time period specified. The algorithm is simply (data - dataMin / dataMax ) * 100 for the % return across the time period. I'm currently using recharts but it seems like there's a limitation in their library as I cannot alter the Y Axis to plot that number AND the price at the time the mouse is hovering over.
Thanks!
Edit:
I have something like this at the moment but as you can see, the Y Axis scaling is capped at ~1000% whereas the max should be ~2800%. https://codesandbox.io/s/recharts-issue-template-czf5t

Related

Plotting a frequency response from biquad filter

This is a hard one and although I can think of a few kludge methods of doing it, I have a feeling there is a clean mathematical method, although I am having difficulty inventing it myself.
I have a number of parameters which control (software) biquad filters for audio. Essentially there are just 3 parameters, frequency, gain and Q (or bandwidth). In audio terms, the frequency represents the center frequency of the filter. The gain represents whether this frequency is boosted or cut (a gain of 0 results in no change to the audio passing through the filter). Q represents the width of the filter - IE a very wide filter might affect frequencies far away from the center frequency, whereas a narrow (low Q) filter will only affect frequencies close to the center frequency.The filters take the form of a bell curve, or at least thats an approximation, whether its mathematically accurate I am not sure.
I want to display the characteristics of these filters graphically - display a graph of gain against frequency. There are several of these filters applied to the audio channel, and I want to be able to add the different result graphs, to produce an overall graph (IE a graph summing all the components of the combined filters). But I also want to be able to access the individual filters graphs.
I can handle adding the component graphs into a single 'total' graph, but how to produce the original x-y graph from the filter parameters escapes me. I will draw bitmaps so all I need is to be able to create arrays of the form frequency[x]=y. Im doing this in C so I don't have the mathematical tools in matlab etc. So I might have a filter with a center frequency of say 1000 (Hz), a gain of say 20 (db or linear I understand how to convert that), and a Q of say 3. The Q factor is relative and does not have to be exactly mathematically correct if that causes any complication.
It seems like a quite simple mathematical function but maths is not my strong point and I don't know enough - I have been messing round with sine functions etc but its not working and I suspect is probably wasting processing power by over complicating the maths (although I might be wrong there).
TIA, Pete
I have my doubts about the relationships between biquad filters, Q values, and bell curves. But I'll put those aside and just tell you how to draw a bell curve, since that's what you asked.
From this wikipedia article, the equation for a bell curve is
where for your application
a corresponds to the gain
b determines the center frequency
2c^2 is related to Q (larger values will make the curve wider)
The C code below computes a sample bell curve. For this example, the numbers were chosen based on drawing into a window that is 250 pixels wide by 200 pixels high, with a coordinate system where the origin {0,0} is at the bottom left corner.
int width = 250;
int height = 200;
int bellCurve[width]; // the output array that holds the f(x) values
double gain = 180; // the 'a' value, determines how high the peak is from the baseline
double offset = 10; // the 'd' value, determines the y coordinate of the baseline
double qFactor = 1000; // the '2c^2' value, determines how fat the curve is
double center = 100; // the 'b' value, determines the x coordinate of the peak
double dx;
for ( int x = 0; x < width; x++ )
{
dx = x - center;
bellCurve[x] = gain * exp( -( dx * dx ) / qFactor ) + offset;
}
Plotting the curve results in an image like this where the peak is at x=100, y=10+180=190
You could input a unit impulse (an array of all zeros, except one element=1.0) into your digital filters, treating them as black boxes. Then FFT the impulse response output array to get the frequency response of the filter. Plotting the magnitude of the complex frequency samples will give you a pretty picture. Python+numpy+matplotlib would probably be an easier way to go about it. You will need to know the sampling period to get meaningful plots.
What you really want is the bode plot of the filter. This is non trivial to calculate yourself, a cursory search for a library to do it for you in C yielded nothing. If accuracy is not important and you can approximate the shape once and stretch it based on the parameters of the particular filter. For example, you might have a normalized array of relative values and construct a new curve (array) based on the parameters of the filter and the base curve you generated earlier.
The base curve could be generated from MATLAB if you can or Wolfram Alpha or something like that.
Here is one in javascript.
http://www.earlevel.com/main/2013/10/13/biquad-calculator-v2/
The filter you describe is the 'peak' filter. Use the log scale to display frequencies.
—Tom

Lock scale on multiple axis ssrs

I have a chart in SSRS (reporting services) that is using both the primary and secondary y axis to plot 4 different years of data. I am also using a series group for multiple values stacked. The problem that I am having is that the 2 axis's are not using the same scale so the numbers look like they don't match the table that is also on this report. How can I lock the 2 axis's so they use the same scale. I would like to still use auto axis if possible so I don't have to calculate the max and min myself.
Right click an axis and select "Axis Properties". In the Axis Options tab you can set the minimum and maximum values of the axis, you want these values to be set to a value (Not Auto) and for both graphs to hold the same values. This is to say for example that a minimum = 0 and maximum = 10 that the graph will start at 0 and end at 10 , if you have a value of 11 it will just hit the top of the graph. You will also want to set the interval. An interval of 1 would make the axis read 0,1,2,3 etc, an interval of 2 would make it go 0,2,4,6 etc and again you want the same value set on both graphs.

Gnuplot fit function - fitting to q-exponential

I have a run an experiment using my C program. I have been using GNU plot to plot histograms/graphs to analyse the data.
The code below takes the data in my file and creates a file called 'tableavalanchesizeGSA' that contains the information that it would have used to plot a histogram for my data - i.e. my data in this table form is binned and the frequency of each bin. Then I take the log of the frequency and plot it against the binned data. (Simply put its just the log of the frequency vs the binned original data).
#Gnuplot commands for avalanche size GSA Log plot (axes as Log of freq/totaltrials):
reset
set xlabel 'Avalanche size'
set ylabel 'Log of Frequency'
set title "Avalanche Size with GSA"
set table 'tableavalanchesizeGSA'
#bw is the binwidth for the histogram
bw = 50.0
bin(x,s)=s*int(x/s)
plot 'avalanche_size_GSA_n_trials_2048000.dat' using (bin($1,bw)+bw/2.0):(1.0/2048000) smooth frequency with points
unset table
set logscale y
plot 'tableavalanchesizeGSA' with points title 'Frequency of Avalanche size with 2048000 trials using 1.0/2048000'
Now I am trying to fit my data to the following function:
Q(x)=(1+(1-q)*((s+x)/m))**(1/(1-q))
where q, s and m are my parameters. I have played around with by plotting my log plot and this function on the same plot for a little bit and know that q = 1.16, m = s = 100 are good values/that somewhat fit the data but not exactly. So I add the following to my code:
q = 1.16
s = 100
m = 100
Q(x)=(1+(1-q)*((s+x)/m))**(1/(1-q))
fit Q(x) 'tableavalanchesizeGSA' via s, m, q
to try and fit the data to the function using 'close' parameter values. But once the iterations are done it gives me q = 1.16116 and s = m = 100 still, which doesn't really give anything different to what I had before with q = 1.6.
Is there something wrong here? Why does the fit function not find a closer fit?
The following image shows the function (green) fitted to my data. But I would still like a more accurate fit.
I guess by "closer fit" you mean that the values for lower avalanche sizes (where you have higher frequencies) should be closer to the fitted function. You may want to add weights to your fit, as the values for higher frequencies should have a higher fidelity. Consider the code
q = 1.16
s = 100
m = 100
Q(x)=(1+(1-q)*((s+x)/m))**(1/(1-q))
fit Q(x) 'tableavalanchesizeGSA' using 1:2:(1/sqrt($2)) via s, m, q
Here, the third column of the using statement is considered as the standard deviation of the value in the second column (see gnuplot documentation). This should give you a better fit, but you may need to use the correct standard deviations.
See also the gnuplot fit demos for examples with weighting.

different Tick Unit (interval) in Number Axis based on density of data points in jFreeChart

I want to show the result of my simulation with XYLineChart of JfreeChart, the range of Number Axis is between time =0 to time =3000, but the density of data points are different in different time intervals,
for example there is just few data points until time 1000, then the density in time interval (1000 - 2000 ) is too high, and again i have few data points in time interval (2000 -3000)
so, i want to have different tick unit automatically based on the density of the data points,
the only code i use is:
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setAutoRange(true);
xAxis.setAutoTickUnitSelection(true);
it must be done automatically, because, the data points are generated during my simulation, and i don't know in which interval the density is high or low

Increasing the pitch of audio using a varied value

Okay, this a bit of maths and DSP question.
Let us say I have 20,000 samples which I want to resample at a different pitch. Twice the normal rate for example. Using an Interpolate cubic method found here I would set my new array index values by multiplying the i variable in an iteration by the new pitch (in this case 2.0). This would also set my new array of samples to total 10,000. As the interpolation is going double the speed it only needs half the amount of time to finish.
But what if I want my pitch to vary throughout the recording? Basically I would like it to slowly increase from a normal rate to 8 times faster (at the 10,000 sample mark) and then back to 1.0. It would be an arc. My questions are this:
How do I calculate how many samples would the final audio track be?
How to create an array of pitch values that would represent this increase from 1.0 to 8.0 back to 1.0
Mind you this is not for live audio output, but for transforming recorded sound. I mainly work in C, but I don't know if that is relevant.
I know this probably is complicated, so please feel free to ask for clarifications.
To represent an increase from 1.0 to 8.0 and back, you could use a function of this form:
f(x) = 1 + 7/2*(1 - cos(2*pi*x/y))
Where y is the number of samples in the resulting track.
It will start at 1 for x=0, increase to 8 for x=y/2, then decrease back to 1 for x=y.
Here's what it looks like for y=10:
Now we need to find the value of y depending on z, the original number of samples (20,000 in this case but let's be general). For this we solve integral 1+7/2 (1-cos(2 pi x/y)) dx from 0 to y = z. The solution is y = 2*z/9 = z/4.5, nice and simple :)
Therefore, for an input with 20,000 samples, you'll get 4,444 samples in the output.
Finally, instead of multiplying the output index by the pitch value, you can access the original samples like this: output[i] = input[g(i)], where g is the integral of the above function f:
g(x) = (9*x)/2-(7*y*sin((2*pi*x)/y))/(4*pi)
For y=4444, it looks like this:
In order not to end up with aliasing in the result, you will also need to low pass filter before or during interpolation using either a filter with a variable transition frequency lower than half the local sample rate, or with a fixed cutoff frequency more than 16X lower than the current sample rate (for an 8X peak pitch increase). This will require a more sophisticated interpolator than a cubic spline. For best results, you might want to try a variable width windowed sinc kernel interpolator.

Resources