Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been using the simple_statistics gem, but im looking to calculate the EMA on the last x records. For example (when calculating WMA)
#stockticker.ema10 = s.stocktickers.last(10).map(&:current_price).map{|f| f.to_f}.wma
I was wondering if anyone can provide advise on how I got about calculating the EMA in rails?
It looks like the Moving Averages gem might be what you're looking for. Here is a copy of their exponential_moving_average method for reference as well.
class Array
def exponential_moving_average(idx=nil, tail=nil)
idx, tail = idx_and_tail_or_defaults(idx, tail)
valid_for_ma(idx, tail)
alpha = 2.0 / (tail + 1)
n = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1) * self[idx - tidx + 1]}.sum
d = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1)}.sum
n / d
end
alias_method :ema, :exponential_moving_average
end
I am the developer of Statsample-timeseries gem which is an extension of Statsample, an advance statistical suite in Ruby. It has quite many statistical methods (including EMA) which you can perform on your data.
If you need any assistance, I will be very happy to help out. :)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to write macro to pick up slices which I would like to include in my MIP. So far it looks like this:
LowerStack = getNumber("prompt", 10);
UpperStack = getNumber("prompt", 10);
run("Z Project...", "start=" + LowerStack) ("stop=" + UpperStack) ("projection=[Max Intensity]");
It recognizes the Lower slice which I want to pick up, but not the upper one.
Any suggestions on what do I do wrong?
The syntax of the third line is incorrect. This works:
LowerStack = getNumber("Lower", 10);
UpperStack = getNumber("Upper", 10);
run("Z Project...", "start=" + LowerStack + " stop=" + UpperStack + " projection=[Max Intensity]");
Note that I also changed the string in the two prompts because you would likely get an error by them not being unique.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm practicing F# for the first time and I thought I'd try to make a program that would calculate the areas of different kinds of shapes. However, the portion that finds the area of a circle is giving me a lot of trouble.enter code here
elif stringInput = "2" then
let PI = 3.14156
printfn "What is the circle's radius: "
let radiusString = System.Console.ReadLine()
let radiusInt = radiusString |> float
let cirlceArea = (radiusInt * radiusInt) * PI
printfn "The area of the circle is : %d" cirlceArea
I'm sure it has something to do with the radiusString |> float part of the code, but nothing I've tried works and I've had no luck in finding any examples that can help. What can I do?
Ok I just found out the problem was that I was using %d instead of %f
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to say if the desired location in the field is 1 return true otherwise return false. Why is this code not working?
fireShot :: Coordinate -> Field -> Bool
fireShot coord Shipfield
| nth ( fst(coord)((nth snd(coord)) ShipField) == 1 = True
| otherwise = False
The brackets in the guard are not balanced, you open five brackets, and you close four brackets. Furthermore variables start with a lowercase, so it should (probably) be shipfield, not Shipfield.
I think it might be better to use pattern matching to obtain the first and second coordinate, since this will make the code more clean. You furthermore do not need guards to return True and False. You can replace the function with:
fireShot :: Coordinate -> Field -> Bool
fireShot (x,y) shipfield = nth x (nth y shipfield) == 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Sorry Im new to rails. I just found a way to calculate the age in a model from here : Get person's age in Ruby
The function is this :
def age
now = Time.now.utc
now.year - birthday.year - (birthday.to_time.change(:year => now.year) > now ? 1 : 0)
end
Can some one please explain whats happening in the third line? I can't understand this :
(birthday.to_time.change(:year => now.year) > now ? 1 : 0)
That sentence is only trying to check if the birthday has already passed for the current year. If it has, then
(birthday.to_time.change(:year => now.year) > now ? 1 : 0)
will equal to 0. otherwise 1. this will be then subtracted like this:
now.year - birthday.year - 1 or now.year - birthday.year - 0
Hope that clarifies your doubt.
Just a suggestion, I would rather use irb and break up above code in smaller pieces and see what each part does exactly for myself. This would help me understand stuff better.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am doing a data structures and algorithms paper in which recurrence relations are being taught.
The question is as follows:
From what I understand from this question, n will keep on being halved over and over again. So what you are left with is 1/32n^2 + 1/16n^2 + 1/8n^2 + 1/4n^2 + 1/2n^2 + n^2. All the fractions sum to 1. So you're left with n^2 +n^2 = 2n^2.
However this is not a possible solution.
Can somebody please help me understand how to calculate these recurrence relations correctly, or point me in the right direction because I am having a lot of trouble with this topic and any help with be greatly appreciated.
Thank you for your time.
You might want to look at the Master Theorem
In the wiki, a = 1, b = 2, c = 2, where T(n) = aT(n/b) + n^c
Case 3 applies, since 2 > 0 = log_2(1)
Thus, by the master theorem, T = Big-Theta(n^c) = Big_Theta(n^2).
Choice B has a n^2 term, so that should be your answer.