Attribute Statistic Node Outputting Weird Mean. #97685

Open
opened 2022-04-28 18:49:30 +02:00 by Gerstmann Bradley · 11 comments

System Information
Operating system: Windows-10-10.0.17134-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 970M/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 466.11

Blender Version
Broken: version:
3.2.0 Alpha, branch: master, commit date: 2022-04-27 22:46, hash: 52a5f68562
3.1.2, branch: master, commit date: 2022-03-31 17:40, hash: cc66d1020c

Short description of error
image.png
Statistics.blend

**System Information** Operating system: Windows-10-10.0.17134-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 970M/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 466.11 **Blender Version** Broken: version: 3.2.0 Alpha, branch: master, commit date: 2022-04-27 22:46, hash: `52a5f68562` 3.1.2, branch: master, commit date: 2022-03-31 17:40, hash: `cc66d1020c` **Short description of error** ![image.png](https://archive.blender.org/developer/F13035434/image.png) [Statistics.blend](https://archive.blender.org/developer/F13035441/Statistics.blend)

Added subscriber: @Bradley_G

Added subscriber: @Bradley_G
Member

Added subscriber: @HooglyBoogly

Added subscriber: @HooglyBoogly
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

This is probably because it adds up all of the values and then divides by the total. It's quite easy to get overflow in that case. I'm not sure if there's a great solution, but there are probably ways to improve this case.

This is probably because it adds up all of the values and then divides by the total. It's quite easy to get overflow in that case. I'm not sure if there's a great solution, but there are probably ways to improve this case.
Member

Added subscriber: @LukasTonne

Added subscriber: @LukasTonne
Member

Hans is correct: storing a value of 0.2 as floating point has an error of about 2.980232e-9, which adds up quite a bit over a 1000000 loop. Here's a python snippet that shows the effect (using numpy's float32 because python floats are usually double precision while Blender uses single precision):

import numpy as np

print((np.float32(0.2) * 1000000) / 1000000)

x = np.float32(0.2)
for i in range(1000000):
    x = x + np.float32(0.2)
x = x / 1000000
print(x)

The effect is quite noticeable:

multiply and divide: 0.20000000298023224
loop sum and divide: 0.201916890625
Hans is correct: storing a value of 0.2 as floating point has an error of about 2.980232e-9, which adds up quite a bit over a 1000000 loop. Here's a python snippet that shows the effect (using numpy's float32 because python floats are usually double precision while Blender uses single precision): ``` import numpy as np print((np.float32(0.2) * 1000000) / 1000000) x = np.float32(0.2) for i in range(1000000): x = x + np.float32(0.2) x = x / 1000000 print(x) ``` The effect is quite noticeable: ``` multiply and divide: 0.20000000298023224 loop sum and divide: 0.201916890625 ```
Member

Added subscriber: @EAW

Added subscriber: @EAW
Member

Would Kahan Summation help?

Would Kahan Summation help?
Member

Oh this is exciting, i didn't know about Kahan sum:

import numpy as np

print("multiply and divide: ", (np.float32(0.2) * 1000000) / 1000000)

s = np.float32(0.0)
for i in range(1000000):
    s = s + np.float32(0.2)
s = s / 1000000
print("simple sum: ", s)

s = np.float32(0.0)
c = np.float32(0.0)
for i in range(1000000):
    y = np.float32(0.2) - c
    t = s + y
    c = (t - s) - y
    s = t
s = s / 1000000
print("Kahan sum: ", s)

This is an ideal case of course, all the terms being exactly equal, but still nice to see it works out. Thanks @EAW, i learned something useful today.

multiply and divide:  0.20000000298023224
simple sum:  0.2019166875
Kahan sum:  0.2
Oh this is exciting, i didn't know about Kahan sum: ``` import numpy as np print("multiply and divide: ", (np.float32(0.2) * 1000000) / 1000000) s = np.float32(0.0) for i in range(1000000): s = s + np.float32(0.2) s = s / 1000000 print("simple sum: ", s) s = np.float32(0.0) c = np.float32(0.0) for i in range(1000000): y = np.float32(0.2) - c t = s + y c = (t - s) - y s = t s = s / 1000000 print("Kahan sum: ", s) ``` This is an ideal case of course, all the terms being exactly equal, but still nice to see it works out. Thanks @EAW, i learned something useful today. ``` multiply and divide: 0.20000000298023224 simple sum: 0.2019166875 Kahan sum: 0.2 ```
Member

In #97685#1372874, @LukasTonne wrote:
Oh this is exciting, i didn't know about Kahan sum:
Thanks @EAW, i learned something useful today.

Glad to help!

> In #97685#1372874, @LukasTonne wrote: > Oh this is exciting, i didn't know about Kahan sum: > Thanks @EAW, i learned something useful today. Glad to help!

I've found another bug about statistical mean and recalled this report. perhaps due to same reason or?
Epsilon is as small as 1e-08.
image.png
Compare False.blend

I've found another bug about statistical mean and recalled this report. perhaps due to same reason or? Epsilon is as small as 1e-08. ![image.png](https://archive.blender.org/developer/F14132297/image.png) [Compare False.blend](https://archive.blender.org/developer/F14132300/Compare_False.blend)
Philipp Oeser removed the
Interest
Nodes & Physics
label 2023-02-10 08:44:04 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#97685
No description provided.