Is there a simple way to calculate sumproduct in Rails? - ruby-on-rails

I have a PositionGroup that has_many :positions.
Whenever a position_group object is touched, I would like to update pg.average_price like so:
# Average Price = ((position1.transaction_price * (position1.volume/total_volume) +
# position2.transaction_price * (position2.volume/total_volme))
In my callback method, I tried this:
def update_average_price
total_volume = positions.sum(:volume)
avg_price = positions.sum("transaction_price * (volume/#{total_volume})")
update_column(:average_price, avg_price)
end
But when I check the value of avg_price once multiple positions exist, I am getting 0.0.
This is my spec for this specific functionality:
it "should calculate the Weighted Average Purchase Price of all positions" do
# Position 3: Price = 20, volume = 200
# (Position 1 Price * (Position 1 Units/Total # of Units)) +
# (Position 2 Price * (Position 2 Units/Total # of Units)) +
# (Position 3 Price * (Position 3 Units/Total # of Units))
# ($10 * (100/400)) + ($15 * (100/400) + $20 * (100/400)) =
# ($2.50 + $3.75 + $5) = $11.25
price3 = 20
pos3 = create(:position, stock: stock, portfolio: portfolio, transaction_price: 20, current_price: price3, action: :buy, volume: 200, position_group: pg)
expect(pg.average_price).to eql 11.25
end
This is the result when I run it:
1) PositionGroup methods should calculate the Weighted Average Purchase Price of all positions
Failure/Error: expect(pg.average_price).to eql 11.25
expected: 11.25
got: 0.0
(compared using eql?)
I am pretty sure the issue is this line, from my callback method update_average_price on PositionGroup:
avg_price = positions.sum("transaction_price * (volume/#{total_volume})")
Is there a better way to approach this or is there something else giving me that 0.0 when I shouldn't be?

avg_price = positions.sum("transaction_price * (volume/#{total_volume.to_f})")
to_f is missing, converting to float to get a decimal to work with.
Example
irb(main):022:0> Position.all
Position Load (0.3ms) SELECT "positions".* FROM "positions" LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Position id: 1, transaction_price: 0.5e2, volume: 150, position_group_id: 1>, #<Position id: 2, transaction_price: 0.1e1, volume: 50, position_group_id: 1>]>
irb(main):023:0> Position.all.sum("transaction_price * (volume/#{total_volume.to_f})")
(0.2ms) SELECT SUM(transaction_price * (volume/200.0)) FROM "positions"
=> 37.75

Related

Sorting list of objects by object attribute?

I have a list of objects that are pulled in from an API. Here is the snippet of output (as there's about 300 lines):
combo =>
ID: 6, Name:Thomas Partey, Club:1, Position: 3, Price: $4.7, Total Pts: 57
ID: 7, Name:Martin Ødegaard, Club:1, Position: 3, Price: $7.0, Total Pts: 128
ID: 8, Name:Kieran Tierney, Club:1, Position: 2, Price: $4.6, Total Pts: 23
ID: 12, Name:Emile Smith Rowe, Club:1, Position: 3, Price: $5.6, Total Pts: 5
I would like to change the order so that they are ranked by Total Points rather than ID
I have tried the following:
sorted = combo.sort_by(#totalpoints)
As well as: (but I assume I want to try and use #teampoints since I've defined that)
sorted = combo.sort_by(:totalpoints)
My Full code is:
class Player
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
:active
def initialize(id, firstname, secondname, club, position, price, totalpoints, active)
#id = id.to_i
#firstname = firstname.to_s
#secondname = secondname.to_s
#club = club.to_s
#position = position.to_i
#price = price / 10.to_f
#totalpoints = totalpoints.to_i
#active = active.to_i
end
def to_s()
" ID: " + #id.to_s + ", Name:" + #firstname.to_s + " " + #secondname.to_s + ", Club:" + #club.to_s + ", Position: " + #position.to_s + ", Price: $" + #price.to_s + ", Total Pts: " + #totalpoints.to_s + " "
end
def self.pull()
require 'net/http'
require 'json'
url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
uri = URI(url)
response = Net::HTTP.get(uri)
object = JSON.parse(response)
elements = object["elements"]
elements.map! { |qb|
if qb["chance_of_playing_next_round"].to_f > 0
Player.new(
qb["id"], # ID
qb["first_name"], # First Name
qb["second_name"], # Surname
qb["team"], # Club
qb["element_type"], # Position
qb["now_cost"], # Current Price
qb["total_points"], # Total Points
qb["chance_of_playing_next_round"]) # Chance Of Playing
end
}
end
combo = Player.pull().map{|qb| qb}
sorted = combo.sort_by(#totalpoints)
puts sorted
end
Based on what you've got shown, this should do what you need:
sorted = combo.sort_by(&:totalpoints)
It's essentially a shortened version of this:
sorted = combo.sort_by { |_combo| _combo.totalpoints }

How to round Decimals to the First Significant Figure in Ruby

I am attempting to solve an edge case to a task related to a personal project.
It is to determine the unit price of a service and is made up of the total_amount and cost.
Examples include:
# 1
unit_price = 300 / 1000 # = 0.3
# 2
unit_price = 600 / 800 # = 0.75
# 3
unit_price = 500 / 1600 # = 0.3125
For 1 and 2, the unit_prices can stay as they are. For 3, rounding to 2 decimal places will be sufficient, e.g. (500 / 1600).round(2)
The issue arises when the float becomes long:
# 4
unit_price = 400 / 56000 # = 0.007142857142857143
What's apparent is that the float is rather long. Rounding to the first significant figure is the aim in such instances.
I've thought about using a regular expression to match the first non-zero decimal, or to find the length of the second part and apply some logic:
unit_price.match ~= /[^.0]/
unit_price.to_s.split('.').last.size
Any assistance would be most welcome
One should use BigDecimal for this kind of computation.
require 'bigdecimal'
bd = BigDecimal((400.0 / 56000).to_s)
#⇒ 0.7142857142857143e-2
bd.exponent
#⇒ -2
Example:
[10_000.0 / 1_000, 300.0 / 1_000, 600.0 / 800,
500.0 / 1_600, 400.0 / 56_000].
map { |bd| BigDecimal(bd.to_s) }.
map do |bd|
additional = bd.exponent >= 0 ? 0 : bd.exponent + 1
bd.round(2 - additional) # THIS
end.
map(&:to_f)
#⇒ [10.0, 0.3, 0.75, 0.31, 0.007]
You can detect the length of the zeros string with regex. It's a bit ugly, but it works:
def significant_round(number, places)
match = number.to_s.match(/\.(0+)/)
return number unless match
zeros = number.to_s.match(/\.(0+)/)[1].size
number.round(zeros+places)
end
pry(main)> significant_round(3.14, 1)
=> 3.14
pry(main)> significant_round(3.00014, 1)
=> 3.0001
def my_round(f)
int = f.to_i
f -= int
coeff, exp = ("%e" % f).split('e')
"#{coeff.to_f.round}e#{exp}".to_f + int
end
my_round(0.3125)
#=> 0.3
my_round(-0.3125)
#=> -0.3
my_round(0.0003625)
#=> 0.0004
my_round(-0.0003625)
#=> -0.0004
my_round(42.0031)
#=> 42.003
my_round(-42.0031)
#=> -42.003
The steps are as follows.
f = -42.0031
int = f.to_i
#=> -42
f -= int
#=> -0.0031000000000034333
s = "%e" % f
#=> "-3.100000e-03"
coeff, exp = s.split('e')
#=> ["-3.100000", "-03"]
c = coeff.to_f.round
#=> -3
d = "#{c}e#{exp}"
#=> "-3e-03"
e = d.to_f
#=> -0.003
e + int
#=> -42.003
To instead keep only the most significant digit after rounding, change the method to the following.
def my_round(f)
coeff, exp = ("%e" % f).split('e')
"#{coeff.to_f.round}e#{exp}".to_f
end
If f <= 0 this returns the same as the earlier method. Here is an example when f > 0:
my_round(-42.0031)
#=> -40.0

I am trying to do arithmetic on table values and keep getting an error. here is my code

I am trying to perform arithmetic on table values and keep getting an error. Here is my total code. I am basically trying to generate simplex noise. I have created a multidimensional array (table) and am trying to perform operations on the values but i keep getting an error that says I cannot perform arithmetic on a table value. I don't know If I have to convert it to something or what. Please Help.
totalNoiseMap = {}
function simplex_noise(width, height)
simplexnoise = {}
for i = 1,512 do
simplexnoise[i] = {}
for j = 1, 512 do
simplexnoise[i][j] = 0
end
end
frequency = 5.0 / width
for x = 1, width do
for y = 1, height do
simplexnoise[x][y] = noise(x * frequency,y * frequency)
simplexnoise[x][y] = (simplexnoise[x][y] + 1) / 2
end
end
return simplexnoise
end
function noise(x, y, frequency)
return simplex_noise(x / frequency, y / frequency)
end
function generateOctavedSimplexNoise(width,height,octaves,roughness,scale)
totalnoise = {}
for i = 1,512 do
totalnoise[i] = {}
for j = 1, 512 do
totalnoise[i][j] = 0
end
end
layerFrequency = scale
layerWeight = 1
weightSum = 0
for octave = 1, octaves do
for x = 1, width do
for y = 1, height do
totalnoise[x][y] = (totalnoise[x][y] + noise(x * layerFrequency,y * layerFrequency, 2) * layerWeight)
end
end
--Increase variables with each incrementing octave
layerFrequency = layerFrequency * 2
weightSum = weightSum + layerWeight
layerWeight = layerWeight * roughness
end
return totalnoise
end
totalNoiseMap = generateOctavedSimplexNoise(512, 512, 3, 0.4, 0.005)
totalnoise[x][y] + noise(x * layerFrequency,y * layerFrequency, 2) * layerWeight
Here you get table noise(x * layerFrequency,y * layerFrequency, 2), mult it by scalar layerWeight and than add it to scalar totalnoise[x][y].
I can think of how to multiply table by scalar - it should be something like
for i = 1,512 do
for j = 1,512 do
a[i][j] = t[i][j] * scalar
end
end
But I'm unable to get what you're trying to do by adding. Suppose it should be addition of two tables
for i = 1,512 do
for j = 1,512 do
a[i][j] = b[i][j] + c[i][j]
end
end
But it works only with same-sized tables

Unexpected result determining coordinates on a nested (grid) array in rails

I have a nested array representing an "image" (a map of 0's and 1's). My end goal is to transform the 4 numbers surrounding any "1" to also be 1's.
The approach I've taken is to map the x,y coordinates of any existing 1 in the initial grid and add those coordinates to a new array so I can later use them to perform the transformation on the original.
To simplify, I'm trying to get this to work, initially, on an array that includes only one "1" — however, the result is unexpected in that it's storing multiple sets of x,y coordinates for the one "1" in the array, instead of a single set. I'm sure the solution is simple, but as a beginner, I'm stumped as to why this is happening.
(Please ignore the commented code; it's the beginnings of the transformation, but I'll bring it back once I solve this issue.)
class Image
def initialize(image)
#image = image
end
def output_image
x_size = #image.first.length
y_size = #image.length
edit = []
#image.each_with_index do | row , y |
row.each_with_index do |cell, x |
edit << [x,y] if cell == 1
end
# edit.each do |pair|
# x = pair.first
# y = pair.last
# #image[x-1][y] = 1 if x > 0
# #image[x+1][y] = 1 if x < (x_size - 1)
# #image[x][y-1] = 1 if y > 0
# #image[x][y+1] = 1 if y < (y_size - 1)
# end
puts edit.inspect
#puts #image.inspect
# #image.each { |x| puts x.join }
end
end
end
image = Image.new([
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
])
image.output_image
This results in:
[]
[[2, 1]]
[[2, 1]]
[[2, 1]]
Rather than the expected:
[]
[[2,1]]
[]
[]
You're not clearing the edit variable before each run of the outer loop. So each time you print it, it's still storing the initial coordinate where you found a 1.
You'll get the expected result if you insert edit = [] after puts edit.inspect.
This does what you want:
def output_image
x_size = #image.first.length
y_size = #image.length
edit = []
#image.each_with_index do | row , y |
row.each_with_index do |cell, x |
edit << [x,y] if cell == 1
end
end
edit.each do |pair|
y = pair.first
x = pair.last
#image[x-1][y] = 1 if x > 0
#image[x+1][y] = 1 if x < (x_size - 1)
#image[x][y-1] = 1 if y > 0
#image[x][y+1] = 1 if y < (y_size - 1)
end
#image.each { |x| puts x.join }
puts edit.inspect
end
Thanks again to Rob for the help. This was the solution I was looking for:
def output_image
x_size = #image.first.length
y_size = #image.length
edit = []
#image.each_with_index do | row , x |
row.each_with_index do |cell, y |
edit << [x,y] if cell == 1
end
end
edit.each do |x,y|
#image[x-1][y] = 1 if x > 0
#image[x+1][y] = 1 if x < (x_size - 1)
#image[x][y-1] = 1 if y > 0
#image[x][y+1] = 1 if y < (y_size - 1)
end
#image.each { |x| puts x.join }
end

Binary clock with Lua, how to remove dots that aren't used?

I have the following binary clock that I grabbed from this wiki article (the one that's for v1.5.*) for the awesome WM:
binClock = wibox.widget.base.make_widget()
binClock.radius = 1.5
binClock.shift = 1.8
binClock.farShift = 2
binClock.border = 1
binClock.lineWidth = 1
binClock.colorActive = beautiful.bg_focus
binClock.fit = function(binClock, width, height)
local size = math.min(width, height)
return 6 * 2 * binClock.radius + 5 * binClock.shift + 2 * binClock.farShift + 2 * binClock.border + 2 * binClock.border, size
end
binClock.draw = function(binClock, wibox, cr, width, height)
local curTime = os.date("*t")
local column = {}
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 2, 2))))
local bigColumn = 0
for i = 0, 5 do
if math.floor(i / 2) > bigColumn then
bigColumn = bigColumn + 1
end
for j = 0, 3 do
if string.sub(column[i + 1], j + 1, j + 1) == "0" then
active = false
else
active = true
end
binClock:draw_point(cr, bigColumn, i, j, active)
end
end
end
binClock.dec_bin = function(binClock, inNum)
inNum = tonumber(inNum)
local base, enum, outNum, rem = 2, "01", "", 0
while inNum > (base - 1) do
inNum, rem = math.floor(inNum / base), math.fmod(inNum, base)
outNum = string.sub(enum, rem + 1, rem + 1) .. outNum
end
outNum = inNum .. outNum
return outNum
end
binClock.draw_point = function(binClock, cr, bigColumn, column, row, active)
cr:arc(binClock.border + column * (2 * binClock.radius + binClock.shift) + bigColumn * binClock.farShift + binClock.radius,
binClock.border + row * (2 * binClock.radius + binClock.shift) + binClock.radius, 2, 0, 2 * math.pi)
if active then
cr:set_source_rgba(0, 0.5, 0, 1)
else
cr:set_source_rgba(0.5, 0.5, 0.5, 1)
end
cr:fill()
end
binClocktimer = timer { timeout = 1 }
binClocktimer:connect_signal("timeout", function() binClock:emit_signal("widget::updated") end)
binClocktimer:start()
First, if something isn't by default already in Lua that's because this is to be used in the config file for awesome. :)
OK, so what I need is some guidance actually. I am not very familiar with Lua currently, so some guidance is all I ask so I can learn. :)
OK, so first, this code outputs a normal binary clock, but every column has 4 dots (44,44,44), instead of a 23,34,34 setup for the dots, as it would be in a normal binary clock. What's controlling that in this code? So that I can pay around with it.
Next, what controls the color? Right now it's gray background and quite a dark green, I want to brighten both of those up.
And what controls the smoothing? Right now it's outputting circles, would like to see what it's like for it to output squares instead.
That's all I need help with, if you can point me to the code and some documentation for what I need, that should be more than enough. :)
Also, if somebody would be nice enough to add some comments, that also would be awesome. Don't have to be very detailed comments, but at least to the point where it gives an idea of what each thing does. :)
EDIT:
Found what modifies the colors, so figured that out. None of the first variables control if it's a square or circle BTW. :)
The draw_point function draws the dots.
The two loops in the draw function are what create the output and is where the columns come from. To do a 23/34/34 layout you would need to modify the inner loop skip the first X points based on the counter of the outer loop I believe.

Resources