feat():initial version
This commit is contained in:
78
build_all.sh
Executable file
78
build_all.sh
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/bin/sh
|
||||
|
||||
BASE_DIR=`pwd`
|
||||
|
||||
export TARGET_HOST="rk3576" #Modify your target platform here
|
||||
export BUILD_HOST="ubuntu"
|
||||
|
||||
|
||||
if [ "rk3576" = ${TARGET_HOST} ];then
|
||||
echo "--- cross build boost, target host: ${TARGET_HOST} ----"
|
||||
### begine of config.mk ###
|
||||
export SDK_VERSION=AG35CEVDMR08A03T4G_OCPU_SDK
|
||||
|
||||
## lastword ${MAKEFILE_LIST} is ../../config.mk
|
||||
export SOC_PATH=${BASE_DIR}/../../
|
||||
export SDK_PATH=${SOC_PATH}/../SDK/${SDK_VERSION}
|
||||
export INSTALL_PATH=${SOC_PATH}/build/install
|
||||
|
||||
export TOOLCHAIN_DIR="/opt"
|
||||
#export SYSROOT="${SDK_PATH}/ql-ol-crosstool/sysroots/armv7ahf-neon-oe-linux-gnueabi"
|
||||
export TOOLCHAIN="${TOOLCHAIN_DIR}/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin"
|
||||
export COMPILER_PREFIX="aarch64-none-linux-gnu-"
|
||||
export MY_CC="${TOOLCHAIN}/${COMPILER_PREFIX}gcc"
|
||||
export MY_AR="${TOOLCHAIN}/${COMPILER_PREFIX}ar"
|
||||
export MY_CXX="${TOOLCHAIN}/${COMPILER_PREFIX}g++"
|
||||
#export MY_CFLAGS=--sysroot="${SYSROOT} -I ${SYSROOT}/usr/include/ql_lib_utils -I ${SYSROOT}/usr/include/quectel-openlinux-sdk"
|
||||
#export MY_LDFLAGS=--sysroot="${SYSROOT}"
|
||||
export MY_CFLAGS="-march=armv7-a -marm -mfpu=neon -mfloat-abi=hard"
|
||||
|
||||
#CFLAGS += -Dposix -ggdb -O0 -Wlogical-op -Wtype-limits -Wsign-conversion -Wsign-compare -Wshadow -Wpointer-arith \
|
||||
# -rdynamic -funwind-tables -ffunction-sections -D__DEBUG_EN -D_GNU_SOURCE
|
||||
#LDFLAGS += -lpthread -lrt
|
||||
#
|
||||
### TCU compile parameter configure
|
||||
#CFLAGS += -D _TCU_INSTALL_PREFIX="/oemapp"
|
||||
#CFLAGS += -D _TCU_ETH_SPEED=1000
|
||||
#CFLAGS += -D _TCU_V2X_EN=0
|
||||
#CFLAGS += -D _TCU_IDPS_EN=0
|
||||
#CFLAGS += -D _TCU_DEBUG_EN=1
|
||||
#CFLAGS += -D _TCU_DV_EN=0
|
||||
### end of config.mk ###
|
||||
elif [ "ubuntu" = ${TARGET_HOST} ];then
|
||||
echo "--- build boost, target host: ${TARGET_HOST} ----"
|
||||
su sudo
|
||||
else
|
||||
|
||||
echo "--- add export MY_CC/MY_CXX of [${TARGET_HOST}] here ---"
|
||||
exit # add export MY_CC/MY_CXX .etc of new platform here ,and remove 'exit'
|
||||
echo "--- build boost, target host: ${TARGET_HOST} ----"
|
||||
fi
|
||||
|
||||
${BASE_DIR}/build_boost.sh
|
||||
|
||||
if [ "rk3576" = ${TARGET_HOST} ];then
|
||||
echo "--- cross build vsomeip, target host: ${TARGET_HOST} ----"
|
||||
# TODO : add :[-Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3] ?
|
||||
export CC="${TOOLCHAIN}/${COMPILER_PREFIX}gcc"
|
||||
export AR="${TOOLCHAIN}/${COMPILER_PREFIX}ar"
|
||||
export CXX="${TOOLCHAIN}/${COMPILER_PREFIX}g++"
|
||||
#export CFLAGS=--sysroot="${SYSROOT} -I ${SYSROOT}/usr/include/ql_lib_utils -I ${SYSROOT}/usr/include/quectel-openlinux-sdk"
|
||||
#export LDFLAGS=--sysroot="${SYSROOT}"
|
||||
#export CFLAGS="-march=armv7-a -marm -mfpu=neon -mfloat-abi=hard"
|
||||
|
||||
elif [ "ubuntu" = ${TARGET_HOST} ];then
|
||||
echo "--- build vsomeip, target host: ${TARGET_HOST} ----"
|
||||
else
|
||||
|
||||
echo "--- add export CC/CXX of [${TARGET_HOST}] here ---"
|
||||
# add export CC/CXX .etc of new platform here
|
||||
echo "--- build vsomeip, target host: ${TARGET_HOST} ----"
|
||||
fi
|
||||
|
||||
${BASE_DIR}/build_vsomeip.sh
|
||||
|
||||
${BASE_DIR}/build_example.sh
|
||||
echo "--- build finished ---"
|
||||
sleep 1
|
||||
exit
|
||||
40
build_boost.sh
Executable file
40
build_boost.sh
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
|
||||
BASE_DIR=`pwd`
|
||||
BOOST=boost_1_75_0
|
||||
SOURCE_DIR=${BASE_DIR}/source/
|
||||
INSTALL_DIR=${BASE_DIR}/install/
|
||||
|
||||
#检测source_code
|
||||
cd ${SOURCE_DIR}
|
||||
if [ ! -d ${SOURCE_DIR}/${BOOST} ];then
|
||||
tar xvf boost_1_75_0.tar.gz
|
||||
fi
|
||||
rm -rf ${INSTALL_DIR}/${BOOST}
|
||||
mkdir -p ${INSTALL_DIR}/${BOOST}
|
||||
# 3.配置
|
||||
echo "--- config start ---"
|
||||
cd ${SOURCE_DIR}/${BOOST}
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
echo "--- config cross build ----"
|
||||
./bootstrap.sh \
|
||||
--with-libraries=system,thread,log \
|
||||
--prefix=${INSTALL_DIR}/${BOOST}
|
||||
#sed -r -i "s#using gcc ;#using gcc : arm : ${MY_CC} ;#" project-config.jam
|
||||
sed -i "/using gcc/c using gcc : : ${MY_CC} ; " project-config.jam
|
||||
else
|
||||
./bootstrap.sh \
|
||||
--with-libraries=system,thread,log
|
||||
fi
|
||||
# 4.编译
|
||||
echo "--- make start ---"
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
./b2 --prefix=${INSTALL_DIR}/${BOOST} link=shared -j4 install #2>&1 | tee ./build.log
|
||||
else
|
||||
./b2 link=shared -j4 install #2>&1 | tee ./build.log
|
||||
fi
|
||||
echo "--- make end ---"
|
||||
|
||||
rm -rf ${SOURCE_DIR}/${BOOST}
|
||||
echo "--- cleaned build folder ---"
|
||||
|
||||
61
build_example.sh
Executable file
61
build_example.sh
Executable file
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
|
||||
#TARGET_HOST=rk3576
|
||||
BASE_DIR=`pwd`
|
||||
BOOST=boost_1_75_0
|
||||
VSOMEIP=vsomeip-3.7.3
|
||||
EXAMPLE=example
|
||||
SOURCE_DIR=${BASE_DIR}/source
|
||||
INSTALL_DIR=${BASE_DIR}/install
|
||||
|
||||
#检测source_code
|
||||
cd ${SOURCE_DIR}
|
||||
if [ ! -d ${SOURCE_DIR}/${EXAMPLE} ];then
|
||||
echo "error : no example code"
|
||||
exit
|
||||
fi
|
||||
|
||||
# clean install dir
|
||||
rm -rf ${INSTALL_DIR}/${EXAMPLE}/${TARGET_HOST}
|
||||
mkdir -p ${INSTALL_DIR}/${EXAMPLE}/${TARGET_HOST}
|
||||
|
||||
#预编译 camke
|
||||
echo "--- start to cmake ---"
|
||||
cd ${SOURCE_DIR}/${EXAMPLE}
|
||||
if [ -d ./build_${TARGET_HOST} ];then
|
||||
rm -rf ./build_${TARGET_HOST}
|
||||
echo "--- delete build folder ---"
|
||||
fi
|
||||
|
||||
mkdir -p build_${TARGET_HOST}
|
||||
cd build_${TARGET_HOST}
|
||||
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
cmake -DBOOST_ROOT=${INSTALL_DIR}/${BOOST} \
|
||||
-DBoost_INCLUDE_DIR=${INSTALL_DIR}/${BOOST}/include \
|
||||
-DBOOST_LIBRARY_DIR=${INSTALL_DIR}/${BOOST}/lib \
|
||||
-DVSOMEIP_LIBRARY_DIR=${INSTALL_DIR}/${VSOMEIP}/lib \
|
||||
-DVSOMEIP_INCLUDE_DIR=${INSTALL_DIR}/${VSOMEIP}/include \
|
||||
-DCMAKE_INSTALL_PREFIX:PATH=${INSTALL_DIR}/${EXAMPLE} \
|
||||
-DENABLE_SIGNAL_HANDLING=1 \
|
||||
..
|
||||
else
|
||||
cmake ..
|
||||
fi
|
||||
|
||||
#编译
|
||||
echo "--start to make--"
|
||||
make clean
|
||||
make -j12
|
||||
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
cp *app* ${INSTALL_DIR}/${EXAMPLE}/${TARGET_HOST}
|
||||
else
|
||||
sudo cp *app* ${INSTALL_DIR}/${EXAMPLE}/${TARGET_HOST}
|
||||
fi
|
||||
cd ..
|
||||
rm -rf build_${TARGET_HOST}
|
||||
# make install
|
||||
#rm -rf ${SOURCE_DIR}/${VSOMEIP}
|
||||
cd ${BASE_DIR}
|
||||
echo "--- build example finished ---"
|
||||
54
build_vsomeip.sh
Executable file
54
build_vsomeip.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/bin/sh
|
||||
|
||||
|
||||
BASE_DIR=`pwd`
|
||||
BOOST=boost_1_75_0
|
||||
VSOMEIP=vsomeip-3.7.3
|
||||
SOURCE_DIR=${BASE_DIR}/source/
|
||||
INSTALL_DIR=${BASE_DIR}/install/
|
||||
|
||||
#检测source_code
|
||||
cd ${SOURCE_DIR}
|
||||
if [ ! -d ${SOURCE_DIR}/${VSOMEIP} ];then
|
||||
tar xvf vsomeip-3.7.3.tar.gz
|
||||
fi
|
||||
# clean install dir
|
||||
rm -rf ${INSTALL_DIR}/${VSOMEIP}
|
||||
mkdir -p ${INSTALL_DIR}/${VSOMEIP}
|
||||
#预编译 camke
|
||||
echo "--- start to cmake ---"
|
||||
cd ${SOURCE_DIR}/${VSOMEIP}
|
||||
if [ -d ./build_${TARGET_HOST} ];then
|
||||
rm -rf ./build_${TARGET_HOST}
|
||||
echo "--- delete build folder ---"
|
||||
fi
|
||||
|
||||
mkdir -p build_${TARGET_HOST}
|
||||
cd build_${TARGET_HOST}
|
||||
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
cmake -DBOOST_ROOT=${INSTALL_DIR}/${BOOST} \
|
||||
-DBoost_INCLUDE_DIR=${INSTALL_DIR}/${BOOST}/include \
|
||||
-DCMAKE_INSTALL_PREFIX:PATH=${INSTALL_DIR}/${VSOMEIP} \
|
||||
-DENABLE_SIGNAL_HANDLING=1 \
|
||||
..
|
||||
|
||||
#-DBoost_INCLUDE_DIR=${INSTALL_DIR}/${BOOST}/include \
|
||||
#-DCMAKE_TOOLCHAIN_FILE=${BASE_DIR}/vsomeip_local_compile.cmake
|
||||
else
|
||||
cmake ..
|
||||
fi
|
||||
|
||||
#编译
|
||||
echo "--start to make--"
|
||||
make clean
|
||||
make -j12
|
||||
make vsomeip_ctrl
|
||||
if [ ${BUILD_HOST} != ${TARGET_HOST} ];then
|
||||
make install
|
||||
else
|
||||
sudo make install
|
||||
echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/vsomeip.conf
|
||||
fi
|
||||
rm -rf ${SOURCE_DIR}/${VSOMEIP}
|
||||
echo "--- cleaned build folder ---"
|
||||
53
code_generator/backup/StudentsRedisDao.cpp
Normal file
53
code_generator/backup/StudentsRedisDao.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/// @class StudentsRedisDao
|
||||
|
||||
/// @brief cpp底层接口类 操作students内容
|
||||
|
||||
/// TABLE students
|
||||
|
||||
/// @author test.py
|
||||
|
||||
/// @generate date: 2022-07-19 09:19:17
|
||||
|
||||
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
|
||||
|
||||
include “StudentsRedisDao.h”
|
||||
|
||||
include “include/students_t.h”
|
||||
|
||||
include “RedisManager.h”
|
||||
|
||||
include “common/LogMacros.h”
|
||||
|
||||
include “common/StringUtility/OtherStringFunc.h”
|
||||
|
||||
include “common/DateTime.h”
|
||||
|
||||
namespace redisdao{
|
||||
|
||||
#define PRIMARY_KEY “123”
|
||||
|
||||
const string StudentsRedisDao::TABLE_NAME = “students”;
|
||||
|
||||
const string StudentsRedisDao::TABLE_ID = “115”; //在数据库中的表的唯一性标识符
|
||||
|
||||
const string StudentsRedisDao::KEY_SEPARETER = “stu”;
|
||||
|
||||
StudentsRedisDao::StudentsRedisDao(void)
|
||||
{
|
||||
|
||||
if ( 0 == m_reHandler.EnsureConnect())
|
||||
m_bRedisConnected = true;
|
||||
else
|
||||
m_bRedisConnected = false;
|
||||
}
|
||||
|
||||
StudentsRedisDao::~StudentsRedisDao(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int StudentsRedisDao::InsertRecord(const string& strVal)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
53
code_generator/backup/cpp.template
Normal file
53
code_generator/backup/cpp.template
Normal file
@@ -0,0 +1,53 @@
|
||||
/// @class ${CLASSNAME}
|
||||
|
||||
/// @brief cpp底层接口类 操作${TABLE_NAME}内容
|
||||
|
||||
/// TABLE ${TABLE_NAME_UPPER}
|
||||
|
||||
/// @author test.py
|
||||
|
||||
/// @generate date: ${GENE_DATE}
|
||||
|
||||
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
|
||||
|
||||
include “${CLASSNAME}.h”
|
||||
|
||||
include “include/${TABLE_NAME}_t.h”
|
||||
|
||||
include “RedisManager.h”
|
||||
|
||||
include “common/LogMacros.h”
|
||||
|
||||
include “common/StringUtility/OtherStringFunc.h”
|
||||
|
||||
include “common/DateTime.h”
|
||||
|
||||
namespace redisdao{
|
||||
|
||||
#define PRIMARY_KEY “${PRIMER_KEY}”
|
||||
|
||||
const string ${CLASSNAME}::TABLE_NAME = “${TABLE_NAME}”;
|
||||
|
||||
const string ${CLASSNAME}::TABLE_ID = “${TABLE_ID}”; //在数据库中的表的唯一性标识符
|
||||
|
||||
const string ${CLASSNAME}::KEY_SEPARETER = “${KEY_SEPARETER}”;
|
||||
|
||||
${CLASSNAME}::${CLASSNAME}(void)
|
||||
{
|
||||
|
||||
if ( 0 == m_reHandler.EnsureConnect())
|
||||
m_bRedisConnected = true;
|
||||
else
|
||||
m_bRedisConnected = false;
|
||||
}
|
||||
|
||||
${CLASSNAME}::~${CLASSNAME}(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int ${CLASSNAME}::InsertRecord(const string& strVal)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
52
code_generator/backup/genService.py
Normal file
52
code_generator/backup/genService.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os,sys,re,traceback
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from string import Template
|
||||
|
||||
def generate(self):
|
||||
tableName = 'students'
|
||||
className = '%sRedisDao' % tableName.capitalize()
|
||||
filePath = r'%s.cpp' % className
|
||||
class_file = open(filePath,'w')
|
||||
lines = []
|
||||
#模版文件
|
||||
template_file = open(r'cpp.template','r')
|
||||
tmpl = Template(template_file.read())
|
||||
#模版替换
|
||||
lines.append(tmpl.substitute(
|
||||
CLASSNAME = className, #类名设置
|
||||
TABLE_NAME = tableName, #成员变量设置
|
||||
PRIMER_KEY = 123, #宏定义设置
|
||||
KEY_SEPARETER = 'stu', #成员变量设置
|
||||
TABLE_NAME_UPPER = tableName, #未使用
|
||||
GENE_DATE = datetime.now().strftime('%Y-%m-%d %H:%M:%S'), #代码生成日期设置
|
||||
TABLE_ID = '115', #成员变量设置
|
||||
EXPIRE_DATE = '06JUN14')) #未使用
|
||||
# 0.将生成的代码写入文件
|
||||
class_file.writelines(lines)
|
||||
class_file.close()
|
||||
print('generate %s over. ~_~' % filePath)
|
||||
|
||||
if __name__ == "__main__":
|
||||
#template = "hello %s , your website is %s " % ("大CC","http://blog.me115.com")
|
||||
#print(template)
|
||||
|
||||
## 1.0 字符串替换
|
||||
#tmp = "%s is %s" % ("wate", "fire")
|
||||
#print(tmp)
|
||||
|
||||
## 2.0 format格式化替换
|
||||
#tp = "it is {name}, the {name} age is {age}".format(name="lihao", age=18)
|
||||
#print(tp)
|
||||
|
||||
## 3.0 模板替换
|
||||
#tempTemplate = Template("Hello $name,your website is $message")
|
||||
#print(tempTemplate.substitute(name='CCP',message='http://blog.me115.com'))
|
||||
|
||||
# auto generate code
|
||||
print("generate code start...")
|
||||
generate("auto_code")
|
||||
|
||||
33593
code_generator/backup/get-pip.py
Normal file
33593
code_generator/backup/get-pip.py
Normal file
File diff suppressed because it is too large
Load Diff
112
code_generator/backup/old/main.py
Normal file
112
code_generator/backup/old/main.py
Normal file
@@ -0,0 +1,112 @@
|
||||
# coding=utf-8
|
||||
import time
|
||||
import tkinter as tk
|
||||
from tkinter import Button, Checkbutton, Entry, Image, IntVar, Label, StringVar, scrolledtext, ttk, filedialog, messagebox
|
||||
from datetime import datetime
|
||||
from string import Template
|
||||
import tkinter_ui as gui
|
||||
import template as tmpl
|
||||
|
||||
|
||||
# 获取当前时间
|
||||
def Time(): #getCurrentTime
|
||||
current_time = time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time()))
|
||||
return current_time
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# #全局风格类型变量
|
||||
# entry_width = 30
|
||||
# short_entry_width = 15
|
||||
# font_pix = 15
|
||||
# font_type = "微软雅黑"
|
||||
# #window.geometry('1000x500+0+0')
|
||||
# #window.resizable(0,0)
|
||||
# #setWindowCenter(window, 700, 400) #窗口居中,传入窗口句柄和窗口宽高
|
||||
|
||||
#主窗口
|
||||
#实例化一个窗体
|
||||
window = gui.instantiate_the_window("Someip Service Code Generator") #窗口名
|
||||
note = gui.instantiate_the_note(window)
|
||||
#在窗体win中创建一个Notebook,并承载以下两个选项卡
|
||||
TabMain = gui.instantiate_the_tab(note, "Main") #选项卡1
|
||||
Tab2 = gui.instantiate_the_tab(note, "Tab2")
|
||||
|
||||
## 以下为Main选项卡
|
||||
# 第一组 :Create/Modify
|
||||
group_app = gui.instantiate_the_group(TabMain,"App") #创建Frame一组
|
||||
|
||||
|
||||
|
||||
#设置输入框控件,使用textvariable参数来接收变量
|
||||
# 定义输入框1
|
||||
label1 = Label(group_app, text='Run Config File Path')
|
||||
label1.grid(sticky="w", padx=15)
|
||||
entry1 = Entry(group_app, width=tmpl.entry_width, textvariable=tmpl.runConfigFilename)
|
||||
entry1.grid(row=0,column=1,sticky="w")
|
||||
entry1.config(state='normal') #'readonly'
|
||||
# Button(group_app, text='...', command=open_file).grid(row=0, column=2)
|
||||
# 定义输入框2
|
||||
label2 = Label(group_app, text='App name')
|
||||
label2.grid(sticky="w", padx=15)
|
||||
entry2 = Entry(group_app, width=tmpl.entry_width, textvariable=tmpl.appName)
|
||||
entry2.grid(row=1,column=1,sticky="w")
|
||||
# 定义输入框3
|
||||
label3 = Label(group_app, text='Service ID')
|
||||
label3.grid(sticky="w", padx=15)
|
||||
entry3 = Entry(group_app, width=tmpl.entry_width, textvariable=tmpl.serviceID)
|
||||
entry3.grid(row=2,column=1)
|
||||
entry3.config(state='readonly')
|
||||
# 定义输入框4
|
||||
label4 = Label(group_app, text='Instance ID')
|
||||
label4.grid(sticky="w", padx=15)
|
||||
entry4 = Entry(group_app, width=tmpl.ntry_width, textvariable=tmpl.instanceID)
|
||||
entry4.grid(row=3,column=1)
|
||||
entry4.config(state='readonly')
|
||||
|
||||
|
||||
# 设置复选框控件,使用variable参数来接收变量
|
||||
check0 = Checkbutton(window, text="use_tcp",variable = tmpl.use_tcp,onvalue=1,offvalue=0)
|
||||
check1 = Checkbutton(window, text="offer_F-F_method",variable = tmpl.offer_F_F_method,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
check2 = Checkbutton(window, text="offer_R-R_method",variable = tmpl.offer_R_R_method,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
check3 = Checkbutton(window, text="offer_setter",variable = tmpl.offer_setter,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
check4 = Checkbutton(window, text="offer_getter",variable = tmpl.offer_getter,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
check56 = Checkbutton(window, text="offer_event",variable = tmpl.offer_event,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
check78 = Checkbutton(window, text="offer_field_notifier",variable = tmpl.offer_field_notifier,onvalue=1,offvalue=0, command=tmpl.entryState)
|
||||
|
||||
check0.grid(sticky="w")
|
||||
check1.grid(sticky="w")
|
||||
check2.grid(sticky="w")
|
||||
check3.grid(sticky="w")
|
||||
check4.grid(sticky="w")
|
||||
check56.grid(sticky="w")
|
||||
check78.grid(sticky="w")
|
||||
|
||||
# 设置各种服务类型的IDs,使用textvariable参数来接收变量
|
||||
idEntry1 = Entry(window, width=tmpl.entry_width, textvariable=tmpl.F_F_method_ids)
|
||||
idEntry2 = Entry(window, width=tmpl.entry_width, textvariable=tmpl.R_R_method_ids)
|
||||
idEntry3 = Entry(window, width=tmpl.entry_width, textvariable=tmpl.setter_ids)
|
||||
idEntry4 = Entry(window, width=tmpl.entry_width, textvariable=tmpl.getter_ids)
|
||||
idEntry5 = Entry(window, width=tmpl.short_entry_width, textvariable=tmpl.eventgroup_ids)
|
||||
idEntry6 = Entry(window, width=tmpl.short_entry_width, textvariable=tmpl.event_ids)
|
||||
idEntry7 = Entry(window, width=tmpl.short_entry_width, textvariable=tmpl.ield_eventgroup_ids)
|
||||
idEntry8 = Entry(window, width=tmpl.short_entry_width, textvariable=tmpl.field_event_ids)
|
||||
|
||||
idEntry1.grid_forget()
|
||||
idEntry2.grid_forget()
|
||||
idEntry3.grid_forget()
|
||||
idEntry4.grid_forget()
|
||||
idEntry5.grid_forget()
|
||||
idEntry6.grid_forget()
|
||||
idEntry7.grid_forget()
|
||||
idEntry8.grid_forget()
|
||||
|
||||
# 按钮
|
||||
button1 = Button(window, text="Generate", command=tmpl.generate)
|
||||
button1.grid(row=11, column=0, sticky="w", padx=15, pady=5)
|
||||
|
||||
button2 = Button(window, text='Exit',command=window.quit)
|
||||
button2.grid(row=11, column=1, sticky="e", padx=15, pady=5)
|
||||
|
||||
window.mainloop()
|
||||
54
code_generator/backup/old/sample_cfg_ids.hpp.template
Normal file
54
code_generator/backup/old/sample_cfg_ids.hpp.template
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
|
||||
/// @author liuzhimin@huaxu.com
|
||||
|
||||
/// @generate date: ${GENE_DATE}
|
||||
|
||||
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
|
||||
|
||||
#ifndef VSOMEIP_${APP_NAME}_CFG_IDS_HPP
|
||||
#define VSOMEIP_${APP_NAME}_CFG_IDS_HPP
|
||||
|
||||
#define VSOMEIP_CONFIGURATION "${RUN_CONFIG_FILENAME}"
|
||||
#define APP_NAME "${APP_NAME}"
|
||||
|
||||
#define ${APP_NAME}_SERVICE_ID ${SERVICE_ID}
|
||||
#define ${APP_NAME}_INSTANCE_ID ${INSTANCE_ID}
|
||||
|
||||
#define USE_TCP ${USE_TCP}
|
||||
|
||||
#define OFFER_F_F_METHOD ${OFFER_F_F_METHOD}
|
||||
#define OFFER_R_R_METHOD ${OFFER_R_R_METHOD}
|
||||
#define OFFER_SETTER ${OFFER_SETTER}
|
||||
#define OFFER_GETTER ${OFFER_GETTER}
|
||||
#define OFFER_EVENT ${OFFER_EVENT}
|
||||
#define OFFER_FIELD_EVENT ${OFFER_FIELD_EVENT}
|
||||
|
||||
#if OFFER_F_F_METHOD
|
||||
#define ${APP_NAME}_${FF_METHOD_NAME}_F_F_METHOD_ID 0x0100 //0x0101,0x0102...
|
||||
#endif
|
||||
|
||||
#if OFFER_R_R_METHOD
|
||||
#define ${APP_NAME}_${RR_METHOD_NAME}_R_R_METHOD_ID 0x0200 //0x0201,0x0202...
|
||||
#endif
|
||||
|
||||
#if OFFER_GETTER
|
||||
#define ${APP_NAME}_${GET_METHOD_NAME}_GET_METHOD_ID 0x0300 //0x0301,0x0302...
|
||||
#endif
|
||||
|
||||
#if OFFER_SETTER
|
||||
#define ${APP_NAME}_${SET_METHOD_NAME}_SET_METHOD_ID 0x0400 //0x0401,0x0402...
|
||||
#endif
|
||||
|
||||
#if OFFER_EVENT
|
||||
#define ${APP_NAME}_${EVENTGROUP_NAME}_EVENTGROUP_ID 0x8100 //0x8200,0x8300...
|
||||
#define ${APP_NAME}_${EVENT_NAME}_EVENT_ID 0x8101 //0x8102,0x8103...
|
||||
#endif
|
||||
|
||||
#if OFFER_FIELD_EVENT
|
||||
#define ${APP_NAME}_${FIELD_EVENTGROUP_NAME}_FIELD_EVENTGROUP_ID 0x8800 //0x8900,0x8a00...
|
||||
#define ${APP_NAME}_${FIELD_EVENT_NAME}_FIELD_EVENT_ID 0x8801 //0x8802,0x8803...
|
||||
#endif
|
||||
|
||||
#endif // VSOMEIP_EXAMPLES_SAMPLE_CFG_IDS_HPP
|
||||
|
||||
54
code_generator/backup/old/tcu_cfg_ids.hpp
Normal file
54
code_generator/backup/old/tcu_cfg_ids.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
|
||||
/// @author liuzhimin@huaxu.com
|
||||
|
||||
/// @generate date: 2022-07-20 16:22:40
|
||||
|
||||
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
|
||||
|
||||
#ifndef VSOMEIP_TCU_CFG_IDS_HPP
|
||||
#define VSOMEIP_TCU_CFG_IDS_HPP
|
||||
|
||||
#define VSOMEIP_CONFIGURATION "/etc/vsomeip"
|
||||
#define APP_NAME "TCU"
|
||||
|
||||
#define TCU_SERVICE_ID 0x1000
|
||||
#define TCU_INSTANCE_ID 0x2000
|
||||
|
||||
#define USE_TCP 1
|
||||
|
||||
#define OFFER_F_F_METHOD 1
|
||||
#define OFFER_R_R_METHOD 1
|
||||
#define OFFER_SETTER 1
|
||||
#define OFFER_GETTER 1
|
||||
#define OFFER_EVENT 0
|
||||
#define OFFER_FIELD_EVENT 1
|
||||
|
||||
#if OFFER_F_F_METHOD
|
||||
#define TCU_ADASOFF_F_F_METHOD_ID 0x0100 //0x0101,0x0102...
|
||||
#endif
|
||||
|
||||
#if OFFER_R_R_METHOD
|
||||
#define TCU_CAMSTATUS_R_R_METHOD_ID 0x0200 //0x0201,0x0202...
|
||||
#endif
|
||||
|
||||
#if OFFER_GETTER
|
||||
#define TCU_DISTANCE_GET_METHOD_ID 0x0300 //0x0301,0x0302...
|
||||
#endif
|
||||
|
||||
#if OFFER_SETTER
|
||||
#define TCU_LIMITEDSPEED_SET_METHOD_ID 0x0400 //0x0401,0x0402...
|
||||
#endif
|
||||
|
||||
#if OFFER_EVENT
|
||||
#define TCU_SIGNDETECTED_EVENTGROUP_ID 0x8100 //0x8200,0x8300...
|
||||
#define TCU_SPEEDLIMITSIGNDETECTED_EVENT_ID 0x8101 //0x8102,0x8103...
|
||||
#endif
|
||||
|
||||
#if OFFER_FIELD_EVENT
|
||||
#define TCU_SIGNDETECTED_FIELD_EVENTGROUP_ID 0x8800 //0x8900,0x8a00...
|
||||
#define TCU_SPEEDLIMITSIGNDETECTED_FIELD_EVENT_ID 0x8801 //0x8802,0x8803...
|
||||
#endif
|
||||
|
||||
#endif // VSOMEIP_EXAMPLES_SAMPLE_CFG_IDS_HPP
|
||||
|
||||
112
code_generator/backup/old/template.py
Normal file
112
code_generator/backup/old/template.py
Normal file
@@ -0,0 +1,112 @@
|
||||
# coding=utf-8
|
||||
from datetime import datetime
|
||||
from string import Template
|
||||
from tkinter import Image, IntVar, StringVar,filedialog, messagebox
|
||||
|
||||
#以下是所有需获取的变量
|
||||
runConfigFilename = StringVar("/etc/vsomeip")
|
||||
appName = StringVar(None,"tcu")
|
||||
serviceID = StringVar(None,"0x1000")
|
||||
instanceID = StringVar(None,"0x2000")
|
||||
|
||||
use_tcp = IntVar()
|
||||
offer_F_F_method = IntVar()
|
||||
offer_R_R_method = IntVar()
|
||||
offer_setter = IntVar()
|
||||
offer_getter = IntVar()
|
||||
offer_event = IntVar()
|
||||
offer_field_notifier = IntVar()
|
||||
|
||||
F_F_method_ids = StringVar(None,"adasOFF")
|
||||
R_R_method_ids = StringVar(None,"camStatus")
|
||||
setter_ids = StringVar(None,"limitedSpeed")
|
||||
getter_ids = StringVar(None,"distance")
|
||||
eventgroup_ids = StringVar(None,"SignDetected")
|
||||
event_ids = StringVar(None,"speedLimitSignDetected")
|
||||
field_eventgroup_ids = StringVar(None,"SignDetected")
|
||||
field_event_ids = StringVar(None,"speedLimitSignDetected")
|
||||
|
||||
def generate():
|
||||
filePath = r'%s_cfg_ids.hpp' % appName.get().lower()
|
||||
class_file = open(filePath,'w')
|
||||
lines = []
|
||||
#模版文件
|
||||
template_file = open(r'sample_cfg_ids.hpp.template','r')
|
||||
tmpl = Template(template_file.read())
|
||||
#模版替换
|
||||
lines.append(tmpl.substitute(
|
||||
GENE_DATE = datetime.now().strftime('%Y-%m-%d %H:%M:%S'), #代码生成日期设置
|
||||
RUN_CONFIG_FILENAME = runConfigFilename.get(),
|
||||
APP_NAME = appName.get().upper(),
|
||||
#MODULE_NAME = appName.get().upper(),
|
||||
SERVICE_ID = serviceID.get(),
|
||||
INSTANCE_ID = instanceID.get(),
|
||||
USE_TCP = use_tcp.get(),
|
||||
OFFER_F_F_METHOD = offer_F_F_method.get(),
|
||||
OFFER_R_R_METHOD = offer_R_R_method.get(),
|
||||
OFFER_SETTER = offer_setter.get(),
|
||||
OFFER_GETTER = offer_getter.get(),
|
||||
OFFER_EVENT = offer_event.get(),
|
||||
OFFER_FIELD_EVENT = offer_field_notifier.get(),
|
||||
FF_METHOD_NAME = F_F_method_ids.get().upper(),
|
||||
RR_METHOD_NAME = R_R_method_ids.get().upper(),
|
||||
GET_METHOD_NAME = getter_ids.get().upper(),
|
||||
SET_METHOD_NAME = setter_ids.get().upper(),
|
||||
EVENTGROUP_NAME = eventgroup_ids.get().upper(),
|
||||
EVENT_NAME = event_ids.get().upper(),
|
||||
FIELD_EVENTGROUP_NAME = field_eventgroup_ids.get().upper(),
|
||||
FIELD_EVENT_NAME = field_event_ids.get().upper(),
|
||||
))
|
||||
# 0.将生成的代码写入文件
|
||||
class_file.writelines(lines)
|
||||
class_file.close()
|
||||
print('generate "%s" success. ~_~' % filePath)
|
||||
messagebox.showinfo("tips",'Generate "%s" success.' % filePath)
|
||||
|
||||
def open_file():
|
||||
try:
|
||||
global file
|
||||
filepath = filedialog.askopenfilename() # 打开文件,返回该文件的完整路径
|
||||
runConfigFilename.set(filepath)
|
||||
file = Image.open(runConfigFilename.get())
|
||||
except Exception as e:
|
||||
print("您没有选择任何文件",e)
|
||||
|
||||
def entryState():
|
||||
None
|
||||
# if offer_F_F_method.get():
|
||||
# idEntry1.grid(row=5,column=1)
|
||||
# else:
|
||||
# idEntry1.grid_forget() # the same as grid_remove()
|
||||
|
||||
# if offer_R_R_method.get():
|
||||
# idEntry2.grid(row=6,column=1)
|
||||
# else:
|
||||
# idEntry2.grid_forget()
|
||||
|
||||
# if offer_setter.get():
|
||||
# idEntry3.grid(row=7,column=1)
|
||||
# else:
|
||||
# idEntry3.grid_forget()
|
||||
|
||||
# if offer_getter.get():
|
||||
# idEntry4.grid(row=8,column=1)
|
||||
# else:
|
||||
# idEntry4.grid_forget()
|
||||
|
||||
# if offer_event.get():
|
||||
# idEntry5.grid(sticky="w",row=9,column=1)
|
||||
# idEntry6.grid(sticky="e",row=9,column=1)
|
||||
# else:
|
||||
# idEntry5.grid_forget()
|
||||
# idEntry6.grid_forget()
|
||||
|
||||
# if offer_field_notifier.get():
|
||||
# idEntry7.grid(sticky="w",row=10,column=1)
|
||||
# idEntry8.grid(sticky="e",row=10,column=1)
|
||||
# else:
|
||||
# idEntry7.grid_forget()
|
||||
# idEntry8.grid_forget()
|
||||
|
||||
def modifyContent():
|
||||
print("modifyContent")
|
||||
52
code_generator/backup/old/tkinter_ui.py
Normal file
52
code_generator/backup/old/tkinter_ui.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# coding=utf-8
|
||||
from doctest import OutputChecker
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from tkinter import scrolledtext, ttk, messagebox
|
||||
import time
|
||||
|
||||
# 获取当前时间
|
||||
def Time(): #getCurrentTime
|
||||
current_time = time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time()))
|
||||
return current_time
|
||||
|
||||
#设置窗口宽高,以及窗口在屏幕居中显示
|
||||
def setWindowCenter(root, width, height):
|
||||
screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
|
||||
screenheight = root.winfo_screenheight() # 获取显示屏高度
|
||||
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数
|
||||
root.geometry(size) # 让窗口居中显示
|
||||
|
||||
def instantiate_the_window(window_name):
|
||||
#实例化一个窗体
|
||||
window = tk.Tk()
|
||||
window.title(window_name) #窗口名
|
||||
#setWindowCenter(window, 800, 400) #窗口居中,传入窗口句柄和窗口宽高
|
||||
return window
|
||||
|
||||
def instantiate_the_note(window):
|
||||
#在窗体win中创建一个Notebook,并承载以下两个选项卡
|
||||
note = ttk.Notebook(window)
|
||||
return note
|
||||
|
||||
def instantiate_the_tab(note, tab_name):
|
||||
#在窗体win中创建一个Notebook,并承载以下两个选项卡
|
||||
tab = ttk.Frame(note,relief="solid",borderwidth=1)
|
||||
note.add(tab, text = tab_name) #选项卡
|
||||
note.pack()
|
||||
return tab
|
||||
|
||||
def instantiate_the_group(tab, group_name, _row=0, _column=0, _padx=10, _pady=10):
|
||||
group = tk.LabelFrame(tab, text=group_name) #创建Frame一组
|
||||
group.grid(row = _row, column = _column, padx = _padx, pady = _pady)
|
||||
return group
|
||||
|
||||
def instantiate_the_element(group,ele_type,ele_name,row,column,padx,pady):
|
||||
#if ele_type
|
||||
None
|
||||
|
||||
# 打印日志定向到滚动文本框
|
||||
def direct_log_to_scrolledtext(tab):
|
||||
log_data_Text = scrolledtext.ScrolledText(tab, width=100, height=10, relief="solid")
|
||||
log_data_Text.pack()
|
||||
sys.stdout = OutputChecker.StdoutRedirector(log_data_Text)
|
||||
314
code_generator/backup/tk_main.py
Normal file
314
code_generator/backup/tk_main.py
Normal file
@@ -0,0 +1,314 @@
|
||||
#encoding: utf-8
|
||||
|
||||
import threading
|
||||
import time
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from tkinter import IntVar, StringVar, scrolledtext, ttk, messagebox,filedialog
|
||||
|
||||
import stdout as out
|
||||
import getchipmsg as getchip
|
||||
import rwexcle as rwsht
|
||||
import sendemail as smail
|
||||
import configmail as cfg
|
||||
|
||||
|
||||
class _Main: #调用SysTrayIcon的Demo窗口
|
||||
|
||||
def __init__(s):
|
||||
s.searching_state = False #搜索状态:是否正在搜索
|
||||
s.running_tk_win = False
|
||||
s.auto_run_thread = None #
|
||||
s.SysTrayIcon = None # 判断是否打开系统托盘图标
|
||||
# def __del__(s):
|
||||
# s.auto_run_thread.join()
|
||||
|
||||
def main(s):
|
||||
s.running_tk_win = True
|
||||
#实例化一个窗体
|
||||
s.win = tk.Tk()
|
||||
s.win.title("Find Chips") #窗口名
|
||||
s.setWindowCenter(s.win, 700, 700) #窗口居中,传入窗口句柄和窗口宽高
|
||||
|
||||
# #在窗体win中创建一个Notebook,并承载以下两个选项卡
|
||||
# note = ttk.Notebook(win)
|
||||
# TabMain = ttk.Frame(note,relief="solid",borderwidth=1)
|
||||
# note.add(TabMain, text = "") #选项卡1
|
||||
# #TabDB = ttk.Frame(note,relief="solid",borderwidth=1)
|
||||
# #note.add(TabDB, text = "Database")#选项卡2
|
||||
# note.pack()
|
||||
|
||||
## 以下为Main选项卡
|
||||
# 第一组 :芯片信息
|
||||
s.group_chipForm= tk.LabelFrame(s.win, text="chipForm") #创建Frame一组
|
||||
s.group_chipForm.grid(row=0, column=0, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.form_label=tk.Label(s.group_chipForm, text="芯片表格:")
|
||||
s.form_label.grid(row=0, column=0, padx=10, pady=10, sticky="w") #创建“芯片表格”字样的Label
|
||||
|
||||
s.file_path_entry = tk.Entry(s.group_chipForm,text='select a form', width = 50) #创建输入框
|
||||
s.file_path_entry.grid(row=0, column=1, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.select_file_button = tk.Button(s.group_chipForm, text="打开芯片表格自动完成搜索", command=s.open_excle_and_search_chips)
|
||||
s.select_file_button.grid(row=0, column=2, padx=10, pady=10, sticky="w")
|
||||
|
||||
|
||||
# 第二组 芯片信息显示列表
|
||||
s.dataTreeview = ttk.Treeview(s.win, column=('1', '2', '3', '4', '5', '6', '7', '8'), show='headings')
|
||||
#s.dataTreeview.place(x=10, y=10, width=660, height=350)
|
||||
s.dataTreeview.grid(row=1, column=0, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.dataTreeview.column('1', width=80, anchor="center")
|
||||
s.dataTreeview.column('2', width=80, anchor="center")
|
||||
s.dataTreeview.column('3', width=80, anchor="center")
|
||||
s.dataTreeview.column('4', width=80, anchor="center")
|
||||
s.dataTreeview.column('5', width=80, anchor="center")
|
||||
s.dataTreeview.column('6', width=80, anchor="center")
|
||||
s.dataTreeview.column('7', width=80, anchor="center")
|
||||
s.dataTreeview.column('8', width=80, anchor="center")
|
||||
# 头显示
|
||||
s.dataTreeview.heading('1', text='型号/参数')
|
||||
s.dataTreeview.heading('2', text='品牌')
|
||||
s.dataTreeview.heading('3', text='需求量')
|
||||
s.dataTreeview.heading('4', text='需求价格')
|
||||
s.dataTreeview.heading('5', text='分销商')
|
||||
s.dataTreeview.heading('6', text='库存')
|
||||
s.dataTreeview.heading('7', text='价格')
|
||||
s.dataTreeview.heading('8', text='是否符合需求')
|
||||
|
||||
# 显示并刷新数据库列表
|
||||
s.showAllInfo(s.dataTreeview,None)
|
||||
|
||||
|
||||
# 第三组 :邮件信息
|
||||
s.group_email= tk.LabelFrame(s.win, text="email") #创建Frame一组
|
||||
s.group_email.grid(row=2, column=0, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.server_addr=tk.Label(s.group_email, text="SMTP服务器地址:")
|
||||
s.server_addr.grid(row=0, column=0, padx=10, pady=10, sticky="w") #创建“SMTP服务器地址”字样的Label
|
||||
s.server_addr_entry = tk.Entry(s.group_email,text='', width=30) #创建SMTP服务器地址输入框
|
||||
s.server_addr_entry.grid(row=0, column=1, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.server_port=tk.Label(s.group_email, text="端口:")
|
||||
s.server_port.grid(row=0, column=1, padx=90, pady=10, sticky="e") #创建“SMTP服务器地址”字样的Label
|
||||
s.server_port_entry = tk.Entry(s.group_email,text='', width=10) #创建SMTP服务器地址输入框
|
||||
s.server_port_entry.grid(row=0, column=1, padx=10, pady=10, sticky="e")
|
||||
|
||||
s.sender_addr=tk.Label(s.group_email, text="发件人地址:")
|
||||
s.sender_addr.grid(row=1, column=0, padx=10, pady=10, sticky="w") #创建“发件人地址”字样的Label
|
||||
s.sender_addr_entry = tk.Entry(s.group_email,text='', width=50) #创建发件人地址输入框
|
||||
s.sender_addr_entry.grid(row=1, column=1, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.sender_passwd=tk.Label(s.group_email, text="密码(或授权码):")
|
||||
s.sender_passwd.grid(row=2, column=0, padx=10, pady=10, sticky="w") #创建“发件人地址”字样的Label
|
||||
s.sender_passwd_entry = tk.Entry(s.group_email,text='', width=50, show='*') #创建发件人地址输入框
|
||||
s.sender_passwd_entry.grid(row=2, column=1, padx=10, pady=10, sticky="w")
|
||||
|
||||
s.receiver_addr=tk.Label(s.group_email, text="收件人(逗号隔开):")
|
||||
s.receiver_addr.grid(row=3, column=0, padx=10, pady=10, sticky="w") #创建“发件人地址”字样的Label
|
||||
s.receiver_addr_entry = tk.Entry(s.group_email,text='', width=50) #创建发件人地址输入框
|
||||
s.receiver_addr_entry.grid(row=3, column=1, padx=10, pady=10, sticky="w")
|
||||
|
||||
# “发送邮件”按钮
|
||||
s.send_email_button = tk.Button(s.group_email, text="发送邮件", command=s.send_email)
|
||||
s.send_email_button.grid(row=0, column=2, padx=10, pady=10, sticky="e")
|
||||
|
||||
# 第四组 自动运行
|
||||
s.group_autorun= tk.LabelFrame(s.win, text="autoRun")
|
||||
s.group_autorun.grid(row=2, column=0, padx=10, pady=10, sticky="se")
|
||||
#s.CheckButtonVar = tk.StringVar()
|
||||
s.check_button_text = StringVar()
|
||||
s.check_button_text.set('当前未自动搜索')
|
||||
s.check_button_value = IntVar()
|
||||
s.run_check_button = tk.Checkbutton(s.group_autorun, text="当前未自动搜索",variable = s.check_button_value, textvariable = s.check_button_text, state='normal',command=s.auto_search)
|
||||
s.run_check_button.grid(row=0, column=0, padx=10, pady=10, sticky="w")# sticky="e")
|
||||
|
||||
s.mail_check_button_value = IntVar()
|
||||
s.mail_check_button = tk.Checkbutton(s.group_autorun, text="符合需求则邮件",variable = s.mail_check_button_value,onvalue = 1, offvalue = 0, state='normal')
|
||||
s.mail_check_button.grid(row=1, column=0, padx=10, pady=10, sticky="w")# sticky="e")
|
||||
|
||||
# s.run_background_button = tk.Button(s.win, text="后台运行", command=s.run_gackground)
|
||||
# s.run_background_button.grid(row=2, column=0, padx=10, pady=10, sticky="se")
|
||||
|
||||
# # 日志框
|
||||
s.log_data_Text = scrolledtext.ScrolledText(s.win, width=90, height=9, relief="solid")
|
||||
s.log_data_Text.grid(row=3, column=0, padx=10, pady=10, sticky="w")
|
||||
sys.stdout = out.StdoutRedirector(s.log_data_Text)
|
||||
|
||||
s.get_mail_config_info()
|
||||
|
||||
##去除“最小化到任务托盘”的功能,目前这功能会使得“查看桌面”的快捷键和鼠标最右下角出现问题
|
||||
#s.win.bind("<Unmap>", lambda event: s.Hidden_window() if s.win.state() == 'iconic' else False) #窗口最小化判断,可以说是调用最重要的一步
|
||||
#s.win.protocol('WM_DELETE_WINDOW', s.exit) #点击Tk窗口关闭时直接调用s.exit,不使用默认关闭
|
||||
##win32gui.ShowWindow(win, 0 )
|
||||
|
||||
# 单独创建一个线程用于自动运行和发送邮件
|
||||
s.auto_run_thread = threading.Thread(target=s.schedule_stsk)
|
||||
s.auto_run_thread.start()
|
||||
# # s.auto_run_thread.join()
|
||||
|
||||
s.win.mainloop()
|
||||
s.running_tk_win = False
|
||||
s.auto_run_thread.join()
|
||||
# #tk窗口
|
||||
# s.root = tk.Tk()
|
||||
# s.root.bind("<Unmap>", lambda event: s.Hidden_window() if s.root.state() == 'iconic' else False) #窗口最小化判断,可以说是调用最重要的一步
|
||||
# s.root.protocol('WM_DELETE_WINDOW', s.exit) #点击Tk窗口关闭时直接调用s.exit,不使用默认关闭
|
||||
# s.root.resizable(0,0) #锁定窗口大小不能改变
|
||||
# s.root.mainloop()
|
||||
|
||||
def exit(s, _sysTrayIcon = None):
|
||||
s.win.destroy()
|
||||
#print ('exit...')
|
||||
|
||||
def get_mail_config_info(s):
|
||||
cfg.touch_config_file() #
|
||||
s.server_addr_entry.insert(0, cfg.getConfig('email','host'))
|
||||
s.server_port_entry.insert(0, cfg.getConfig('email','port'))
|
||||
s.sender_addr_entry.insert(0, cfg.getConfig('email','sender'))
|
||||
s.sender_passwd_entry.insert(0, cfg.getConfig('email','password'))
|
||||
s.receiver_addr_entry.insert(0, cfg.getConfig('email','receiver'))
|
||||
|
||||
# mail_host = "smtp.qq.com" #SMTP服务器地址
|
||||
# mail_sender = "lzm0819@qq.com" #账号
|
||||
# mail_passwd = "zzqkxlaqroidjixx" #密码(或者是授权码!如qq邮箱需要在设置里获取授权码!)
|
||||
# mail_receives="1150732137@qq.com,jimyliu@qq.com" #收件人,多个收件人地址用英文逗号隔开
|
||||
def send_email(s):
|
||||
path = s.file_path_entry.get()
|
||||
mail_host = s.server_addr_entry.get()
|
||||
mail_port = s.server_port_entry.get()
|
||||
mail_sender = s.sender_addr_entry.get()
|
||||
mail_passwd = s.sender_passwd_entry.get()
|
||||
mail_receives = s.receiver_addr_entry.get()
|
||||
if path and mail_host and mail_port and mail_sender and mail_passwd and mail_receives:
|
||||
smail.send_email_with_attachment(path,mail_host,mail_port,mail_sender,mail_passwd,mail_receives)
|
||||
cfg.setConfig('chipForm','path',path)
|
||||
cfg.setConfig('email','host',mail_host)
|
||||
cfg.setConfig('email','port',mail_port)
|
||||
cfg.setConfig('email','sender',mail_sender)
|
||||
cfg.setConfig('email','password',mail_passwd)
|
||||
cfg.setConfig('email','receiver',mail_receives)
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"请选择芯片表格并正确填写邮件信息!")
|
||||
messagebox.showwarning("Warning","请选择芯片表格并正确填写邮件信息!")
|
||||
|
||||
def auto_search(s):
|
||||
# 改变Checkbutton的显示值
|
||||
if s.check_button_value.get() == 1:
|
||||
path = s.file_path_entry.get()
|
||||
if path !="":
|
||||
s.run_check_button.select()
|
||||
s.check_button_text.set('自动搜索中 ...... ')
|
||||
|
||||
# 单独创建一个线程用于自动运行和发送邮件
|
||||
# s.auto_run_thread = threading.Thread(target=s.schedule_stsk)
|
||||
# s.auto_run_thread.start()
|
||||
# s.auto_run_thread.join()
|
||||
else:
|
||||
s.run_check_button.deselect()
|
||||
s.check_button_text.set('当前未自动搜索')
|
||||
print(out.Time(),"Warn:"+"请选择芯片表格或正确填写表格路径!")
|
||||
messagebox.showwarning("Warning","请选择芯片表格或正确填写表格路径!")
|
||||
else:
|
||||
s.run_check_button.deselect()
|
||||
s.check_button_text.set('当前未自动搜索')
|
||||
|
||||
# def auto_mail(s):
|
||||
# if s.mail_check_button_value.get() == 1:
|
||||
# s.send_email()
|
||||
# else:
|
||||
# None
|
||||
|
||||
def schedule_stsk(s):
|
||||
# 当前执行一次,且以后每两小时执行一次
|
||||
schedule.every(2).hours.do(s.auto_run)
|
||||
#schedule.every(2).hour.until("18:00").do(s.auto_run))
|
||||
#schedule.every(2).minutes.do(s.auto_run)
|
||||
while s.running_tk_win :
|
||||
schedule.run_pending()
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
def auto_run(s):
|
||||
# 如果勾选了自动运行,且当前未运行搜索,就去搜索
|
||||
if (s.check_button_value.get() == 1) and (s.searching_state == False):
|
||||
s.searching_state == True # searching_state相当于锁
|
||||
get_msg_state,match_request_state = s.get_chips_msg()
|
||||
s.searching_state == False
|
||||
# 勾选了“符合条件则邮件”,且搜索信息成功,且符合需求量及价格,就发邮件
|
||||
if (s.mail_check_button_value.get() == 1) and get_msg_state and match_request_state:
|
||||
s.send_email()
|
||||
|
||||
|
||||
def get_file_path(s):
|
||||
file_path = filedialog.askopenfilename(title="Select A File", filetypes=(
|
||||
("Microsoft Excle 文件", "*.xlsx"), ("CSV", "*.csv")))
|
||||
s.file_path_entry.delete(0, 'end')
|
||||
s.file_path_entry.insert(0, file_path) #写入value
|
||||
#chipmsg_excle_path = file_path
|
||||
if file_path != "":
|
||||
#s.get_msg_button.config(state="normal")
|
||||
chip_mfg_list=rwsht.read_excle_get_chips_list("input",file_path)
|
||||
if chip_mfg_list:
|
||||
print(out.Time(),"Info:"+"获取待搜索芯片列表成功。")
|
||||
s.showAllInfo(s.dataTreeview,chip_mfg_list)
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"芯片型号未按顺序填写或列表为空!")
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"未选择excle表格!")
|
||||
return file_path
|
||||
|
||||
def get_chips_msg(s):
|
||||
get_msg_state = False
|
||||
match_request_state = False
|
||||
path = s.file_path_entry.get()
|
||||
if path != "":
|
||||
chip_mfg_list=rwsht.read_excle_get_chips_list("input",path)
|
||||
if chip_mfg_list != []:
|
||||
#print(out.Time(),"获取待搜索芯片列表成功。")
|
||||
print(out.Time(),"Info:"+"开始搜索...")
|
||||
all_chips_list,match_request_state = getchip.serch_all_chips_from_list(chip_mfg_list)
|
||||
#print(out.Time(),all_chips_list)
|
||||
if all_chips_list != []:
|
||||
print(out.Time(),"Info:"+"搜索芯片列表信息完成。")
|
||||
write_lines=rwsht.write_chips_msg_to_excle(path,all_chips_list)
|
||||
if write_lines != 0:
|
||||
print(out.Time(),"Info:"+"保存芯片库存价格等信息成功。")
|
||||
chip_output_list=rwsht.read_excle_get_chips_list("output",path)
|
||||
s.showAllInfo(s.dataTreeview,chip_output_list)
|
||||
get_msg_state = True
|
||||
#messagebox.showwarning("Warning","表格信息显示不全,详细信息请参考excle文档!")
|
||||
print(out.Time(),"Warn:"+"表格预览显示不完整,详细信息请参考excle文档!")
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"保存芯片库存价格等信息失败!")
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"搜索芯片信息失败!")
|
||||
else:
|
||||
#print(out.Time(),"芯片型号未按顺序填写或列表为空!")
|
||||
None
|
||||
else:
|
||||
print(out.Time(),"Warn:"+"文件路径为空!")
|
||||
messagebox.showwarning("Warning","文件路径为空!")
|
||||
return get_msg_state,match_request_state
|
||||
|
||||
#设置窗口宽高,以及窗口在屏幕居中显示
|
||||
def setWindowCenter(s,root, width, height):
|
||||
screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
|
||||
screenheight = root.winfo_screenheight() # 获取显示屏高度
|
||||
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数
|
||||
root.geometry(size) # 让窗口居中显示
|
||||
|
||||
# 显示函数
|
||||
def showAllInfo(s,dataTreeview_,list):
|
||||
# 先删除显示列表
|
||||
if list:
|
||||
x = dataTreeview_.get_children()
|
||||
for item in x:
|
||||
dataTreeview_.delete(item)
|
||||
for item in range(len(list)):
|
||||
dataTreeview_.insert("", item, text="line1", values=list[item])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
Main = _Main()
|
||||
Main.main()
|
||||
BIN
code_generator/backup/vsomeip_0529.zip
Normal file
BIN
code_generator/backup/vsomeip_0529.zip
Normal file
Binary file not shown.
45
code_generator/backup/vsomeip_client.json
Normal file
45
code_generator/backup/vsomeip_client.json
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"unicast" : "192.168.225.25",
|
||||
"logging" :
|
||||
{
|
||||
"level" : "debug",
|
||||
"console" : "true",
|
||||
"file" : { "enable" : "true", "path" : "/var/log/vsomeip.log" },
|
||||
"dlt" : "true"
|
||||
},
|
||||
"applications" :
|
||||
[
|
||||
{
|
||||
"name" : "ag_client",
|
||||
"id" : "0x1343"
|
||||
}
|
||||
],
|
||||
"clients" :
|
||||
[
|
||||
{
|
||||
"service" : "0x5034",
|
||||
"instance" : "0x6034",
|
||||
"unreliable" : "30509"
|
||||
},
|
||||
{
|
||||
"service" : "0x5042",
|
||||
"instance" : "0x6042",
|
||||
"reliable" : "30609"
|
||||
}
|
||||
],
|
||||
"routing" : "ag_client",
|
||||
"service-discovery" :
|
||||
{
|
||||
"enable" : "true",
|
||||
"multicast" : "239.71.0.2",
|
||||
"port" : "30490",
|
||||
"protocol" : "udp",
|
||||
"initial_delay_min" : "10",
|
||||
"initial_delay_max" : "100",
|
||||
"repetitions_base_delay" : "100",
|
||||
"repetitions_max" : "3",
|
||||
"ttl" : "3",
|
||||
"cyclic_offer_delay" : "2000",
|
||||
"request_response_delay" : "100"
|
||||
}
|
||||
}
|
||||
732
code_generator/someip_app/read_excel.py
Normal file
732
code_generator/someip_app/read_excel.py
Normal file
@@ -0,0 +1,732 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
===============================================================
|
||||
Python模块:Python操作excel(.xlsx)封装类MyPyXlsToXlsx、myPyXwBook和myPyXwSheet
|
||||
Date:2022年02月07日
|
||||
我的博客:https://datutu.blog.csdn.net/
|
||||
===============================================================
|
||||
"""
|
||||
import re
|
||||
import xlwings as xw
|
||||
from openpyxl.utils import get_column_letter
|
||||
import os
|
||||
import os.path
|
||||
|
||||
|
||||
class MyPyXlsToXlsx:
|
||||
"""
|
||||
类功能: Python使用xlwings操作将xls文件转换为xlsx文件格式类
|
||||
"""
|
||||
|
||||
def __init__(self, strOpenFilePath: str):
|
||||
xlsFilePath = strOpenFilePath
|
||||
# 文件存在就加载,不存在就创建工作簿。
|
||||
if os.path.exists(xlsFilePath):
|
||||
# 文件存在就加载,加载失败就报错。
|
||||
try:
|
||||
app = xw.App(visible=False, add_book=False)
|
||||
app.display_alerts = False # 关闭一些提示信息,可以加快运行速度。 默认为 True。
|
||||
app.screen_updating = False # 更新显示工作表的内容。默认为 True。关闭它也可以提升运行速度。
|
||||
workbook = app.books.open(xlsFilePath) # 打开现有excel
|
||||
workbook.api.SaveAs(xlsFilePath + "x", FileFormat=51) # 参数 51 为xlsx格式;56为 Excel 97-2003的xls版本
|
||||
app.kill() # 使用kill()关闭进程
|
||||
os.remove(xlsFilePath) # 删除老的xls文件
|
||||
except IOError:
|
||||
print("加载xls文件失败!请检查!")
|
||||
else:
|
||||
print("已成功将xls文件转换为xlsx文件!!!\n")
|
||||
else:
|
||||
print("加载的xls文件不存在!请检查!")
|
||||
|
||||
|
||||
class myPyXwBook:
|
||||
"""
|
||||
类功能: Python使用xlwings操作excel(.xlsx)封装类myPyXwBook
|
||||
"""
|
||||
def __init__(self, strOpenFilePath: str):
|
||||
"""
|
||||
:function: 初始化
|
||||
:param strOpenFilePath: 打开的文件绝对路径
|
||||
"""
|
||||
self.strFilePath = strOpenFilePath
|
||||
self.app = None
|
||||
# 工作簿对象
|
||||
self.workbook = None
|
||||
|
||||
# 文件存在就加载,不存在就创建工作簿。
|
||||
if os.path.exists(self.strFilePath):
|
||||
# 文件存在就加载,加载失败就报错。
|
||||
try:
|
||||
self.app = xw.App(visible=False, add_book=False)
|
||||
self.app.display_alerts = False # 关闭一些提示信息,可以加快运行速度。 默认为 True。
|
||||
self.app.screen_updating = False # 更新显示工作表的内容。默认为 True。关闭它也可以提升运行速度。
|
||||
self.workbook = self.app.books.open(self.strFilePath)
|
||||
except IOError:
|
||||
print("加载excel文件失败!请检查!")
|
||||
else:
|
||||
print("已成功加载excel文件(.xlsx)文件!!!\n")
|
||||
else:
|
||||
print("加载excel文件不存在!自动创建文件!")
|
||||
self.app = xw.App(visible=False, add_book=False)
|
||||
self.app.display_alerts = False # 关闭一些提示信息,可以加快运行速度。 默认为 True。
|
||||
self.app.screen_updating = False # 更新显示工作表的内容。默认为 True。关闭它也可以提升运行速度。
|
||||
self.workbook = self.app.books.add() # 创建工作簿
|
||||
|
||||
def openWorkSheet(self, strSheetName: str):
|
||||
"""
|
||||
:function: 打开工作表功能
|
||||
:param strSheetName: 要打开的工作表名称
|
||||
:return: 返回打开的工作表Worksheet对象
|
||||
"""
|
||||
worksheet = None # 工作表对象
|
||||
listSheetNames = []
|
||||
|
||||
for i in range(0, len(self.workbook.sheets)):
|
||||
listSheetNames.append(self.workbook.sheets[i].name)
|
||||
|
||||
if strSheetName in listSheetNames:
|
||||
worksheet = self.workbook.sheets[strSheetName]
|
||||
else:
|
||||
print("表单名称在当前excel文件中不存在,请检查表单名称!")
|
||||
|
||||
return worksheet
|
||||
|
||||
def addSheet(self, strSheetName: str):
|
||||
"""
|
||||
:function: 新建或添加工作表
|
||||
:param strSheetName: 工作表名称
|
||||
"""
|
||||
self.workbook.sheets.add(strSheetName)
|
||||
|
||||
def deleteSheet(self, strSheetName: str):
|
||||
"""
|
||||
:function: 删除工作表
|
||||
:param strSheetName: 工作表名称
|
||||
"""
|
||||
|
||||
if strSheetName in self.getAllSheetNames():
|
||||
self.workbook.sheets[strSheetName].delete()
|
||||
else:
|
||||
print("表单名称在当前excel文件中不存在,请检查表单名称!")
|
||||
|
||||
def getAllSheetNames(self):
|
||||
"""
|
||||
:function: 获取一个工作簿所有工作表名称
|
||||
"""
|
||||
listSheetNames = []
|
||||
|
||||
for i in range(0, len(self.workbook.sheets)):
|
||||
listSheetNames.append(self.workbook.sheets[i].name)
|
||||
|
||||
return listSheetNames
|
||||
|
||||
def getActiveSheet(self):
|
||||
"""
|
||||
:function: 获取当前活跃的sheet
|
||||
"""
|
||||
return self.workbook.sheets.active
|
||||
|
||||
def saveWorkBookData(self):
|
||||
"""
|
||||
:function: 保存工作簿数据
|
||||
"""
|
||||
self.workbook.save(self.strFilePath)
|
||||
|
||||
def saveAsWorkBookData(self, strNewFilePath: str):
|
||||
"""
|
||||
:function: 另存为工作簿数据
|
||||
:param strNewFilePath: 新的工作簿名称
|
||||
"""
|
||||
self.workbook.save(strNewFilePath)
|
||||
|
||||
def closeWorkBook(self):
|
||||
"""
|
||||
:function: 关闭工作簿
|
||||
"""
|
||||
# 关闭此工作簿
|
||||
self.workbook.close()
|
||||
# 关闭app
|
||||
self.app.quit()
|
||||
|
||||
|
||||
class myPyXwSheet:
|
||||
"""
|
||||
类功能: Python使用xlwings操作excel(.xlsx)工作表封装类myPyXwSheet
|
||||
"""
|
||||
def __init__(self, worksheet):
|
||||
# 工作表对象
|
||||
self.worksheet = worksheet
|
||||
|
||||
def insertRows(self, strRowsRange):
|
||||
"""
|
||||
:function: 在strRowRange范围上方插入多行或一行
|
||||
:param strRowsRange: 例,
|
||||
在第3行上方插入1行,即'3'
|
||||
在第3行和第4行上方插入2行,即'3:4'
|
||||
"""
|
||||
self.worksheet.api.Rows(strRowsRange).Insert()
|
||||
|
||||
def deleteRows(self, strCellsRange: str):
|
||||
"""
|
||||
:function: 删除多行或一行
|
||||
:param strCellsRange: 例,
|
||||
删除第4行和第5行,即'4:5'
|
||||
删除第4行,即'4'
|
||||
"""
|
||||
self.worksheet.range(strCellsRange).api.EntireRow.Delete()
|
||||
|
||||
def insertCols(self, strColsRange: str):
|
||||
"""
|
||||
:function: 在strColsRange范围处(左侧)插入多列或一列
|
||||
:param strColsRange: 例,
|
||||
在第C列处插入1列,即'C'
|
||||
在第C列和第D列处插入2列,即'C:D'
|
||||
"""
|
||||
self.worksheet.api.Columns(strColsRange).Insert()
|
||||
|
||||
def deleteCols(self, strColsRange):
|
||||
"""
|
||||
:function: 删除多列或一列
|
||||
:param strColsRange: 单元格范围,例,'C:D'或'C'
|
||||
"""
|
||||
self.worksheet.range(strColsRange).api.EntireColumn.Delete()
|
||||
|
||||
def getSheetName(self):
|
||||
"""
|
||||
:function: 获取工作表名称
|
||||
"""
|
||||
return self.worksheet.name
|
||||
|
||||
def getMaxRows(self):
|
||||
"""
|
||||
:function: 获取工作表最大行数
|
||||
"""
|
||||
return self.worksheet.used_range.last_cell.row
|
||||
|
||||
def getUsedRangeData(self):
|
||||
"""
|
||||
:function: 获取工作表已使用区域的全部数据
|
||||
:备注:返回的是二维列表,例,[[1,2],[3,6]
|
||||
"""
|
||||
return self.worksheet.used_range.value
|
||||
|
||||
def getMaxColumns(self):
|
||||
"""
|
||||
:function: 获取工作表最大列数
|
||||
"""
|
||||
return self.worksheet.used_range.last_cell.column
|
||||
|
||||
def renameSheet(self, strNewSheetName: str):
|
||||
"""
|
||||
:function: 重命名工作表
|
||||
:param strNewSheetName: 新的工作表名称
|
||||
"""
|
||||
self.worksheet.name = strNewSheetName
|
||||
|
||||
def clearSheetContentsAndFormat(self):
|
||||
"""
|
||||
:function: 清空表中所有内容和格式
|
||||
"""
|
||||
self.worksheet.clear()
|
||||
|
||||
def clearSheetContents(self):
|
||||
"""
|
||||
:function: 清除工作表的内容,但保留格式
|
||||
"""
|
||||
self.worksheet.clear_contents()
|
||||
|
||||
def setCellFormula(self, strCellRange: str, strFormat: str):
|
||||
"""
|
||||
:function: 设置单元格公式
|
||||
:param strCellRange: 单元格范围,例,‘C6’
|
||||
:param strFormat: 单元格数字格式,例,'=SUM(B6:B7)'
|
||||
"""
|
||||
self.worksheet[strCellRange].formula = strFormat
|
||||
|
||||
def getCellFormula(self, strCellRange: str):
|
||||
"""
|
||||
:function: 获取单元格公式
|
||||
:param strCellRange: 单元格范围,例,‘C6’
|
||||
"""
|
||||
return self.worksheet[strCellRange].formula_array
|
||||
|
||||
def setCellNumberFormat(self, strCellRange: str, strNumberFormat: str):
|
||||
"""
|
||||
:function: 设置单元格数字格式
|
||||
:param strCellRange: 单元格范围,例,‘C6’
|
||||
:param strNumberFormat: 单元格数字格式,例,'"¥"#,##0.00;-"¥"#,##0.00'
|
||||
或人民币大写:'[DBNum2][$RMB]G/通用格式;[红色][DBNum2][$RMB]G/通用格式'
|
||||
"""
|
||||
self.worksheet[strCellRange].number_format = strNumberFormat
|
||||
|
||||
def setRowHeight(self, nRowIndex: int, valRowHeight):
|
||||
"""
|
||||
:function: 设置行高
|
||||
:param nRowIndex: 行号,取值1,2...整数
|
||||
:param valRowHeight: 行高度值,0~409之间的整数或浮点数
|
||||
:return: 设置成功,返回True;否则反
|
||||
"""
|
||||
# 类型检测
|
||||
if type(nRowIndex) is not int:
|
||||
print("nRowNumber 数据类型错误!请确认为整数类型")
|
||||
return False
|
||||
|
||||
if type(valRowHeight) is not int and type(valRowHeight) is not float:
|
||||
print("cur_num 数据类型错误!请确认为整数类型,或者为浮点数类型!")
|
||||
return False
|
||||
|
||||
# 数字是否出范围
|
||||
if 0 <= valRowHeight <= 409:
|
||||
self.worksheet.range('A' + str(nRowIndex)).row_height = valRowHeight
|
||||
return True
|
||||
else:
|
||||
print("行高度值,超出了目前最大行高度!")
|
||||
return False
|
||||
|
||||
def setColumnWidth(self, strColumnIndex: str, valColumnWidth):
|
||||
"""
|
||||
:function: 设置列宽
|
||||
:param strColumnIndex: 列索引,取值整数1,2...或者'A','B'...
|
||||
:param valColumnWidth: 列宽度值,0~255之间的整数或浮点数
|
||||
:return: 设置成功,返回True;否则反
|
||||
"""
|
||||
# 数字检测
|
||||
if type(strColumnIndex) is int:
|
||||
try:
|
||||
strColumnIndex = get_column_letter(strColumnIndex) # 列号转列字母
|
||||
except ValueError:
|
||||
print("ColumnIndex参数非法!请确认是输入数据为列索引字符串!")
|
||||
return False
|
||||
|
||||
if type(valColumnWidth) is not int and type(valColumnWidth) is not float:
|
||||
print("valColumnWidth 数据类型错误!请确认为整数类型,或者为浮点数类型!")
|
||||
return False
|
||||
|
||||
# 数字是否出范围
|
||||
if 0 <= valColumnWidth <= 255:
|
||||
self.worksheet.range(strColumnIndex + '1').column_width = valColumnWidth
|
||||
return True
|
||||
else:
|
||||
print("列宽度值,超出了目前最大列宽度!")
|
||||
return False
|
||||
|
||||
def setRowsHeight(self, strCellsRange: str, valRowHeight):
|
||||
"""
|
||||
:function: 设置多行行高
|
||||
:param strCellsRange: 例,单元格'A2:D5'或'A2'
|
||||
:param valRowHeight: 行高度值,0~409之间的整数或浮点数
|
||||
:return: 设置成功,返回True;否则反
|
||||
"""
|
||||
if type(valRowHeight) is not int and type(valRowHeight) is not float:
|
||||
print("cur_num 数据类型错误!请确认为整数类型,或者为浮点数类型!")
|
||||
return False
|
||||
|
||||
# 数字是否出范围
|
||||
if 0 <= valRowHeight <= 409:
|
||||
self.worksheet.range(strCellsRange).row_height = valRowHeight
|
||||
return True
|
||||
else:
|
||||
print("行高度值,超出了目前最大行高度!")
|
||||
return False
|
||||
|
||||
def setColumnsWidth(self, strCellsRange: str, valColumnWidth):
|
||||
"""
|
||||
:function: 设置多列列宽
|
||||
:param strCellsRange: 例,单元格'A2:D5'或'A2'
|
||||
:param valColumnWidth: 列宽度值,0~255之间的整数或浮点数
|
||||
:return: 设置成功,返回True;否则反
|
||||
"""
|
||||
if type(valColumnWidth) is not int and type(valColumnWidth) is not float:
|
||||
print("valColumnWidth 数据类型错误!请确认为整数类型,或者为浮点数类型!")
|
||||
return False
|
||||
|
||||
# 数字是否出范围
|
||||
if 0 <= valColumnWidth <= 255:
|
||||
self.worksheet.range(strCellsRange).column_width = valColumnWidth
|
||||
return True
|
||||
else:
|
||||
print("列宽度值,超出了目前最大列宽度!")
|
||||
return False
|
||||
|
||||
def getCellRowIndex(self, strCellRange: str):
|
||||
"""
|
||||
:function: 获取单元格的行标
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:return: 返回单元格的行标
|
||||
"""
|
||||
return self.worksheet.range(strCellRange).row
|
||||
|
||||
def getCellColIndex(self, strCellRange: str):
|
||||
"""
|
||||
:function: 获取单元格的列标
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:return: 返回单元格的列标
|
||||
"""
|
||||
return self.worksheet.range(strCellRange).column
|
||||
|
||||
def putDataByCell(self, strCellRange: str, Value):
|
||||
"""
|
||||
:function: 写入一个单元格数据功能
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:param Value: 写入指定单元格的数值
|
||||
"""
|
||||
self.worksheet.range(strCellRange).value = Value
|
||||
|
||||
def getDataByCell(self, strCellRange: str):
|
||||
"""
|
||||
:function: 读取一个单元格数据功能
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:备注:返回的数值默认是浮点数
|
||||
"""
|
||||
return self.worksheet.range(strCellRange).value
|
||||
|
||||
def putDataByRowOrCol(self, strCellsRange: str, listData: list):
|
||||
"""
|
||||
:function:写入一行或一列数据
|
||||
:param strCellsRange: 例,单元格范围,一行'A2:D2'或一列'A2:A4'或单元格'A2'
|
||||
:param listData: 一维数据列表,[]
|
||||
"""
|
||||
index = 0
|
||||
|
||||
for row in self.worksheet[strCellsRange]:
|
||||
for cell in row:
|
||||
if index >= len(listData):
|
||||
print("一维列表下表超出范围!")
|
||||
else:
|
||||
cell.value = listData[index]
|
||||
index += 1
|
||||
|
||||
def getDataByRowOrCol(self, strCellsRange: str) -> list:
|
||||
"""
|
||||
:function:读取一行或一列数据
|
||||
:param strCellsRange: 例,单元格范围,一行'A2:D2'或一列'A2:A4'或单元格'A2'
|
||||
:return:一维列表[],返回的数值默认是浮点数
|
||||
"""
|
||||
return self.worksheet.range(strCellsRange).value
|
||||
|
||||
def putDataByRow(self, strCellRange: str, listData: list):
|
||||
"""
|
||||
:function:以第一个单元格为起点,写入一行数据
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:param listData: 一维数据列表,[]
|
||||
:备注:
|
||||
# 将列表[1,2,3]储存在A1:C1中
|
||||
self.worksheet.range('A1').value=[1,2,3]
|
||||
# 等同于
|
||||
self.worksheet.range('A1:C1').value = [1,2,3]
|
||||
|
||||
"""
|
||||
self.worksheet.range(strCellRange).value = listData
|
||||
|
||||
def putDataByCol(self, strCellRange: str, listData: list):
|
||||
"""
|
||||
:function:以第一个单元格为起点,写入一列数据
|
||||
:param strCellRange: 例,单元格'A2'
|
||||
:param listData: 一维数据列表,[]
|
||||
:备注:
|
||||
# 将列表[1,2,3]储存在A1:A3中
|
||||
self.worksheet.range('A1').options(transpose=True).value=[1,2,3]
|
||||
"""
|
||||
self.worksheet.range(strCellRange).options(transpose=True).value = listData
|
||||
|
||||
def putDataByRows(self, strCellsRange: str, listData: list):
|
||||
"""
|
||||
:function:以第一个单元格为起点,多行输入就要用二维列表
|
||||
:param strCellsRange: 例,单元格'A2'
|
||||
:param listData: 二维数据列表,例,[[1,2],[3,4]]
|
||||
:备注:
|
||||
# 将2x2表格,即二维数组,储存在A1:B2中,如第一行1,2,第二行3,4
|
||||
self.worksheet.range('A1').options(expand='table').value=[[1,2],[3,4]]
|
||||
"""
|
||||
self.worksheet.range(strCellsRange).options(expand='table').value = listData
|
||||
|
||||
def getDataByRows(self, strCellsRange: str) -> list:
|
||||
"""
|
||||
:function:读取多行数据就要用二维列表
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
:return listData: 二维数据列表,例,[[1,2],[3,4]]
|
||||
:备注:返回的数值默认是浮点数
|
||||
"""
|
||||
return self.worksheet.range(strCellsRange).value
|
||||
|
||||
def getCellRowHeight(self, nRowIndex: int):
|
||||
"""
|
||||
:function:获取单元格行高度
|
||||
:param nRowIndex: 例,1, 2, 3,...
|
||||
:return: 返回单元格行高度
|
||||
"""
|
||||
return self.worksheet.range('A' + str(nRowIndex)).row_height
|
||||
|
||||
def getCellColWidth(self, strColumnIndex: str):
|
||||
"""
|
||||
:function:获取单元格列宽度
|
||||
:param strColumnIndex: 例,'A','B',...
|
||||
:return: 返回单元格列宽度
|
||||
"""
|
||||
fColWidth = self.worksheet.range(strColumnIndex + '1').column_width
|
||||
# 浮点数保留2位小数
|
||||
fNewColWidth = round(fColWidth, 2)
|
||||
return fNewColWidth
|
||||
|
||||
def setRowsAutofit(self, strCellsRange: str):
|
||||
"""
|
||||
:function:单元格范围的所有行宽度自适应
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
"""
|
||||
# 所有行宽度自适应
|
||||
self.worksheet.range(strCellsRange).rows.autofit()
|
||||
|
||||
def setColAutofit(self, strCellsRange: str):
|
||||
"""
|
||||
:function:单元格范围的所有列宽度自适应
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
"""
|
||||
# 所有行宽度自适应
|
||||
self.worksheet.range(strCellsRange).columns.autofit()
|
||||
|
||||
def setAutofit(self, strCellsRange: str):
|
||||
"""
|
||||
:function:单元格范围的所有行和列的宽度自适应
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
"""
|
||||
# 所有行宽度自适应
|
||||
self.worksheet.range(strCellsRange).autofit()
|
||||
|
||||
def getRangeRowsCount(self, strCellsRange: str):
|
||||
"""
|
||||
:function:获取单元格范围range的总行数
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
:return : 单元格范围range的总行数
|
||||
"""
|
||||
return self.worksheet.range(strCellsRange).rows.count
|
||||
|
||||
def getRangeColsCount(self, strCellsRange: str):
|
||||
"""
|
||||
:function:获取单元格范围range的总列数
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
:return : 单元格范围range的总列数
|
||||
"""
|
||||
return self.worksheet.range(strCellsRange).columns.count
|
||||
|
||||
def getRangeRows(self, strCellsRange: str) -> list:
|
||||
"""
|
||||
:function:获取单元格范围range的行对象列表
|
||||
:param strCellsRange: 例,单元格范围,'A2:C6'
|
||||
:return : 单元格范围range的行对象列表
|
||||
"""
|
||||
# 返回第一行数据列表
|
||||
# return self.worksheet.range(strCellsRange).rows[0].value
|
||||
# 返回单元格范围range的行对象列表
|
||||
return self.worksheet.range(strCellsRange).rows
|
||||
|
||||
def copyRange(self, strSourceCellsRange: str):
|
||||
"""
|
||||
:function:同一个工作表或不同工作表,把一个区域拷贝到剪贴板
|
||||
:param strSourceCellsRange: 例,源单元格范围,'A1:B3'
|
||||
"""
|
||||
# 参数:destination 设置目标区域,如果省略,rng区域会被拷贝到剪贴板上
|
||||
self.worksheet.range(strSourceCellsRange).copy(destination=None)
|
||||
|
||||
def pasteRange(self, strDestCellsRange: str, paste=None, operation=None, skip_blanks=False, transpose=False):
|
||||
"""
|
||||
:function:同一个工作表或不同工作表,把剪贴板拷贝到目的区域
|
||||
:param strDestCellsRange: 例,目的单元格范围,'D5'或'D5:E7'
|
||||
:param paste:
|
||||
参数:
|
||||
【paste】
|
||||
all_merging_conditional_formats(将粘贴所有内容,并且将合并条件格式)
|
||||
all(粘贴全部内容)
|
||||
all_except_borders(粘贴除边框外的全部内容)
|
||||
all_using_source_theme(使用源主题粘贴全部内容)
|
||||
column_widths(粘贴复制的列宽)
|
||||
comments(粘贴批注)
|
||||
formats(粘贴复制的源格式)
|
||||
formulas(粘贴公式)
|
||||
formulas_and_number_formats(粘贴公式和数字格式)
|
||||
validation(粘贴有效性)
|
||||
values(粘贴值)
|
||||
values_and_number_formats(粘贴值和数字格式)
|
||||
:param operation:
|
||||
参数:
|
||||
【operation】
|
||||
add(加)
|
||||
divide(除)
|
||||
multiply(乘)
|
||||
subtract(减)
|
||||
:param skip_blanks:
|
||||
参数:
|
||||
【skip_blanks】
|
||||
(bool, default False),设为 True 时忽略空白单元格
|
||||
:param transpose:
|
||||
参数:
|
||||
【transpose】
|
||||
(bool, default False),设为 True 时对行列转置
|
||||
"""
|
||||
# 将剪贴板里内容粘贴到指定区域
|
||||
self.worksheet.range(strDestCellsRange).paste(paste=paste, operation=operation,
|
||||
skip_blanks=skip_blanks, transpose=transpose)
|
||||
|
||||
def copyPasteRange(self, strSourceCellsRange: str, strDestCellsRange: str):
|
||||
"""
|
||||
:function:同一个工作表内,把一个区域复制粘贴到目的区域
|
||||
:param strSourceCellsRange: 例,源单元格范围,'A1:B3'
|
||||
:param strDestCellsRange: 例,目的单元格范围,'D5'或'D5:E7'
|
||||
"""
|
||||
'''同个表格复制、粘贴'''
|
||||
# 复制 A1 到 B3 之间单元格的值,粘贴到'D5'中
|
||||
self.worksheet.range(strSourceCellsRange).api.Copy(self.worksheet.range(strDestCellsRange).api)
|
||||
|
||||
def setCellMerge(self, strCellsRange: str):
|
||||
"""
|
||||
:function: 合并单元格
|
||||
:param strCellsRange: 例,单元格范围,'A1:D1'
|
||||
"""
|
||||
self.worksheet.range(strCellsRange).merge(across=False)
|
||||
|
||||
def setCellUnMerge(self, strCellsRange: str):
|
||||
"""
|
||||
:function: 拆分单元格
|
||||
:param strCellsRange: 例,单元格范围,'A1:D1'
|
||||
"""
|
||||
self.worksheet.range(strCellsRange).unmerge()
|
||||
|
||||
def setFontStyleByCells(self, strCellsRange: str, name="宋体", size=11,
|
||||
bold=False, italic=False, color=1):
|
||||
"""
|
||||
:function:设置一个或多个单元格的字体样式风格
|
||||
配置字体格式为:样式(宋体)、尺寸(11)、粗体(flase)、斜体(flase)、颜色(黑色)
|
||||
这里可以根据自己需求修改
|
||||
:param strCellsRange: 例,单元格范围'A29:F39'或单元格'A2'
|
||||
:param name: 字体名称
|
||||
:param size: 字体大小点数
|
||||
:param bold: True表示粗体
|
||||
:param italic: True表示斜体
|
||||
:param color: 字体颜色
|
||||
===字体颜色索引===
|
||||
无色 = -4142,自动 = -4105,黑色 = 1,
|
||||
白色 = 2 , 红色 = 3,鲜绿 = 4,
|
||||
蓝色 = 5 ,黄色 = 6,粉红 = 7,
|
||||
青绿 = 8 ,深红 = 9,绿色 = 10,
|
||||
深蓝 = 11,深黄 = 12 ,紫罗兰 = 13,
|
||||
青色 = 14,灰色25 = 15,褐色 = 53,
|
||||
橄榄 = 52,深绿 = 51,深青 = 49,
|
||||
靛蓝 = 55,灰色80 = 56,橙色 = 46,
|
||||
蓝灰 = 47,灰色50 = 16,浅橙色 = 45,
|
||||
酸橙色 = 43,海绿 = 50, 水绿色 = 42,
|
||||
浅蓝 = 41, 灰色40 = 48,金色 = 44,
|
||||
天蓝 = 33,梅红 = 54,玫瑰红 = 38,
|
||||
茶色 = 40,浅黄 = 36,浅绿 = 35,
|
||||
浅青绿 = 34,淡蓝 = 37,淡紫 = 39,
|
||||
"""
|
||||
cellRange = self.worksheet.range(strCellsRange)
|
||||
cellRange.api.Font.Name = name # 设置字体名称
|
||||
cellRange.api.Font.Size = size # 设置字体大小
|
||||
cellRange.api.Font.Bold = bold # 设置字体是否加粗
|
||||
cellRange.api.Font.Italic = italic # 设置字体是否斜体
|
||||
cellRange.api.Font.ColorIndex = color # 设置字体颜色
|
||||
|
||||
@staticmethod
|
||||
def strHorizontalAlignment_to_int(strHorizontalAlignment: str):
|
||||
"""
|
||||
:function:水平对齐方式字符串转int
|
||||
:param strHorizontalAlignment: 水平对齐方式字符串,'left'、'center'、'right'
|
||||
"""
|
||||
numbers = {
|
||||
'left': -4131,
|
||||
'center': -4108,
|
||||
'right': -4152
|
||||
}
|
||||
|
||||
return numbers.get(strHorizontalAlignment, None)
|
||||
|
||||
@staticmethod
|
||||
def strVerticalAlignment_to_int(strVerticalAlignment: str):
|
||||
"""
|
||||
:function:垂直对齐方式字符串转int
|
||||
:param strVerticalAlignment: 垂直对齐方式字符串,'top'、'center'、'bottom'、'wordwrap'
|
||||
"""
|
||||
numbers = {
|
||||
'top': -4160,
|
||||
'center': -4108,
|
||||
'bottom': -4107,
|
||||
'wordwrap': -4130
|
||||
}
|
||||
|
||||
return numbers.get(strVerticalAlignment, None)
|
||||
|
||||
def setAlignmentStyleByCells(self, strCellsRange: str, horizontal='center', vertical='center'):
|
||||
"""
|
||||
:function:设置一个或多个单元格的对齐方式
|
||||
:param strCellsRange: 例,单元格范围'A29:F39'或单元格'A2'
|
||||
:param horizontal: 水平对齐方式,'left'、'center'、'right'
|
||||
:param vertical: 垂直对齐方式,'top'、'center'、'bottom'、'wordwrap'
|
||||
"""
|
||||
cellRange = self.worksheet.range(strCellsRange)
|
||||
# -4108 水平居中。 -4131 靠左,-4152 靠右。
|
||||
cellRange.api.HorizontalAlignment = self.strHorizontalAlignment_to_int(horizontal)
|
||||
# -4108 垂直居中(默认)。 -4160 靠上,-4107 靠下, -4130 自动换行对齐。
|
||||
cellRange.api.VerticalAlignment = self.strVerticalAlignment_to_int(vertical)
|
||||
|
||||
def setBorderStyleByCell(self, strCellsRange: str, nBorderLineStyle: int = 1, nBorderWeight: int = 2):
|
||||
"""
|
||||
:function:设置一个或多个单元格的边框样式
|
||||
:param strCellsRange: 例,单元格范围'A29:F39'或单元格'A2'
|
||||
:param nBorderLineStyle: 【边框线型】,例,直线:1;虚线:2;点划线:4;双点划线:5
|
||||
:param nBorderWeight: 【边框粗细】,例,默认值2
|
||||
"""
|
||||
cellRange = self.worksheet.range(strCellsRange)
|
||||
|
||||
# Borders(7) 左边框
|
||||
cellRange.api.Borders(7).LineStyle = nBorderLineStyle # 设置边框线型
|
||||
cellRange.api.Borders(7).Weight = nBorderWeight # 设置边框粗细。
|
||||
|
||||
# Borders(8) 顶部框
|
||||
cellRange.api.Borders(8).LineStyle = nBorderLineStyle
|
||||
cellRange.api.Borders(8).Weight = nBorderWeight
|
||||
|
||||
# Borders(9) 底部边框
|
||||
cellRange.api.Borders(9).LineStyle = nBorderLineStyle
|
||||
cellRange.api.Borders(9).Weight = nBorderWeight
|
||||
|
||||
# Borders(10) 右边框
|
||||
cellRange.api.Borders(10).LineStyle = nBorderLineStyle
|
||||
cellRange.api.Borders(10).Weight = nBorderWeight
|
||||
|
||||
"""如果是一个区域的单元格,内部边框设置如下"""
|
||||
# Borders(11) 内部垂直边线
|
||||
cellRange.api.Borders(11).LineStyle = nBorderLineStyle
|
||||
cellRange.api.Borders(11).Weight = nBorderWeight
|
||||
|
||||
# Borders(12) 内部水平边线
|
||||
cellRange.api.Borders(12).LineStyle = nBorderLineStyle
|
||||
cellRange.api.Borders(12).Weight = nBorderWeight
|
||||
|
||||
|
||||
"""
|
||||
===============================================================
|
||||
主函数
|
||||
===============================================================
|
||||
"""
|
||||
if __name__ == '__main__':
|
||||
# excel文件绝对路径
|
||||
xlsxFilePath = os.path.join(os.getcwd() + "\\" + "someip.xlsx")
|
||||
|
||||
# 创建工作簿对象
|
||||
myXwBook = myPyXwBook(xlsxFilePath)
|
||||
# 创建工作表对象
|
||||
myXwSheet1 = myPyXwSheet(myXwBook.openWorkSheet("ServiceInterfaces"))
|
||||
|
||||
# 写入数据
|
||||
# myXwSheet1.putDataByRowOrCol('A1:A4', [1, 2, 3, 4])
|
||||
|
||||
# 读数据
|
||||
readData = myXwSheet1.getDataByCell('A2:C2')
|
||||
|
||||
print(readData)
|
||||
|
||||
# 保存工作簿数据
|
||||
myXwBook.saveWorkBookData()
|
||||
# 关闭工作簿
|
||||
myXwBook.closeWorkBook()
|
||||
|
||||
|
||||
54
code_generator/someip_app/sample_cfg_ids.hpp.template
Normal file
54
code_generator/someip_app/sample_cfg_ids.hpp.template
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
|
||||
/// @author liuzhimin@huaxu.com
|
||||
|
||||
/// @generate date: ${GENE_DATE}
|
||||
|
||||
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
|
||||
|
||||
#ifndef VSOMEIP_${APP_NAME}_CFG_IDS_HPP
|
||||
#define VSOMEIP_${APP_NAME}_CFG_IDS_HPP
|
||||
|
||||
#define VSOMEIP_CONFIGURATION "${RUN_CONFIG_FILENAME}"
|
||||
#define APP_NAME "${APP_NAME}"
|
||||
|
||||
#define ${APP_NAME}_SERVICE_ID ${SERVICE_ID}
|
||||
#define ${APP_NAME}_INSTANCE_ID ${INSTANCE_ID}
|
||||
|
||||
#define USE_TCP ${USE_TCP}
|
||||
|
||||
#define OFFER_F_F_METHOD ${OFFER_F_F_METHOD}
|
||||
#define OFFER_R_R_METHOD ${OFFER_R_R_METHOD}
|
||||
#define OFFER_SETTER ${OFFER_SETTER}
|
||||
#define OFFER_GETTER ${OFFER_GETTER}
|
||||
#define OFFER_EVENT ${OFFER_EVENT}
|
||||
#define OFFER_FIELD_EVENT ${OFFER_FIELD_EVENT}
|
||||
|
||||
#if OFFER_F_F_METHOD
|
||||
#define ${APP_NAME}_${FF_METHOD_NAME}_F_F_METHOD_ID 0x0100 //0x0101,0x0102...
|
||||
#endif
|
||||
|
||||
#if OFFER_R_R_METHOD
|
||||
#define ${APP_NAME}_${RR_METHOD_NAME}_R_R_METHOD_ID 0x0200 //0x0201,0x0202...
|
||||
#endif
|
||||
|
||||
#if OFFER_GETTER
|
||||
#define ${APP_NAME}_${GET_METHOD_NAME}_GET_METHOD_ID 0x0300 //0x0301,0x0302...
|
||||
#endif
|
||||
|
||||
#if OFFER_SETTER
|
||||
#define ${APP_NAME}_${SET_METHOD_NAME}_SET_METHOD_ID 0x0400 //0x0401,0x0402...
|
||||
#endif
|
||||
|
||||
#if OFFER_EVENT
|
||||
#define ${APP_NAME}_${EVENTGROUP_NAME}_EVENTGROUP_ID 0x8100 //0x8200,0x8300...
|
||||
#define ${APP_NAME}_${EVENT_NAME}_EVENT_ID 0x8101 //0x8102,0x8103...
|
||||
#endif
|
||||
|
||||
#if OFFER_FIELD_EVENT
|
||||
#define ${APP_NAME}_${FIELD_EVENTGROUP_NAME}_FIELD_EVENTGROUP_ID 0x8800 //0x8900,0x8a00...
|
||||
#define ${APP_NAME}_${FIELD_EVENT_NAME}_FIELD_EVENT_ID 0x8801 //0x8802,0x8803...
|
||||
#endif
|
||||
|
||||
#endif // VSOMEIP_EXAMPLES_SAMPLE_CFG_IDS_HPP
|
||||
|
||||
BIN
code_generator/someip_app/someip.xlsx
Normal file
BIN
code_generator/someip_app/someip.xlsx
Normal file
Binary file not shown.
34
code_generator/someip_app/template.someip_ids.hpp
Normal file
34
code_generator/someip_app/template.someip_ids.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (${YEAR}).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_ids.hpp
|
||||
* @brief head file of someip ids
|
||||
* @author ${AUTHOR}
|
||||
* @date ${TIME}
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 ${DATE} ${AUTHOR} First version by auto generator
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef AG_VSOMEIP_IDS_HPP
|
||||
#define AG_VSOMEIP_IDS_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
${ALL_IDS}
|
||||
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
|
||||
#endif // AG_VSOMEIP_IDS_HPP
|
||||
109
code_generator/someip_app/template.someip_service.cpp
Normal file
109
code_generator/someip_app/template.someip_service.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (${YEAR}).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_service.cpp
|
||||
* @brief main entry of someip_service
|
||||
* @author ${AUTHOR}
|
||||
* @date ${TIME}
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 ${DATE} ${AUTHOR} First version by auto generator
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include "someip_ids.hpp"
|
||||
#include "someip_type.hpp"
|
||||
#include "someip_service.hpp"
|
||||
#include <list>
|
||||
|
||||
using namespace vsomeip;
|
||||
/*=======[G L O B A L D A T A]==============================================*/
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*=======[I N T E R N A L D A T A]==========================================*/
|
||||
|
||||
/*=======[L O C A L F U N C T I O N D E C L A R A T I O N S]==============*/
|
||||
|
||||
${ALL_FUNCS_DECLARATIONS}
|
||||
|
||||
/*=======[L O C A L F U N C T I O N I M P L E M E N T A T I O N S]========*/
|
||||
|
||||
${ALL_FUNCS_IMPLEMENTATIONS}
|
||||
|
||||
/*=======[G L O B A L F U N C T I O N I M P L E M E N T A T I O N S]======*/
|
||||
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
service *server_ptr(nullptr);
|
||||
void handle_signal(int _signal)
|
||||
{
|
||||
if (server_ptr != nullptr &&
|
||||
(_signal == SIGINT || _signal == SIGTERM
|
||||
|| _signal == SIGKILL))
|
||||
server_ptr->stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
service server;
|
||||
|
||||
std::vector<offer_method_t> offer_methods;
|
||||
std::vector<offer_event_t> offer_events;
|
||||
std::vector<offer_service_t> my_services;
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
server_ptr = &server;
|
||||
signal(SIGINT, handle_signal);
|
||||
signal(SIGTERM, handle_signal);
|
||||
signal(SIGKILL, handle_signal);
|
||||
#endif
|
||||
offer_methods.clear();
|
||||
${ALL_OFFER_METHODS}
|
||||
|
||||
offer_events.clear();
|
||||
${ALL_OFFER_EVENTS}
|
||||
|
||||
my_services.clear();
|
||||
${ALL_OFFER_SERVICES}
|
||||
|
||||
if (server.init())
|
||||
{
|
||||
for (auto const&service: my_services){
|
||||
server.start_offer_service(service._service, service._instance,
|
||||
service._major, service._minor);
|
||||
}
|
||||
|
||||
for (auto const&method: offer_methods){
|
||||
server.start_offer_method(method._service,method._instance,method._method,method._get_response_data_func);
|
||||
}
|
||||
|
||||
for (auto const&event: offer_events){
|
||||
server.start_offer_event(event._service, event._instance, event._eventgroup,
|
||||
event._event, event._type, event._cycle, event._change_resets_cycle,
|
||||
event._update_on_change, event._epsilon_change_func,
|
||||
event._reliability);
|
||||
}
|
||||
|
||||
server.start(); //block function
|
||||
|
||||
// stop:
|
||||
for (auto const&service: my_services){
|
||||
server.stop_offer_service(service._service, service._instance,
|
||||
service._major, service._minor);
|
||||
}
|
||||
server.stop();
|
||||
return 0;
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
158
code_generator/someip_app/template.someip_type.hpp
Normal file
158
code_generator/someip_app/template.someip_type.hpp
Normal file
@@ -0,0 +1,158 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2022).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_type.hpp
|
||||
* @brief head file of someip type defines
|
||||
* @author LiuZhimin
|
||||
* @date 2023-03-15 15:15:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20230315 Liuzhimin Initial Version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef AG_VSOMEIP_TYPE_HPP
|
||||
#define AG_VSOMEIP_TYPE_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
${ALL_TYPES}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float Accel_x; //加速度计: 惯导X轴
|
||||
float Accel_y; //加速度计: 惯导Y轴
|
||||
float Accel_z; //加速度计: 惯导Z轴
|
||||
float Gyro_x; //陀螺: 惯导X轴
|
||||
float Gyro_y; //陀螺: 惯导Y轴
|
||||
float Gyro_z; //陀螺: 惯导Z轴
|
||||
bool IMU_Vaild; // IMU信号组有效性标志位
|
||||
bool IMU_Status; // IMU故障标志位
|
||||
float Acc_Bias_X; //加速度计x轴零偏
|
||||
float Acc_Bias_Y; //加速度计y轴零偏
|
||||
float Acc_Bias_Z; //加速度计z轴零偏
|
||||
float Gyro_Bias_X; //陀螺仪x轴零偏
|
||||
float Gyro_Bias_Y; //陀螺仪x轴零偏
|
||||
float Gyro_Bias_Z; //陀螺仪x轴零偏
|
||||
bool Bias_Valid; //零偏信号组有效性标志位
|
||||
uint32_t IMU_Tow; // GPS周内秒(ms),起点每周日的0时0点0分0秒,未同步恒为0
|
||||
uint16_t IMU_Week; // GPS周,起点为1980年1月6日0时0分0秒,未同步恒为0
|
||||
uint64_t IMU_SyncTime; //时间同步后系统unix时间戳,未同步恒为0
|
||||
uint8_t IMU_SyncStatus; //时间同步状态
|
||||
} imu_info_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float INS_GndSpd; // INS地向速度
|
||||
float INS_GndSpdStd; // INS地向速度标准差
|
||||
float INS_EastSpd; // INS东向速度
|
||||
float INS_EastSpdStd; // INS东向速度标准差
|
||||
float INS_NorthSpd; // INS北向速度
|
||||
float INS_NorthSpdStd; // INS北向速度标准差
|
||||
float INS_LocatingHght; // INS高度
|
||||
float INS_HghtStd; // INS高度标准差
|
||||
double INS_Longd; // INS经度
|
||||
float INS_LongdStd; // INS经度标准差
|
||||
double INS_Latd; // INS纬度
|
||||
float INS_LatdStd; // INS纬度标准差
|
||||
float INS_Yaw; // INS航向角
|
||||
float INS_YawStd; // INS航向角标准差
|
||||
float INS_Pitch; // INS俯仰角
|
||||
float INS_PitchStd; // INS俯仰角标准差
|
||||
float INS_Roll; // INS横滚角
|
||||
float INS_RollStd; // INS横滚角标准差
|
||||
bool INS_Valid; // INS_data信号组有效性标志位
|
||||
uint8_t INS_DR_Confidence; // INS DR推算数据置信度
|
||||
uint32_t INS_Tow; // GPS周内秒(ms),起点每周日的0时0点0分0秒,未同步恒为0
|
||||
uint16_t INS_Week; // GPS周,起点为1980年1月6日0时0分0秒,未同步恒为0
|
||||
uint64_t INS_SyncTime; //时间同步后系统时间戳,未同步恒为0
|
||||
uint8_t IMU_SyncStatus; //时间同步状态
|
||||
} ins_info_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bool INS_PosIniSta; // INS位置初始化标志位
|
||||
bool INS_VelIniSta; // INS速度初始化标志位
|
||||
bool INS_HeadingIniSta; // INS航向初始化标志位
|
||||
bool INS_AttitudeIniSta; // INS姿态初始化标志位
|
||||
bool INS_INSIniSta; // INS总体初始化标志位
|
||||
bool INS_whlspdfusion_Status; // 轮速融合状态标志位
|
||||
uint16_t INS_Status; // INS故障标志位
|
||||
} ins_status_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t GNSS_NumSV; // GNSS收星数
|
||||
uint8_t GnssFlag_Pos; // GNSS定位解状态
|
||||
uint8_t GNSSRTKDly_HDM; // RTCM差分延迟
|
||||
double GNSS_Latitude; // GNSS纬度
|
||||
double GNSS_Longtitude; // GNSS经度
|
||||
float GNSS_LocatHeight; // GNSS海拔高
|
||||
float GNSS_Heading; // GNSS航向角
|
||||
float GNSS_Pitch; // GNSS俯仰角
|
||||
float GNSS_HorizontalSpd; // GNSS水平速度
|
||||
float GNSS_VerticalSpd; // GNSS垂直速度
|
||||
float GNSS_TrackTrue; // GNSS航迹角
|
||||
float GNSS_Std_Lat; // GNSS纬度_标准差
|
||||
float GNSS_Std_Lon; // GNSS经度_标准差
|
||||
float GNSS_Std_LocatHeight; // GNSS高度_标准差
|
||||
float GNSS_Std_Heading; // GNSS航向角_标准差
|
||||
float GNSS_Std_Pitch; // GNSS俯仰角_标准差
|
||||
float GNSS_Std_HorizontalSpd; // GNSS水平速度_标准差
|
||||
float GNSS_Std_VerticalSpd; // GNSS垂直速度_标准差
|
||||
float GNSS_Std_TrackTrue; // GNSS航迹角_标准差
|
||||
uint16_t GNSS_GDOP; // 几何精度因子
|
||||
uint16_t GNSS_PDOP; // 位置精度因子
|
||||
uint16_t GNSS_HDOP; // 水平精度因子
|
||||
uint16_t GNSS_HTDOP; // 水平和时间精度因子
|
||||
uint8_t SSR_Integrity; // SSR修正数据完好性
|
||||
bool GNSS_Valid; // GNSS_data信号组有效性标志位
|
||||
uint16_t GNSS_Week; // GPS周,起点为1980年1月6日0时0分0秒,未同步恒为0
|
||||
uint32_t GNSS_Tow; // GPS周内秒(ms),起点每周日的0时0点0分0秒,未同步恒为0
|
||||
uint64_t GNSS_SyncTime; // 时间同步后系统时间戳,未同步恒为0
|
||||
uint8_t IMU_SyncStatus; // 时间同步状态
|
||||
} gnss_info_t;
|
||||
|
||||
typedef std::string partNumber_id_t; //XX零件号
|
||||
typedef std::string software_id_t; //XX电子控制单元软件版本号
|
||||
typedef std::string software_id_supplier_t; //供应商电子控制单元软件版本号
|
||||
|
||||
typedef struct
|
||||
{
|
||||
partNumber_id_t partNumber_id;
|
||||
software_id_t software_id;
|
||||
software_id_supplier_t software_id_supplier;
|
||||
} part_info_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float Extrinsic_Roll; // 横滚角误差估计值
|
||||
float Extrinsic_Pitch; // 俯仰角误差估计值
|
||||
float Extrinsic_Yaw; // 偏航角误差估计值
|
||||
bool Extrinsic_Valid; // ExtParameter信号组有效性标志位
|
||||
uint32_t Extrinsic_Tow; // GPS周内秒(ms),起点每周日的0时0点0分0秒,未同步恒为0
|
||||
uint16_t Extrinsic_Week; // GPS周,起点为1980年1月6日0时0分0秒,未同步恒为0
|
||||
uint64_t Extrinsic_SyncTime; // 时间同步后系统时间戳,未同步恒为0
|
||||
uint8_t Extrinsic_SyncStatus; // 时间同步状态
|
||||
} ext_parameter_t;
|
||||
|
||||
typedef std::string gngga_info_t; //GNGGA定位数据
|
||||
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
|
||||
#endif // AG_VSOMEIP_TYPE_HPP
|
||||
62
code_generator/someip_app/tmp.template
Normal file
62
code_generator/someip_app/tmp.template
Normal file
@@ -0,0 +1,62 @@
|
||||
##########################ids##############################
|
||||
## ${ALL_IDS}
|
||||
// ${SERVICE DESCRIPTION}
|
||||
#define ${SERVICE_NAME} ${SERVICE_ID} // Service ID
|
||||
#define ${INSTANCE_NAME} ${INSTANCE_ID} // Instance ID
|
||||
${METHODS}
|
||||
${EVENT_GROUPS}
|
||||
|
||||
##${METHODS}
|
||||
#define ${METHOD_NAME} ${METHOD_ID} // ${METHOD_TYPE}
|
||||
|
||||
##${EVENT_GROUPS}
|
||||
#define ${EVENTGROUP_NAME} ${EVENTGROUP_ID} // Event Group
|
||||
${EVENTS}
|
||||
|
||||
##${EVENTS}
|
||||
#define ${EVENT_NAME} ${EVENT_ID} // ${EVENT_TYPE}
|
||||
|
||||
|
||||
###########################types#############################
|
||||
## ${ALL_TYPES}
|
||||
// ${TYPE_DISCRIPTION}
|
||||
typedef ${CONSTRUCTION_TYPE} ${ALL_MEMBERS} ${DEFINE_TYPE};
|
||||
|
||||
## ${ALL_MEMBERS}
|
||||
if ${CONSTRUCTION_TYPE} == <BASE TYPE>
|
||||
${ALL_MEMBERS} == ''
|
||||
else if ${CONSTRUCTION_TYPE} == enum
|
||||
${ALL_MEMBERS} == """
|
||||
{
|
||||
${ENUM_MEMBERS}, // ${COMMENT}
|
||||
}"""
|
||||
else if ${CONSTRUCTION_TYPE} == struct/union/<FROM COLUMN A>
|
||||
${ALL_MEMBERS} == """
|
||||
{
|
||||
${CONSTRUCTION_MEMBERS}; // ${COMMENT}
|
||||
}"""
|
||||
|
||||
|
||||
###########################service#############################
|
||||
##${ALL_FUNCS}
|
||||
static std::vector<byte_t> ${GET_DATA_FUNC_NAME}(void);
|
||||
|
||||
##${ALL_FUNCS_IMPLEMENTATIONS}
|
||||
// ${FUNCTION_DESCRIPTION}
|
||||
static std::vector<byte_t> ${GET_DATA_FUNC_NAME}(void){
|
||||
//...
|
||||
//...
|
||||
std::string example_string = ${FUNCTION_DESCRIPTION};
|
||||
std::vector<byte_t> data;
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
##${ALL_OFFER_METHODS}
|
||||
offer_methods.push_back({${SERVICE_ID}, ${INSTANCE_ID}, ${METHOD_ID}, &${GET_DATA_FUNC_NAME}});
|
||||
##${ALL_OFFER_EVENTS}
|
||||
"""offer_events.push_back({${SERVICE_ID}, ${INSTANCE_ID}, ${EVENTGROUP_ID},
|
||||
${EVENT_ID}, ${EVENT_TYPE}, ${EVENT_CYCLE}, false,
|
||||
${UPDATE_ON_CHANGE}, nullptr, ${RELIABILITY}, &${GET_DATA_FUNC_NAME}});"""
|
||||
##${ALL_OFFER_SERVICES}
|
||||
"""my_services.push_back({${SERVICE_ID}, ${INSTANCE_ID}, DEFAULT_MAJOR, DEFAULT_MINOR});"""
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file accumulators.hpp
|
||||
/// Includes all of the Accumulators Framework
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_ACCUMULATORS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_ACCUMULATORS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/accumulators/framework/accumulator_set.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_concept.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/external.hpp>
|
||||
#include <boost/accumulators/framework/features.hpp>
|
||||
#include <boost/accumulators/framework/parameters/accumulator.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/parameters/weight.hpp>
|
||||
#include <boost/accumulators/framework/parameters/weights.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/droppable_accumulator.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/reference_accumulator.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/value_accumulator.hpp>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,232 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulators_fwd.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_ACCUMULATORS_FWD_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_ACCUMULATORS_FWD_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/core/enable_if.hpp>
|
||||
#include <boost/parameter/is_argument_pack.hpp>
|
||||
#include <boost/mpl/apply_fwd.hpp> // for mpl::na
|
||||
#include <boost/mpl/limits/vector.hpp>
|
||||
#include <boost/preprocessor/cat.hpp>
|
||||
#include <boost/preprocessor/arithmetic/inc.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
||||
#include <boost/accumulators/numeric/functional_fwd.hpp>
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_MAX_FEATURES
|
||||
/// The maximum number of accumulators that may be put in an accumulator_set.
|
||||
/// Defaults to BOOST_MPL_LIMIT_VECTOR_SIZE (which defaults to 20).
|
||||
# define BOOST_ACCUMULATORS_MAX_FEATURES BOOST_MPL_LIMIT_VECTOR_SIZE
|
||||
#endif
|
||||
|
||||
#if BOOST_ACCUMULATORS_MAX_FEATURES > BOOST_MPL_LIMIT_VECTOR_SIZE
|
||||
# error BOOST_ACCUMULATORS_MAX_FEATURES cannot be larger than BOOST_MPL_LIMIT_VECTOR_SIZE
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_MAX_ARGS
|
||||
/// The maximum number of arguments that may be specified to an accumulator_set's
|
||||
/// accumulation function. Defaults to 15.
|
||||
# define BOOST_ACCUMULATORS_MAX_ARGS 15
|
||||
#endif
|
||||
|
||||
#if BOOST_WORKAROUND(__GNUC__, == 3) \
|
||||
|| BOOST_WORKAROUND(__EDG_VERSION__, BOOST_TESTED_AT(306))
|
||||
# define BOOST_ACCUMULATORS_BROKEN_CONST_OVERLOADS
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_BROKEN_CONST_OVERLOADS
|
||||
# include <boost/type_traits/is_const.hpp>
|
||||
# define BOOST_ACCUMULATORS_PROTO_DISABLE_IF_IS_CONST(T)\
|
||||
, typename boost::disable_if<boost::is_const<T> >::type * = 0
|
||||
#else
|
||||
# define BOOST_ACCUMULATORS_PROTO_DISABLE_IF_IS_CONST(T)
|
||||
#endif
|
||||
|
||||
#define BOOST_ACCUMULATORS_GCC_VERSION \
|
||||
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Named parameters tags
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct sample;
|
||||
struct weight;
|
||||
struct accumulator;
|
||||
struct weights;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// User-level features
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename ValueType, typename Tag>
|
||||
struct value;
|
||||
|
||||
template<typename Tag>
|
||||
struct value_tag;
|
||||
|
||||
template<typename Referent, typename Tag>
|
||||
struct reference;
|
||||
|
||||
template<typename Tag>
|
||||
struct reference_tag;
|
||||
|
||||
template<typename Type, typename Tag = void, typename AccumulatorSet = void>
|
||||
struct external;
|
||||
|
||||
template<typename Feature>
|
||||
struct droppable;
|
||||
}
|
||||
|
||||
template<typename Accumulator>
|
||||
struct droppable_accumulator_base;
|
||||
|
||||
template<typename Accumulator>
|
||||
struct droppable_accumulator;
|
||||
|
||||
template<typename Accumulator>
|
||||
struct with_cached_result;
|
||||
|
||||
template<typename Sample, typename Features, typename Weight = void>
|
||||
struct accumulator_set;
|
||||
|
||||
template<typename Feature>
|
||||
struct extractor;
|
||||
|
||||
template<typename Feature>
|
||||
struct feature_of;
|
||||
|
||||
template<typename Feature>
|
||||
struct as_feature;
|
||||
|
||||
template<typename Feature>
|
||||
struct as_weighted_feature;
|
||||
|
||||
template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_ACCUMULATORS_MAX_FEATURES, typename Feature, mpl::na)>
|
||||
struct depends_on;
|
||||
|
||||
template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_ACCUMULATORS_MAX_FEATURES, typename Feature, mpl::na)>
|
||||
struct features;
|
||||
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type const &
|
||||
find_accumulator(AccumulatorSet const &acc);
|
||||
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type
|
||||
extract_result(AccumulatorSet const &acc);
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct _enabler
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
// ... other overloads generated by Boost.Preprocessor:
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_EXTRACT_RESULT_FWD(z, n, _) \
|
||||
template< \
|
||||
typename Feature \
|
||||
, typename AccumulatorSet \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type \
|
||||
extract_result( \
|
||||
AccumulatorSet const &acc \
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::enable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
); \
|
||||
template< \
|
||||
typename Feature \
|
||||
, typename AccumulatorSet \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type \
|
||||
extract_result( \
|
||||
AccumulatorSet const &acc \
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::disable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
);
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
BOOST_PP_REPEAT_FROM_TO(
|
||||
1
|
||||
, BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS)
|
||||
, BOOST_ACCUMULATORS_EXTRACT_RESULT_FWD
|
||||
, _
|
||||
)
|
||||
|
||||
#undef BOOST_ACCUMULATORS_EXTRACT_RESULT_FWD
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
template<typename Feature, typename AccumulatorSet, typename A1, typename A2 ...>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type
|
||||
extract_result(AccumulatorSet const &acc, A1 const &a1, A2 const &a2 ...);
|
||||
#endif
|
||||
|
||||
namespace impl
|
||||
{
|
||||
using namespace numeric::operators;
|
||||
|
||||
template<typename Accumulator, typename Tag>
|
||||
struct external_impl;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename Accumulator>
|
||||
struct feature_tag;
|
||||
|
||||
template<typename Feature, typename Sample, typename Weight>
|
||||
struct to_accumulator;
|
||||
|
||||
struct accumulator_set_base;
|
||||
|
||||
template<typename T>
|
||||
struct is_accumulator_set;
|
||||
|
||||
inline void ignore_variable(void const *) {}
|
||||
}
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#define BOOST_ACCUMULATORS_IGNORE_GLOBAL(X) \
|
||||
namespace detail \
|
||||
{ \
|
||||
struct BOOST_PP_CAT(ignore_, X) \
|
||||
{ \
|
||||
void ignore() \
|
||||
{ \
|
||||
boost::accumulators::detail::ignore_variable(&X); \
|
||||
} \
|
||||
}; \
|
||||
} \
|
||||
/**/
|
||||
|
||||
#include <boost/parameter/nested_keyword.hpp>
|
||||
|
||||
#endif // include guard
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_base.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_BASE_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_BASE_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/mpl/joint_view.hpp>
|
||||
#include <boost/mpl/single_view.hpp>
|
||||
#include <boost/mpl/fold.hpp>
|
||||
#include <boost/mpl/contains.hpp>
|
||||
#include <boost/mpl/empty_sequence.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_concept.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
typedef void void_;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// dont_care
|
||||
//
|
||||
struct dont_care
|
||||
{
|
||||
template<typename Args>
|
||||
dont_care(Args const &)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_base
|
||||
//
|
||||
struct accumulator_base
|
||||
{
|
||||
// hidden if defined in derived classes
|
||||
detail::void_ operator ()(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
typedef mpl::false_ is_droppable;
|
||||
|
||||
detail::void_ add_ref(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
detail::void_ drop(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
detail::void_ on_drop(dont_care)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_concept.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATOR_CONCEPT_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATOR_CONCEPT_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/concept_check.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
template<typename Stat>
|
||||
struct accumulator_concept
|
||||
{
|
||||
void constraints()
|
||||
{
|
||||
// TODO: define the stat concept
|
||||
}
|
||||
|
||||
Stat stat;
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,544 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_set.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATOR_SET_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATOR_SET_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/apply.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/protect.hpp>
|
||||
#include <boost/mpl/identity.hpp>
|
||||
#include <boost/mpl/is_sequence.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_base_of.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/core/enable_if.hpp>
|
||||
#include <boost/parameter/is_argument_pack.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_concept.hpp>
|
||||
#include <boost/accumulators/framework/parameters/accumulator.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/droppable_accumulator.hpp>
|
||||
#include <boost/fusion/include/any.hpp>
|
||||
#include <boost/fusion/include/find_if.hpp>
|
||||
#include <boost/fusion/include/for_each.hpp>
|
||||
#include <boost/fusion/include/filter_view.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_visitor
|
||||
// wrap a boost::parameter argument pack in a Fusion extractor object
|
||||
template<typename Args>
|
||||
struct accumulator_visitor
|
||||
{
|
||||
explicit accumulator_visitor(Args const &a)
|
||||
: args(a)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Accumulator>
|
||||
void operator ()(Accumulator &accumulator) const
|
||||
{
|
||||
accumulator(this->args);
|
||||
}
|
||||
|
||||
private:
|
||||
accumulator_visitor &operator =(accumulator_visitor const &);
|
||||
Args const &args;
|
||||
};
|
||||
|
||||
template<typename Args>
|
||||
inline accumulator_visitor<Args> const make_accumulator_visitor(Args const &args)
|
||||
{
|
||||
return accumulator_visitor<Args>(args);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_set_base
|
||||
struct accumulator_set_base
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// is_accumulator_set
|
||||
template<typename T>
|
||||
struct is_accumulator_set
|
||||
: mpl::if_<
|
||||
boost::is_base_of<
|
||||
accumulator_set_base
|
||||
, typename boost::remove_const<
|
||||
typename boost::remove_reference<T>::type
|
||||
>::type
|
||||
>
|
||||
, mpl::true_
|
||||
, mpl::false_
|
||||
>::type
|
||||
{
|
||||
};
|
||||
|
||||
// function object that serialize an accumulator
|
||||
template<typename Archive>
|
||||
struct serialize_accumulator
|
||||
{
|
||||
serialize_accumulator(Archive & _ar, const unsigned int _file_version) :
|
||||
ar(_ar), file_version(_file_version)
|
||||
{}
|
||||
|
||||
template<typename Accumulator>
|
||||
void operator ()(Accumulator &accumulator)
|
||||
{
|
||||
accumulator.serialize(ar, file_version);
|
||||
}
|
||||
|
||||
private:
|
||||
Archive& ar;
|
||||
const unsigned int file_version;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \brief A set of accumulators.
|
||||
///
|
||||
/// accumulator_set resolves the dependencies between features and ensures that
|
||||
/// the accumulators in the set are updated in the proper order.
|
||||
///
|
||||
/// acccumulator_set provides a general mechanism to visit the accumulators
|
||||
/// in the set in order, with or without a filter. You can also fetch a reference
|
||||
/// to an accumulator that corresponds to a feature.
|
||||
///
|
||||
template<typename Sample, typename Features, typename Weight>
|
||||
struct accumulator_set
|
||||
: detail::accumulator_set_base
|
||||
{
|
||||
typedef Sample sample_type; ///< The type of the samples that will be accumulated
|
||||
typedef Features features_type; ///< An MPL sequence of the features that should be accumulated.
|
||||
typedef Weight weight_type; ///< The type of the weight parameter. Must be a scalar. Defaults to void.
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef
|
||||
typename detail::make_accumulator_tuple<
|
||||
Features
|
||||
, Sample
|
||||
, Weight
|
||||
>::type
|
||||
accumulators_mpl_vector;
|
||||
|
||||
// generate a fusion::list of accumulators
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef
|
||||
typename detail::meta::make_acc_list<
|
||||
accumulators_mpl_vector
|
||||
>::type
|
||||
accumulators_type;
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
//BOOST_MPL_ASSERT((mpl::is_sequence<accumulators_type>));
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// default-construct all contained accumulators
|
||||
accumulator_set()
|
||||
: accumulators(
|
||||
detail::make_acc_list(
|
||||
accumulators_mpl_vector()
|
||||
, (boost::accumulators::accumulator = *this)
|
||||
)
|
||||
)
|
||||
{
|
||||
// Add-ref the Features that the user has specified
|
||||
this->template visit_if<detail::contains_feature_of_<Features> >(
|
||||
detail::make_add_ref_visitor(boost::accumulators::accumulator = *this)
|
||||
);
|
||||
}
|
||||
|
||||
/// \overload
|
||||
///
|
||||
/// \param a1 Optional named parameter to be passed to all the accumulators
|
||||
template<typename A1>
|
||||
explicit accumulator_set(
|
||||
A1 const &a1
|
||||
, typename boost::enable_if<
|
||||
parameter::is_argument_pack<A1>
|
||||
, detail::_enabler
|
||||
>::type = detail::_enabler()
|
||||
) : accumulators(
|
||||
detail::make_acc_list(
|
||||
accumulators_mpl_vector()
|
||||
, (boost::accumulators::accumulator = *this, a1)
|
||||
)
|
||||
)
|
||||
{
|
||||
// Add-ref the Features that the user has specified
|
||||
this->template visit_if<detail::contains_feature_of_<Features> >(
|
||||
detail::make_add_ref_visitor(boost::accumulators::accumulator = *this)
|
||||
);
|
||||
}
|
||||
|
||||
/// \overload
|
||||
///
|
||||
/// \param a1 Optional sample parameter to be passed to all the accumulators
|
||||
template<typename A1>
|
||||
explicit accumulator_set(
|
||||
A1 const &a1
|
||||
, typename boost::disable_if<
|
||||
parameter::is_argument_pack<A1>
|
||||
, detail::_enabler
|
||||
>::type = detail::_enabler()
|
||||
) : accumulators(
|
||||
detail::make_acc_list(
|
||||
accumulators_mpl_vector()
|
||||
, (
|
||||
boost::accumulators::accumulator = *this
|
||||
, boost::accumulators::sample = a1
|
||||
)
|
||||
)
|
||||
)
|
||||
{
|
||||
// Add-ref the Features that the user has specified
|
||||
this->template visit_if<detail::contains_feature_of_<Features> >(
|
||||
detail::make_add_ref_visitor(boost::accumulators::accumulator = *this)
|
||||
);
|
||||
}
|
||||
|
||||
// ... other overloads generated by Boost.Preprocessor:
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_ACCUMULATOR_SET_CTOR(z, n, _) \
|
||||
template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
|
||||
accumulator_set( \
|
||||
BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::enable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
) : accumulators( \
|
||||
detail::make_acc_list( \
|
||||
accumulators_mpl_vector() \
|
||||
, ( \
|
||||
boost::accumulators::accumulator = *this \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a) \
|
||||
) \
|
||||
) \
|
||||
) \
|
||||
{ \
|
||||
/* Add-ref the Features that the user has specified */ \
|
||||
this->template visit_if<detail::contains_feature_of_<Features> >( \
|
||||
detail::make_add_ref_visitor(boost::accumulators::accumulator = *this) \
|
||||
); \
|
||||
} \
|
||||
template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
|
||||
accumulator_set( \
|
||||
BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::disable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
) : accumulators( \
|
||||
detail::make_acc_list( \
|
||||
accumulators_mpl_vector() \
|
||||
, ( \
|
||||
boost::accumulators::accumulator = *this \
|
||||
, boost::accumulators::sample = BOOST_PP_ENUM_PARAMS_Z(z, n, a) \
|
||||
) \
|
||||
) \
|
||||
) \
|
||||
{ \
|
||||
/* Add-ref the Features that the user has specified */ \
|
||||
this->template visit_if<detail::contains_feature_of_<Features> >( \
|
||||
detail::make_add_ref_visitor(boost::accumulators::accumulator = *this) \
|
||||
); \
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
BOOST_PP_REPEAT_FROM_TO(
|
||||
2
|
||||
, BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS)
|
||||
, BOOST_ACCUMULATORS_ACCUMULATOR_SET_CTOR
|
||||
, _
|
||||
)
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// \overload
|
||||
///
|
||||
template<typename A1, typename A2, ...>
|
||||
accumulator_set(A1 const &a1, A2 const &a2, ...);
|
||||
#endif
|
||||
|
||||
// ... other overloads generated by Boost.Preprocessor below ...
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Visitation
|
||||
/// \param func UnaryFunction which is invoked with each accumulator in turn.
|
||||
template<typename UnaryFunction>
|
||||
void visit(UnaryFunction const &func)
|
||||
{
|
||||
fusion::for_each(this->accumulators, func);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Conditional visitation
|
||||
/// \param func UnaryFunction which is invoked with each accumulator in turn,
|
||||
/// provided the accumulator satisfies the MPL predicate FilterPred.
|
||||
template<typename FilterPred, typename UnaryFunction>
|
||||
void visit_if(UnaryFunction const &func)
|
||||
{
|
||||
fusion::filter_view<accumulators_type, FilterPred> filtered_accs(this->accumulators);
|
||||
fusion::for_each(filtered_accs, func);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// The return type of the operator() overloads is void.
|
||||
typedef void result_type;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Accumulation
|
||||
/// \param a1 Optional named parameter to be passed to all the accumulators
|
||||
void operator ()()
|
||||
{
|
||||
this->visit(
|
||||
detail::make_accumulator_visitor(
|
||||
boost::accumulators::accumulator = *this
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ... other overloads generated by Boost.Preprocessor:
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_ACCUMULATOR_SET_FUN_OP(z, n, _) \
|
||||
template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
|
||||
void operator ()( \
|
||||
BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::enable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
) \
|
||||
{ \
|
||||
this->visit( \
|
||||
detail::make_accumulator_visitor( \
|
||||
( \
|
||||
boost::accumulators::accumulator = *this \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a) \
|
||||
) \
|
||||
) \
|
||||
); \
|
||||
} \
|
||||
template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
|
||||
void operator ()( \
|
||||
BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::disable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type = detail::_enabler() \
|
||||
) \
|
||||
{ \
|
||||
this->visit( \
|
||||
detail::make_accumulator_visitor( \
|
||||
( \
|
||||
boost::accumulators::accumulator = *this \
|
||||
, boost::accumulators::sample = BOOST_PP_ENUM_PARAMS_Z(z, n, a) \
|
||||
) \
|
||||
) \
|
||||
); \
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
BOOST_PP_REPEAT_FROM_TO(
|
||||
1
|
||||
, BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS)
|
||||
, BOOST_ACCUMULATORS_ACCUMULATOR_SET_FUN_OP
|
||||
, _
|
||||
)
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// \overload
|
||||
///
|
||||
template<typename A1, typename A2, ...>
|
||||
void operator ()(A1 const &a1, A2 const &a2, ...);
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Extraction
|
||||
template<typename Feature>
|
||||
struct apply
|
||||
: fusion::result_of::value_of<
|
||||
typename fusion::result_of::find_if<
|
||||
accumulators_type
|
||||
, detail::matches_feature<Feature>
|
||||
>::type
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Extraction
|
||||
template<typename Feature>
|
||||
typename apply<Feature>::type &extract()
|
||||
{
|
||||
return *fusion::find_if<detail::matches_feature<Feature> >(this->accumulators);
|
||||
}
|
||||
|
||||
/// \overload
|
||||
template<typename Feature>
|
||||
typename apply<Feature>::type const &extract() const
|
||||
{
|
||||
return *fusion::find_if<detail::matches_feature<Feature> >(this->accumulators);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Drop
|
||||
template<typename Feature>
|
||||
void drop()
|
||||
{
|
||||
// You can only drop the features that you have specified explicitly
|
||||
typedef typename apply<Feature>::type the_accumulator;
|
||||
BOOST_MPL_ASSERT((detail::contains_feature_of<Features, the_accumulator>));
|
||||
|
||||
typedef
|
||||
typename feature_of<typename as_feature<Feature>::type>::type
|
||||
the_feature;
|
||||
|
||||
(*fusion::find_if<detail::matches_feature<Feature> >(this->accumulators))
|
||||
.drop(boost::accumulators::accumulator = *this);
|
||||
|
||||
// Also drop accumulators that this feature depends on
|
||||
typedef typename the_feature::dependencies dependencies;
|
||||
this->template visit_if<detail::contains_feature_of_<dependencies> >(
|
||||
detail::make_drop_visitor(boost::accumulators::accumulator = *this)
|
||||
);
|
||||
}
|
||||
|
||||
// make the accumulator set serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
detail::serialize_accumulator<Archive> serializer(ar, file_version);
|
||||
fusion::for_each(this->accumulators, serializer);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
accumulators_type accumulators;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// find_accumulator
|
||||
// find an accumulator in an accumulator_set corresponding to a feature
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type &
|
||||
find_accumulator(AccumulatorSet &acc BOOST_ACCUMULATORS_PROTO_DISABLE_IF_IS_CONST(AccumulatorSet))
|
||||
{
|
||||
return acc.template extract<Feature>();
|
||||
}
|
||||
|
||||
/// \overload
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type const &
|
||||
find_accumulator(AccumulatorSet const &acc)
|
||||
{
|
||||
return acc.template extract<Feature>();
|
||||
}
|
||||
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type
|
||||
extract_result(AccumulatorSet const &acc)
|
||||
{
|
||||
return find_accumulator<Feature>(acc).result(
|
||||
boost::accumulators::accumulator = acc
|
||||
);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract_result
|
||||
// extract a result from an accumulator set
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_EXTRACT_RESULT_FUN(z, n, _) \
|
||||
template< \
|
||||
typename Feature \
|
||||
, typename AccumulatorSet \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type \
|
||||
extract_result( \
|
||||
AccumulatorSet const &acc \
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::enable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type \
|
||||
) \
|
||||
{ \
|
||||
return find_accumulator<Feature>(acc).result( \
|
||||
( \
|
||||
boost::accumulators::accumulator = acc \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a) \
|
||||
) \
|
||||
); \
|
||||
} \
|
||||
template< \
|
||||
typename Feature \
|
||||
, typename AccumulatorSet \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename mpl::apply<AccumulatorSet, Feature>::type::result_type \
|
||||
extract_result( \
|
||||
AccumulatorSet const &acc \
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
, typename boost::disable_if< \
|
||||
parameter::is_argument_pack<A0> \
|
||||
, detail::_enabler \
|
||||
>::type \
|
||||
) \
|
||||
{ \
|
||||
return find_accumulator<Feature>(acc).result(( \
|
||||
boost::accumulators::accumulator = acc \
|
||||
, boost::accumulators::sample = BOOST_PP_ENUM_PARAMS_Z(z, n, a) \
|
||||
)); \
|
||||
}
|
||||
|
||||
BOOST_PP_REPEAT_FROM_TO(
|
||||
1
|
||||
, BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS)
|
||||
, BOOST_ACCUMULATORS_EXTRACT_RESULT_FUN
|
||||
, _
|
||||
)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,328 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// droppable_accumulator.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_DROPPABLE_ACCUMULATOR_HPP_EAN_13_12_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_DROPPABLE_ACCUMULATOR_HPP_EAN_13_12_2005
|
||||
|
||||
#include <new>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/mpl/apply.hpp>
|
||||
#include <boost/aligned_storage.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp> // for feature_of
|
||||
#include <boost/accumulators/framework/parameters/accumulator.hpp> // for accumulator
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
template<typename Accumulator>
|
||||
struct droppable_accumulator;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// add_ref_visitor
|
||||
// a fusion function object for add_ref'ing accumulators
|
||||
template<typename Args>
|
||||
struct add_ref_visitor
|
||||
{
|
||||
explicit add_ref_visitor(Args const &args)
|
||||
: args_(args)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Accumulator>
|
||||
void operator ()(Accumulator &acc) const
|
||||
{
|
||||
typedef typename Accumulator::feature_tag::dependencies dependencies;
|
||||
|
||||
acc.add_ref(this->args_);
|
||||
|
||||
// Also add_ref accumulators that this feature depends on
|
||||
this->args_[accumulator].template
|
||||
visit_if<detail::contains_feature_of_<dependencies> >(
|
||||
*this
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
add_ref_visitor &operator =(add_ref_visitor const &);
|
||||
Args const &args_;
|
||||
};
|
||||
|
||||
template<typename Args>
|
||||
add_ref_visitor<Args> make_add_ref_visitor(Args const &args)
|
||||
{
|
||||
return add_ref_visitor<Args>(args);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// drop_visitor
|
||||
// a fusion function object for dropping accumulators
|
||||
template<typename Args>
|
||||
struct drop_visitor
|
||||
{
|
||||
explicit drop_visitor(Args const &args)
|
||||
: args_(args)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Accumulator>
|
||||
void operator ()(Accumulator &acc) const
|
||||
{
|
||||
if(typename Accumulator::is_droppable())
|
||||
{
|
||||
typedef typename Accumulator::feature_tag::dependencies dependencies;
|
||||
|
||||
acc.drop(this->args_);
|
||||
// Also drop accumulators that this feature depends on
|
||||
this->args_[accumulator].template
|
||||
visit_if<detail::contains_feature_of_<dependencies> >(
|
||||
*this
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
drop_visitor &operator =(drop_visitor const &);
|
||||
Args const &args_;
|
||||
};
|
||||
|
||||
template<typename Args>
|
||||
drop_visitor<Args> make_drop_visitor(Args const &args)
|
||||
{
|
||||
return drop_visitor<Args>(args);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// droppable_accumulator_base
|
||||
template<typename Accumulator>
|
||||
struct droppable_accumulator_base
|
||||
: Accumulator
|
||||
{
|
||||
typedef droppable_accumulator_base base;
|
||||
typedef mpl::true_ is_droppable;
|
||||
typedef typename Accumulator::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
droppable_accumulator_base(Args const &args)
|
||||
: Accumulator(args)
|
||||
, ref_count_(0)
|
||||
{
|
||||
}
|
||||
|
||||
droppable_accumulator_base(droppable_accumulator_base const &that)
|
||||
: Accumulator(*static_cast<Accumulator const *>(&that))
|
||||
, ref_count_(that.ref_count_)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
if(!this->is_dropped())
|
||||
{
|
||||
this->Accumulator::operator ()(args);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void add_ref(Args const &)
|
||||
{
|
||||
++this->ref_count_;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void drop(Args const &args)
|
||||
{
|
||||
BOOST_ASSERT(0 < this->ref_count_);
|
||||
if(1 == this->ref_count_)
|
||||
{
|
||||
static_cast<droppable_accumulator<Accumulator> *>(this)->on_drop(args);
|
||||
}
|
||||
--this->ref_count_;
|
||||
}
|
||||
|
||||
bool is_dropped() const
|
||||
{
|
||||
return 0 == this->ref_count_;
|
||||
}
|
||||
|
||||
private:
|
||||
int ref_count_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// droppable_accumulator
|
||||
// this can be specialized for any type that needs special handling
|
||||
template<typename Accumulator>
|
||||
struct droppable_accumulator
|
||||
: droppable_accumulator_base<Accumulator>
|
||||
{
|
||||
template<typename Args>
|
||||
droppable_accumulator(Args const &args)
|
||||
: droppable_accumulator::base(args)
|
||||
{
|
||||
}
|
||||
|
||||
droppable_accumulator(droppable_accumulator const &that)
|
||||
: droppable_accumulator::base(*static_cast<typename droppable_accumulator::base const *>(&that))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// with_cached_result
|
||||
template<typename Accumulator>
|
||||
struct with_cached_result
|
||||
: Accumulator
|
||||
{
|
||||
typedef typename Accumulator::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
with_cached_result(Args const &args)
|
||||
: Accumulator(args)
|
||||
, cache()
|
||||
{
|
||||
}
|
||||
|
||||
with_cached_result(with_cached_result const &that)
|
||||
: Accumulator(*static_cast<Accumulator const *>(&that))
|
||||
, cache()
|
||||
{
|
||||
if(that.has_result())
|
||||
{
|
||||
this->set(that.get());
|
||||
}
|
||||
}
|
||||
|
||||
~with_cached_result()
|
||||
{
|
||||
// Since this is a base class of droppable_accumulator_base,
|
||||
// this destructor is called before any of droppable_accumulator_base's
|
||||
// members get cleaned up, including is_dropped, so the following
|
||||
// call to has_result() is valid.
|
||||
if(this->has_result())
|
||||
{
|
||||
this->get().~result_type();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void on_drop(Args const &args)
|
||||
{
|
||||
// cache the result at the point this calculation was dropped
|
||||
BOOST_ASSERT(!this->has_result());
|
||||
this->set(this->Accumulator::result(args));
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return this->has_result() ? this->get() : this->Accumulator::result(args);
|
||||
}
|
||||
|
||||
private:
|
||||
with_cached_result &operator =(with_cached_result const &);
|
||||
|
||||
void set(result_type const &r)
|
||||
{
|
||||
::new(this->cache.address()) result_type(r);
|
||||
}
|
||||
|
||||
result_type const &get() const
|
||||
{
|
||||
return *static_cast<result_type const *>(this->cache.address());
|
||||
}
|
||||
|
||||
bool has_result() const
|
||||
{
|
||||
typedef with_cached_result<Accumulator> this_type;
|
||||
typedef droppable_accumulator_base<this_type> derived_type;
|
||||
return static_cast<derived_type const *>(this)->is_dropped();
|
||||
}
|
||||
|
||||
aligned_storage<sizeof(result_type)> cache;
|
||||
};
|
||||
|
||||
namespace tag
|
||||
{
|
||||
template<typename Feature>
|
||||
struct as_droppable
|
||||
{
|
||||
typedef droppable<Feature> type;
|
||||
};
|
||||
|
||||
template<typename Feature>
|
||||
struct as_droppable<droppable<Feature> >
|
||||
{
|
||||
typedef droppable<Feature> type;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// droppable
|
||||
template<typename Feature>
|
||||
struct droppable
|
||||
: as_feature<Feature>::type
|
||||
{
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
typedef typename feature_type::dependencies tmp_dependencies_;
|
||||
|
||||
typedef
|
||||
typename mpl::transform<
|
||||
typename feature_type::dependencies
|
||||
, as_droppable<mpl::_1>
|
||||
>::type
|
||||
dependencies;
|
||||
|
||||
struct impl
|
||||
{
|
||||
template<typename Sample, typename Weight>
|
||||
struct apply
|
||||
{
|
||||
typedef
|
||||
droppable_accumulator<
|
||||
typename mpl::apply2<typename feature_type::impl, Sample, Weight>::type
|
||||
>
|
||||
type;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
// make droppable<tag::feature(modifier)> work
|
||||
template<typename Feature>
|
||||
struct as_feature<tag::droppable<Feature> >
|
||||
{
|
||||
typedef tag::droppable<typename as_feature<Feature>::type> type;
|
||||
};
|
||||
|
||||
// make droppable<tag::mean> work with non-void weights (should become
|
||||
// droppable<tag::weighted_mean>
|
||||
template<typename Feature>
|
||||
struct as_weighted_feature<tag::droppable<Feature> >
|
||||
{
|
||||
typedef tag::droppable<typename as_weighted_feature<Feature>::type> type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// droppable<Foo> provides the same feature as Foo
|
||||
template<typename Feature>
|
||||
struct feature_of<tag::droppable<Feature> >
|
||||
: feature_of<Feature>
|
||||
{
|
||||
};
|
||||
|
||||
// Note: Usually, the extractor is pulled into the accumulators namespace with
|
||||
// a using directive, not the tag. But the droppable<> feature doesn't have an
|
||||
// extractor, so we can put the droppable tag in the accumulators namespace
|
||||
// without fear of a name conflict.
|
||||
using tag::droppable;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// external_accumulator.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_EXTERNAL_ACCUMULATOR_HPP_EAN_01_12_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_EXTERNAL_ACCUMULATOR_HPP_EAN_01_12_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp> // for feature_tag
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/reference_accumulator.hpp>
|
||||
|
||||
namespace boost { namespace accumulators { namespace impl
|
||||
{
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// external_impl
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Accumulator, typename Tag>
|
||||
struct external_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename Accumulator::result_type result_type;
|
||||
typedef typename detail::feature_tag<Accumulator>::type feature_tag;
|
||||
|
||||
external_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return this->extract_(args, args[parameter::keyword<Tag>::instance | 0]);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<typename Args>
|
||||
static result_type extract_(Args const &args, int)
|
||||
{
|
||||
// No named parameter passed to the extractor. Maybe the external
|
||||
// feature is held by reference<>.
|
||||
extractor<feature_tag> extract;
|
||||
return extract(accumulators::reference_tag<Tag>(args));
|
||||
}
|
||||
|
||||
template<typename Args, typename AccumulatorSet>
|
||||
static result_type extract_(Args const &, AccumulatorSet const &acc)
|
||||
{
|
||||
// OK, a named parameter for this external feature was passed to the
|
||||
// extractor, so use that.
|
||||
extractor<feature_tag> extract;
|
||||
return extract(acc);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
namespace tag
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// external
|
||||
template<typename Feature, typename Tag, typename AccumulatorSet>
|
||||
struct external
|
||||
: depends_on<reference<AccumulatorSet, Tag> >
|
||||
{
|
||||
typedef
|
||||
accumulators::impl::external_impl<
|
||||
detail::to_accumulator<Feature, mpl::_1, mpl::_2>
|
||||
, Tag
|
||||
>
|
||||
impl;
|
||||
};
|
||||
|
||||
template<typename Feature, typename Tag>
|
||||
struct external<Feature, Tag, void>
|
||||
: depends_on<>
|
||||
{
|
||||
typedef
|
||||
accumulators::impl::external_impl<
|
||||
detail::to_accumulator<Feature, mpl::_1, mpl::_2>
|
||||
, Tag
|
||||
>
|
||||
impl;
|
||||
};
|
||||
}
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// external_accumulator<Feature, Tag> provides the same feature as Feature
|
||||
template<typename Feature, typename Tag, typename AccumulatorSet>
|
||||
struct feature_of<tag::external<Feature, Tag, AccumulatorSet> >
|
||||
: feature_of<Feature>
|
||||
{
|
||||
};
|
||||
|
||||
// Note: Usually, the extractor is pulled into the accumulators namespace with
|
||||
// a using directive, not the tag. But the external<> feature doesn't have an
|
||||
// extractor, so we can put the external tag in the accumulators namespace
|
||||
// without fear of a name conflict.
|
||||
using tag::external;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,89 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// reference_accumulator.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_REFERENCE_ACCUMULATOR_HPP_EAN_03_23_2006
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_REFERENCE_ACCUMULATOR_HPP_EAN_03_23_2006
|
||||
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/mpl/always.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp> // for feature_tag
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// reference_accumulator_impl
|
||||
//
|
||||
template<typename Referent, typename Tag>
|
||||
struct reference_accumulator_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef Referent &result_type;
|
||||
|
||||
template<typename Args>
|
||||
reference_accumulator_impl(Args const &args)
|
||||
: ref(args[parameter::keyword<Tag>::instance])
|
||||
{
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->ref;
|
||||
}
|
||||
|
||||
private:
|
||||
reference_wrapper<Referent> ref;
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
namespace tag
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// reference_tag
|
||||
template<typename Tag>
|
||||
struct reference_tag
|
||||
{
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// reference
|
||||
template<typename Referent, typename Tag>
|
||||
struct reference
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::reference_accumulator_impl<Referent, Tag> > impl;
|
||||
};
|
||||
}
|
||||
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, reference, (typename)(typename))
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, reference_tag, (typename))
|
||||
}
|
||||
|
||||
using extract::reference;
|
||||
using extract::reference_tag;
|
||||
|
||||
// Map all reference<V,T> features to reference_tag<T> so
|
||||
// that references can be extracted using reference_tag<T>
|
||||
// without specifying the referent type.
|
||||
template<typename ValueType, typename Tag>
|
||||
struct feature_of<tag::reference<ValueType, Tag> >
|
||||
: feature_of<tag::reference_tag<Tag> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,89 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// value_accumulator.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler, Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_VALUE_ACCUMULATOR_HPP_EAN_03_23_2006
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_ACCUMULATORS_VALUE_ACCUMULATOR_HPP_EAN_03_23_2006
|
||||
|
||||
#include <boost/mpl/always.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp> // for feature_tag
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// value_accumulator_impl
|
||||
template<typename ValueType, typename Tag>
|
||||
struct value_accumulator_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef ValueType result_type;
|
||||
|
||||
template<typename Args>
|
||||
value_accumulator_impl(Args const &args)
|
||||
: val(args[parameter::keyword<Tag>::instance])
|
||||
{
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->val;
|
||||
}
|
||||
|
||||
private:
|
||||
ValueType val;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
namespace tag
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// value_tag
|
||||
template<typename Tag>
|
||||
struct value_tag
|
||||
{
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// value
|
||||
template<typename ValueType, typename Tag>
|
||||
struct value
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::value_accumulator_impl<ValueType, Tag> > impl;
|
||||
};
|
||||
}
|
||||
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, value, (typename)(typename))
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, value_tag, (typename))
|
||||
}
|
||||
|
||||
using extract::value;
|
||||
using extract::value_tag;
|
||||
|
||||
// Map all value<V,T> features to value_tag<T> so
|
||||
// that values can be extracted using value_tag<T>
|
||||
// without specifying the value type.
|
||||
template<typename ValueType, typename Tag>
|
||||
struct feature_of<tag::value<ValueType, Tag> >
|
||||
: feature_of<tag::value_tag<Tag> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,455 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// depends_on.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_DEPENDS_ON_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_DEPENDS_ON_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/mpl/end.hpp>
|
||||
#include <boost/mpl/map.hpp>
|
||||
#include <boost/mpl/set.hpp>
|
||||
#include <boost/mpl/copy.hpp>
|
||||
#include <boost/mpl/fold.hpp>
|
||||
#include <boost/mpl/size.hpp>
|
||||
#include <boost/mpl/sort.hpp>
|
||||
#include <boost/mpl/insert.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/remove.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/mpl/inherit.hpp>
|
||||
#include <boost/mpl/identity.hpp>
|
||||
#include <boost/mpl/equal_to.hpp>
|
||||
#include <boost/mpl/contains.hpp>
|
||||
#include <boost/mpl/transform.hpp>
|
||||
#include <boost/mpl/is_sequence.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/mpl/insert_range.hpp>
|
||||
#include <boost/mpl/back_inserter.hpp>
|
||||
#include <boost/mpl/transform_view.hpp>
|
||||
#include <boost/mpl/inherit_linearly.hpp>
|
||||
#include <boost/type_traits/is_base_and_derived.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/facilities/intercept.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/fusion/include/next.hpp>
|
||||
#include <boost/fusion/include/equal_to.hpp>
|
||||
#include <boost/fusion/include/value_of.hpp>
|
||||
#include <boost/fusion/include/mpl.hpp>
|
||||
#include <boost/fusion/include/end.hpp>
|
||||
#include <boost/fusion/include/begin.hpp>
|
||||
#include <boost/fusion/include/cons.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// as_feature
|
||||
template<typename Feature>
|
||||
struct as_feature
|
||||
{
|
||||
typedef Feature type;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// weighted_feature
|
||||
template<typename Feature>
|
||||
struct as_weighted_feature
|
||||
{
|
||||
typedef Feature type;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// feature_of
|
||||
template<typename Feature>
|
||||
struct feature_of
|
||||
{
|
||||
typedef Feature type;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// feature_tag
|
||||
template<typename Accumulator>
|
||||
struct feature_tag
|
||||
{
|
||||
typedef typename Accumulator::feature_tag type;
|
||||
};
|
||||
|
||||
template<typename Feature>
|
||||
struct undroppable
|
||||
{
|
||||
typedef Feature type;
|
||||
};
|
||||
|
||||
template<typename Feature>
|
||||
struct undroppable<tag::droppable<Feature> >
|
||||
{
|
||||
typedef Feature type;
|
||||
};
|
||||
|
||||
// For the purpose of determining whether one feature depends on another,
|
||||
// disregard whether the feature is droppable or not.
|
||||
template<typename A, typename B>
|
||||
struct is_dependent_on
|
||||
: is_base_and_derived<
|
||||
typename feature_of<typename undroppable<B>::type>::type
|
||||
, typename undroppable<A>::type
|
||||
>
|
||||
{};
|
||||
|
||||
template<typename Feature>
|
||||
struct dependencies_of
|
||||
{
|
||||
typedef typename Feature::dependencies type;
|
||||
};
|
||||
|
||||
// Should use mpl::insert_range, but doesn't seem to work with mpl sets
|
||||
template<typename Set, typename Range>
|
||||
struct set_insert_range
|
||||
: mpl::fold<
|
||||
Range
|
||||
, Set
|
||||
, mpl::insert<mpl::_1, mpl::_2>
|
||||
>
|
||||
{};
|
||||
|
||||
template<typename Features>
|
||||
struct collect_abstract_features
|
||||
: mpl::fold<
|
||||
Features
|
||||
, mpl::set0<>
|
||||
, set_insert_range<
|
||||
mpl::insert<mpl::_1, feature_of<mpl::_2> >
|
||||
, collect_abstract_features<dependencies_of<mpl::_2> >
|
||||
>
|
||||
>
|
||||
{};
|
||||
|
||||
template<typename Features>
|
||||
struct depends_on_base
|
||||
: mpl::inherit_linearly<
|
||||
typename mpl::sort<
|
||||
typename mpl::copy<
|
||||
typename collect_abstract_features<Features>::type
|
||||
, mpl::back_inserter<mpl::vector0<> >
|
||||
>::type
|
||||
, is_dependent_on<mpl::_1, mpl::_2>
|
||||
>::type
|
||||
// Don't inherit multiply from a feature
|
||||
, mpl::if_<
|
||||
is_dependent_on<mpl::_1, mpl::_2>
|
||||
, mpl::_1
|
||||
, mpl::inherit<mpl::_1, mpl::_2>
|
||||
>
|
||||
>::type
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// depends_on
|
||||
template<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, typename Feature)>
|
||||
struct depends_on
|
||||
: detail::depends_on_base<
|
||||
typename mpl::transform<
|
||||
mpl::vector<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, Feature)>
|
||||
, as_feature<mpl::_1>
|
||||
>::type
|
||||
>
|
||||
{
|
||||
typedef mpl::false_ is_weight_accumulator;
|
||||
typedef
|
||||
typename mpl::transform<
|
||||
mpl::vector<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, Feature)>
|
||||
, as_feature<mpl::_1>
|
||||
>::type
|
||||
dependencies;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename Feature>
|
||||
struct matches_feature
|
||||
{
|
||||
template<typename Accumulator>
|
||||
struct apply
|
||||
: is_same<
|
||||
typename feature_of<typename as_feature<Feature>::type>::type
|
||||
, typename feature_of<typename as_feature<typename feature_tag<Accumulator>::type>::type>::type
|
||||
>
|
||||
{};
|
||||
};
|
||||
|
||||
template<typename Features, typename Accumulator>
|
||||
struct contains_feature_of
|
||||
{
|
||||
typedef
|
||||
mpl::transform_view<Features, feature_of<as_feature<mpl::_> > >
|
||||
features_list;
|
||||
|
||||
typedef
|
||||
typename feature_of<typename feature_tag<Accumulator>::type>::type
|
||||
the_feature;
|
||||
|
||||
typedef
|
||||
typename mpl::contains<features_list, the_feature>::type
|
||||
type;
|
||||
};
|
||||
|
||||
// This is to work around a bug in early versions of Fusion which caused
|
||||
// a compile error if contains_feature_of<List, mpl::_> is used as a
|
||||
// predicate to fusion::find_if
|
||||
template<typename Features>
|
||||
struct contains_feature_of_
|
||||
{
|
||||
template<typename Accumulator>
|
||||
struct apply
|
||||
: contains_feature_of<Features, Accumulator>
|
||||
{};
|
||||
};
|
||||
|
||||
template<
|
||||
typename First
|
||||
, typename Last
|
||||
, bool is_empty = fusion::result_of::equal_to<First, Last>::value
|
||||
>
|
||||
struct build_acc_list;
|
||||
|
||||
template<typename First, typename Last>
|
||||
struct build_acc_list<First, Last, true>
|
||||
{
|
||||
typedef fusion::nil_ type;
|
||||
|
||||
template<typename Args>
|
||||
static fusion::nil_
|
||||
call(Args const &, First const&, Last const&)
|
||||
{
|
||||
return fusion::nil_();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename First, typename Last>
|
||||
struct build_acc_list<First, Last, false>
|
||||
{
|
||||
typedef
|
||||
build_acc_list<typename fusion::result_of::next<First>::type, Last>
|
||||
next_build_acc_list;
|
||||
|
||||
typedef fusion::cons<
|
||||
typename fusion::result_of::value_of<First>::type
|
||||
, typename next_build_acc_list::type>
|
||||
type;
|
||||
|
||||
template<typename Args>
|
||||
static type
|
||||
call(Args const &args, First const& f, Last const& l)
|
||||
{
|
||||
return type(args, next_build_acc_list::call(args, fusion::next(f), l));
|
||||
}
|
||||
};
|
||||
|
||||
namespace meta
|
||||
{
|
||||
template<typename Sequence>
|
||||
struct make_acc_list
|
||||
: build_acc_list<
|
||||
typename fusion::result_of::begin<Sequence>::type
|
||||
, typename fusion::result_of::end<Sequence>::type
|
||||
>
|
||||
{};
|
||||
}
|
||||
|
||||
template<typename Sequence, typename Args>
|
||||
typename meta::make_acc_list<Sequence>::type
|
||||
make_acc_list(Sequence &seq, Args const &args)
|
||||
{
|
||||
return meta::make_acc_list<Sequence>::call(args, fusion::begin(seq), fusion::end(seq));
|
||||
}
|
||||
|
||||
template<typename Sequence, typename Args>
|
||||
typename meta::make_acc_list<Sequence>::type
|
||||
make_acc_list(Sequence const &seq, Args const &args)
|
||||
{
|
||||
return meta::make_acc_list<Sequence const>::call(args, fusion::begin(seq), fusion::end(seq));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// checked_as_weighted_feature
|
||||
template<typename Feature>
|
||||
struct checked_as_weighted_feature
|
||||
{
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
typedef typename as_weighted_feature<feature_type>::type type;
|
||||
// weighted and non-weighted flavors should provide the same feature.
|
||||
BOOST_MPL_ASSERT((
|
||||
is_same<
|
||||
typename feature_of<feature_type>::type
|
||||
, typename feature_of<type>::type
|
||||
>
|
||||
));
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// as_feature_list
|
||||
template<typename Features, typename Weight>
|
||||
struct as_feature_list
|
||||
: mpl::transform_view<Features, checked_as_weighted_feature<mpl::_1> >
|
||||
{
|
||||
};
|
||||
|
||||
template<typename Features>
|
||||
struct as_feature_list<Features, void>
|
||||
: mpl::transform_view<Features, as_feature<mpl::_1> >
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// accumulator_wrapper
|
||||
template<typename Accumulator, typename Feature>
|
||||
struct accumulator_wrapper
|
||||
: Accumulator
|
||||
{
|
||||
typedef Feature feature_tag;
|
||||
|
||||
accumulator_wrapper(accumulator_wrapper const &that)
|
||||
: Accumulator(*static_cast<Accumulator const *>(&that))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
accumulator_wrapper(Args const &args)
|
||||
: Accumulator(args)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// to_accumulator
|
||||
template<typename Feature, typename Sample, typename Weight>
|
||||
struct to_accumulator
|
||||
{
|
||||
typedef
|
||||
accumulator_wrapper<
|
||||
typename mpl::apply2<typename Feature::impl, Sample, Weight>::type
|
||||
, Feature
|
||||
>
|
||||
type;
|
||||
};
|
||||
|
||||
template<typename Feature, typename Sample, typename Weight, typename Tag, typename AccumulatorSet>
|
||||
struct to_accumulator<Feature, Sample, tag::external<Weight, Tag, AccumulatorSet> >
|
||||
{
|
||||
BOOST_MPL_ASSERT((is_same<Tag, void>));
|
||||
BOOST_MPL_ASSERT((is_same<AccumulatorSet, void>));
|
||||
|
||||
typedef
|
||||
accumulator_wrapper<
|
||||
typename mpl::apply2<typename Feature::impl, Sample, Weight>::type
|
||||
, Feature
|
||||
>
|
||||
accumulator_type;
|
||||
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
typename Feature::is_weight_accumulator
|
||||
, accumulator_wrapper<impl::external_impl<accumulator_type, tag::weights>, Feature>
|
||||
, accumulator_type
|
||||
>::type
|
||||
type;
|
||||
};
|
||||
|
||||
// BUGBUG work around an MPL bug wrt map insertion
|
||||
template<typename FeatureMap, typename Feature>
|
||||
struct insert_feature
|
||||
: mpl::eval_if<
|
||||
mpl::has_key<FeatureMap, typename feature_of<Feature>::type>
|
||||
, mpl::identity<FeatureMap>
|
||||
, mpl::insert<FeatureMap, mpl::pair<typename feature_of<Feature>::type, Feature> >
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename FeatureMap, typename Feature, typename Weight>
|
||||
struct insert_dependencies
|
||||
: mpl::fold<
|
||||
as_feature_list<typename Feature::dependencies, Weight>
|
||||
, FeatureMap
|
||||
, insert_dependencies<
|
||||
insert_feature<mpl::_1, mpl::_2>
|
||||
, mpl::_2
|
||||
, Weight
|
||||
>
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename FeatureMap, typename Features, typename Weight>
|
||||
struct insert_sequence
|
||||
: mpl::fold< // BUGBUG should use insert_range, but doesn't seem to work for maps
|
||||
as_feature_list<Features, Weight>
|
||||
, FeatureMap
|
||||
, insert_feature<mpl::_1, mpl::_2>
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename Features, typename Sample, typename Weight>
|
||||
struct make_accumulator_tuple
|
||||
{
|
||||
typedef
|
||||
typename mpl::fold<
|
||||
as_feature_list<Features, Weight>
|
||||
, mpl::map0<>
|
||||
, mpl::if_<
|
||||
mpl::is_sequence<mpl::_2>
|
||||
, insert_sequence<mpl::_1, mpl::_2, Weight>
|
||||
, insert_feature<mpl::_1, mpl::_2>
|
||||
>
|
||||
>::type
|
||||
feature_map;
|
||||
|
||||
// for each element in the map, add its dependencies also
|
||||
typedef
|
||||
typename mpl::fold<
|
||||
feature_map
|
||||
, feature_map
|
||||
, insert_dependencies<mpl::_1, mpl::second<mpl::_2>, Weight>
|
||||
>::type
|
||||
feature_map_with_dependencies;
|
||||
|
||||
// turn the map into a vector so we can sort it
|
||||
typedef
|
||||
typename mpl::insert_range<
|
||||
mpl::vector<>
|
||||
, mpl::end<mpl::vector<> >::type
|
||||
, mpl::transform_view<feature_map_with_dependencies, mpl::second<mpl::_1> >
|
||||
>::type
|
||||
feature_vector_with_dependencies;
|
||||
|
||||
// sort the features according to which is derived from which
|
||||
typedef
|
||||
typename mpl::sort<
|
||||
feature_vector_with_dependencies
|
||||
, is_dependent_on<mpl::_2, mpl::_1>
|
||||
>::type
|
||||
sorted_feature_vector;
|
||||
|
||||
// From the vector of features, construct a vector of accumulators
|
||||
typedef
|
||||
typename mpl::transform<
|
||||
sorted_feature_vector
|
||||
, to_accumulator<mpl::_1, Sample, Weight>
|
||||
>::type
|
||||
type;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// external.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_EXTERNAL_HPP_EAN_01_12_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_EXTERNAL_HPP_EAN_01_12_2005
|
||||
|
||||
#include <boost/mpl/apply.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
|
||||
|
||||
//namespace boost { namespace accumulators
|
||||
//{
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//// external
|
||||
////
|
||||
//template<typename Type>
|
||||
//struct external
|
||||
//{
|
||||
//};
|
||||
//
|
||||
//}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,248 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extractor.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_EXTRACTOR_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_EXTRACTOR_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/preprocessor/cat.hpp>
|
||||
#include <boost/preprocessor/tuple/rem.hpp>
|
||||
#include <boost/preprocessor/array/size.hpp>
|
||||
#include <boost/preprocessor/array/data.hpp>
|
||||
#include <boost/preprocessor/array/elem.hpp>
|
||||
#include <boost/preprocessor/seq/to_array.hpp>
|
||||
#include <boost/preprocessor/seq/transform.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
||||
#include <boost/parameter/binding.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/eval_if.hpp>
|
||||
#include <boost/mpl/apply.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/parameters/accumulator.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename AccumulatorSet, typename Feature>
|
||||
struct accumulator_set_result
|
||||
{
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
typedef typename mpl::apply<
|
||||
typename boost::remove_const<
|
||||
typename boost::remove_reference<AccumulatorSet>::type
|
||||
>::type
|
||||
, feature_type
|
||||
>::type::result_type type;
|
||||
};
|
||||
|
||||
template<typename Args, typename Feature>
|
||||
struct argument_pack_result
|
||||
: accumulator_set_result<
|
||||
typename boost::remove_reference<
|
||||
typename parameter::binding<
|
||||
typename boost::remove_const<
|
||||
typename boost::remove_reference<Args>::type
|
||||
>::type
|
||||
, tag::accumulator
|
||||
>::type
|
||||
>::type
|
||||
, Feature
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename A, typename Feature>
|
||||
struct extractor_result
|
||||
: mpl::eval_if<
|
||||
detail::is_accumulator_set<A>
|
||||
, accumulator_set_result<A, Feature>
|
||||
, argument_pack_result<A, Feature>
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename Feature, typename AccumulatorSet>
|
||||
typename extractor_result<AccumulatorSet, Feature>::type
|
||||
do_extract(AccumulatorSet const &acc, mpl::true_)
|
||||
{
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
return extract_result<feature_type>(acc);
|
||||
}
|
||||
|
||||
template<typename Feature, typename Args>
|
||||
typename extractor_result<Args, Feature>::type
|
||||
do_extract(Args const &args, mpl::false_)
|
||||
{
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
return find_accumulator<feature_type>(args[accumulator]).result(args);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Extracts the result associated with Feature from the specified accumulator_set.
|
||||
template<typename Feature>
|
||||
struct extractor
|
||||
{
|
||||
typedef extractor<Feature> this_type;
|
||||
|
||||
/// The result meta-function for determining the return type of the extractor
|
||||
template<typename F>
|
||||
struct result;
|
||||
|
||||
template<typename A1>
|
||||
struct result<this_type(A1)>
|
||||
: detail::extractor_result<A1, Feature>
|
||||
{
|
||||
};
|
||||
|
||||
/// Extract the result associated with Feature from the accumulator set
|
||||
/// \param acc The accumulator set object from which to extract the result
|
||||
template<typename Arg1>
|
||||
typename detail::extractor_result<Arg1, Feature>::type
|
||||
operator ()(Arg1 const &arg1) const
|
||||
{
|
||||
// Arg1 could be an accumulator_set or an argument pack containing
|
||||
// an accumulator_set. Dispatch accordingly.
|
||||
return detail::do_extract<Feature>(arg1, detail::is_accumulator_set<Arg1>());
|
||||
}
|
||||
|
||||
/// \overload
|
||||
///
|
||||
/// \param a1 Optional named parameter to be passed to the accumulator's result() function.
|
||||
template<typename AccumulatorSet, typename A1>
|
||||
typename detail::extractor_result<AccumulatorSet, Feature>::type
|
||||
operator ()(AccumulatorSet const &acc, A1 const &a1) const
|
||||
{
|
||||
BOOST_MPL_ASSERT((detail::is_accumulator_set<AccumulatorSet>));
|
||||
typedef typename as_feature<Feature>::type feature_type;
|
||||
return extract_result<feature_type>(acc, a1);
|
||||
}
|
||||
|
||||
// ... other overloads generated by Boost.Preprocessor:
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_EXTRACTOR_FUN_OP(z, n, _) \
|
||||
template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
|
||||
struct result<this_type(BOOST_PP_ENUM_PARAMS_Z(z, n, A))> \
|
||||
: detail::extractor_result<A1, Feature> \
|
||||
{}; \
|
||||
template< \
|
||||
typename AccumulatorSet \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename detail::extractor_result<AccumulatorSet, Feature>::type \
|
||||
operator ()( \
|
||||
AccumulatorSet const &acc \
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) \
|
||||
) const \
|
||||
{ \
|
||||
BOOST_MPL_ASSERT((detail::is_accumulator_set<AccumulatorSet>)); \
|
||||
typedef typename as_feature<Feature>::type feature_type; \
|
||||
return extract_result<feature_type>(acc BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a));\
|
||||
}
|
||||
|
||||
BOOST_PP_REPEAT_FROM_TO(
|
||||
2
|
||||
, BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS)
|
||||
, BOOST_ACCUMULATORS_EXTRACTOR_FUN_OP
|
||||
, _
|
||||
)
|
||||
|
||||
#undef BOOST_ACCUMULATORS_EXTRACTOR_FUN_OP
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// \overload
|
||||
///
|
||||
template<typename AccumulatorSet, typename A1, typename A2, ...>
|
||||
typename detail::extractor_result<AccumulatorSet, Feature>::type
|
||||
operator ()(AccumulatorSet const &acc, A1 const &a1, A2 const &a2, ...);
|
||||
#endif
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_ARRAY_REM(Array) \
|
||||
BOOST_PP_TUPLE_REM_CTOR(BOOST_PP_ARRAY_SIZE(Array), BOOST_PP_ARRAY_DATA(Array))
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_SEQ_REM(Seq) \
|
||||
BOOST_ACCUMULATORS_ARRAY_REM(BOOST_PP_SEQ_TO_ARRAY(Seq))
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_ARGS_OP(s, data, elem) \
|
||||
T ## s
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_PARAMS_OP(s, data, elem) \
|
||||
elem T ## s
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq) \
|
||||
Tag::Feature< \
|
||||
BOOST_ACCUMULATORS_SEQ_REM( \
|
||||
BOOST_PP_SEQ_TRANSFORM(BOOST_ACCUMULATORS_ARGS_OP, ~, ParamsSeq) \
|
||||
) \
|
||||
>
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN_IMPL(z, n, Tag, Feature, ParamsSeq) \
|
||||
template< \
|
||||
BOOST_ACCUMULATORS_SEQ_REM( \
|
||||
BOOST_PP_SEQ_TRANSFORM(BOOST_ACCUMULATORS_PARAMS_OP, ~, ParamsSeq) \
|
||||
) \
|
||||
, typename Arg1 \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A) \
|
||||
> \
|
||||
typename boost::accumulators::detail::extractor_result< \
|
||||
Arg1 \
|
||||
, BOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq) \
|
||||
>::type \
|
||||
Feature(Arg1 const &arg1 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) ) \
|
||||
{ \
|
||||
typedef BOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq) feature_type; \
|
||||
return boost::accumulators::extractor<feature_type>()( \
|
||||
arg1 BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a)); \
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN(z, n, _) \
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN_IMPL( \
|
||||
z \
|
||||
, n \
|
||||
, BOOST_PP_ARRAY_ELEM(0, _) \
|
||||
, BOOST_PP_ARRAY_ELEM(1, _) \
|
||||
, BOOST_PP_ARRAY_ELEM(2, _) \
|
||||
)
|
||||
|
||||
#define BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(Tag, Feature, ParamSeq) \
|
||||
BOOST_PP_REPEAT( \
|
||||
BOOST_PP_INC(BOOST_ACCUMULATORS_MAX_ARGS) \
|
||||
, BOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN \
|
||||
, (3, (Tag, Feature, ParamSeq)) \
|
||||
)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// features.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_STATS_HPP_EAN_08_12_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_STATS_HPP_EAN_08_12_2005
|
||||
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// features
|
||||
//
|
||||
template<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, typename Feature)>
|
||||
struct features
|
||||
: mpl::vector<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, Feature)>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// accumulator.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_ACCUMULATOR_HPP_EAN_31_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_ACCUMULATOR_HPP_EAN_31_10_2005
|
||||
|
||||
#include <boost/parameter/name.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
BOOST_PARAMETER_NAME((accumulator, tag) accumulator)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(accumulator)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// sample.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_SAMPLE_HPP_EAN_31_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_SAMPLE_HPP_EAN_31_10_2005
|
||||
|
||||
#include <boost/parameter/name.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
BOOST_PARAMETER_NAME((sample, tag) sample)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sample)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weight.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_WEIGHT_HPP_EAN_31_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_WEIGHT_HPP_EAN_31_10_2005
|
||||
|
||||
#include <boost/parameter/name.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
// The weight of a single sample
|
||||
BOOST_PARAMETER_NAME((weight, tag) weight)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weight)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weights.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_WEIGHTS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_FRAMEWORK_PARAMETERS_WEIGHTS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/parameter/name.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
// The weight accumulator
|
||||
BOOST_PARAMETER_NAME((weights, tag) weights)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weights)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_DETAIL_FUNCTION1_DWA200655_HPP
|
||||
# define BOOST_DETAIL_FUNCTION1_DWA200655_HPP
|
||||
|
||||
# include <boost/concept_check.hpp>
|
||||
# include <boost/type_traits/remove_reference.hpp>
|
||||
# include <boost/type_traits/add_const.hpp>
|
||||
# include <boost/mpl/apply.hpp>
|
||||
|
||||
namespace boost { namespace detail {
|
||||
|
||||
// A utility for creating unary function objects that play nicely with
|
||||
// boost::result_of and that handle the forwarding problem.
|
||||
//
|
||||
// mpl::apply<F, A0>::type is expected to be a stateless function
|
||||
// object that accepts an argument of type A0&. It is also expected
|
||||
// to have a nested ::result_type identical to its return type.
|
||||
template<typename F>
|
||||
struct function1
|
||||
{
|
||||
template<typename Signature>
|
||||
struct result
|
||||
{};
|
||||
|
||||
template<typename This, typename A0>
|
||||
struct result<This(A0)>
|
||||
{
|
||||
// How adding const to arguments handles rvalues.
|
||||
//
|
||||
// if A0 is arg0 is represents actual argument
|
||||
// -------- ------- --------------------------
|
||||
// T const & T const const T lvalue
|
||||
// T & T non-const T lvalue
|
||||
// T const T const const T rvalue
|
||||
// T T const non-const T rvalue
|
||||
typedef typename remove_reference<
|
||||
typename add_const< A0 >::type
|
||||
>::type arg0;
|
||||
|
||||
typedef typename mpl::apply1<F, arg0>::type impl;
|
||||
typedef typename impl::result_type type;
|
||||
};
|
||||
|
||||
// Handles mutable lvalues
|
||||
template<typename A0>
|
||||
typename result<function1(A0 &)>::type
|
||||
operator ()(A0 &a0) const
|
||||
{
|
||||
typedef typename result<function1(A0 &)>::impl impl;
|
||||
typedef typename result<function1(A0 &)>::type type;
|
||||
typedef A0 &arg0;
|
||||
BOOST_CONCEPT_ASSERT((UnaryFunction<impl, type, arg0>));
|
||||
//boost::function_requires<UnaryFunctionConcept<impl, type, arg0> >();
|
||||
return impl()(a0);
|
||||
}
|
||||
|
||||
// Handles const lvalues and all rvalues
|
||||
template<typename A0>
|
||||
typename result<function1(A0 const &)>::type
|
||||
operator ()(A0 const &a0) const
|
||||
{
|
||||
typedef typename result<function1(A0 const &)>::impl impl;
|
||||
typedef typename result<function1(A0 const &)>::type type;
|
||||
typedef A0 const &arg0;
|
||||
BOOST_CONCEPT_ASSERT((UnaryFunction<impl, type, arg0>));
|
||||
//boost::function_requires<UnaryFunctionConcept<impl, type, arg0> >();
|
||||
return impl()(a0);
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::detail
|
||||
|
||||
#endif // BOOST_DETAIL_FUNCTION1_DWA200655_HPP
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_DETAIL_FUNCTION2_DWA200655_HPP
|
||||
# define BOOST_DETAIL_FUNCTION2_DWA200655_HPP
|
||||
|
||||
# define args (2)
|
||||
# include <boost/accumulators/numeric/detail/function_n.hpp>
|
||||
|
||||
#endif // BOOST_DETAIL_FUNCTION2_DWA200655_HPP
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_DETAIL_FUNCTION3_DWA2006514_HPP
|
||||
# define BOOST_DETAIL_FUNCTION3_DWA2006514_HPP
|
||||
|
||||
# define args (3)
|
||||
# include <boost/accumulators/numeric/detail/function_n.hpp>
|
||||
|
||||
#endif // BOOST_DETAIL_FUNCTION3_DWA2006514_HPP
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_DETAIL_FUNCTION4_DWA2006514_HPP
|
||||
# define BOOST_DETAIL_FUNCTION4_DWA2006514_HPP
|
||||
|
||||
# define args (4)
|
||||
# include <boost/accumulators/numeric/detail/function_n.hpp>
|
||||
|
||||
#endif // BOOST_DETAIL_FUNCTION4_DWA2006514_HPP
|
||||
@@ -0,0 +1,148 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// #include guards intentionally disabled.
|
||||
// #ifndef BOOST_DETAIL_FUNCTION_N_DWA2006514_HPP
|
||||
// # define BOOST_DETAIL_FUNCTION_N_DWA2006514_HPP
|
||||
|
||||
#include <boost/mpl/void.hpp>
|
||||
#include <boost/mpl/apply.hpp>
|
||||
|
||||
#include <boost/preprocessor/control/if.hpp>
|
||||
#include <boost/preprocessor/cat.hpp>
|
||||
#include <boost/preprocessor/punctuation/comma_if.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat.hpp>
|
||||
#include <boost/preprocessor/seq/fold_left.hpp>
|
||||
#include <boost/preprocessor/seq/seq.hpp>
|
||||
#include <boost/preprocessor/seq/for_each.hpp>
|
||||
#include <boost/preprocessor/seq/for_each_i.hpp>
|
||||
#include <boost/preprocessor/seq/for_each_product.hpp>
|
||||
#include <boost/preprocessor/seq/size.hpp>
|
||||
#include <boost/type_traits/add_const.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
|
||||
namespace boost { namespace detail {
|
||||
|
||||
# define BOOST_DETAIL_default_arg(z, n, _) \
|
||||
typedef mpl::void_ BOOST_PP_CAT(arg, n);
|
||||
|
||||
# define BOOST_DETAIL_function_arg(z, n, _) \
|
||||
typedef typename remove_reference< \
|
||||
typename add_const< BOOST_PP_CAT(A, n) >::type \
|
||||
>::type BOOST_PP_CAT(arg, n);
|
||||
|
||||
#define BOOST_DETAIL_cat_arg_counts(s, state, n) \
|
||||
BOOST_PP_IF( \
|
||||
n \
|
||||
, BOOST_PP_CAT(state, BOOST_PP_CAT(_, n)) \
|
||||
, state \
|
||||
) \
|
||||
/**/
|
||||
|
||||
#define function_name \
|
||||
BOOST_PP_SEQ_FOLD_LEFT( \
|
||||
BOOST_DETAIL_cat_arg_counts \
|
||||
, BOOST_PP_CAT(function, BOOST_PP_SEQ_HEAD(args)) \
|
||||
, BOOST_PP_SEQ_TAIL(args)(0) \
|
||||
) \
|
||||
/**/
|
||||
|
||||
template<typename F>
|
||||
struct function_name
|
||||
{
|
||||
BOOST_PP_REPEAT(
|
||||
BOOST_MPL_LIMIT_METAFUNCTION_ARITY
|
||||
, BOOST_DETAIL_default_arg
|
||||
, ~
|
||||
)
|
||||
|
||||
template<typename Signature>
|
||||
struct result {};
|
||||
|
||||
#define BOOST_DETAIL_function_result(r, _, n) \
|
||||
template<typename This BOOST_PP_ENUM_TRAILING_PARAMS(n, typename A)> \
|
||||
struct result<This(BOOST_PP_ENUM_PARAMS(n, A))> \
|
||||
{ \
|
||||
BOOST_PP_REPEAT(n, BOOST_DETAIL_function_arg, ~) \
|
||||
typedef \
|
||||
typename BOOST_PP_CAT(mpl::apply, BOOST_MPL_LIMIT_METAFUNCTION_ARITY)<\
|
||||
F \
|
||||
BOOST_PP_ENUM_TRAILING_PARAMS( \
|
||||
BOOST_MPL_LIMIT_METAFUNCTION_ARITY \
|
||||
, arg \
|
||||
) \
|
||||
>::type \
|
||||
impl; \
|
||||
typedef typename impl::result_type type; \
|
||||
}; \
|
||||
/**/
|
||||
|
||||
BOOST_PP_SEQ_FOR_EACH(BOOST_DETAIL_function_result, _, args)
|
||||
|
||||
# define arg_type(r, _, i, is_const) \
|
||||
BOOST_PP_COMMA_IF(i) BOOST_PP_CAT(A, i) BOOST_PP_CAT(const_if, is_const) &
|
||||
|
||||
# define result_(r, n, constness) \
|
||||
typename result< \
|
||||
function_name( \
|
||||
BOOST_PP_SEQ_FOR_EACH_I_R(r, arg_type, ~, constness) \
|
||||
) \
|
||||
> \
|
||||
/**/
|
||||
|
||||
# define param(r, _, i, is_const) BOOST_PP_COMMA_IF(i) \
|
||||
BOOST_PP_CAT(A, i) BOOST_PP_CAT(const_if, is_const) & BOOST_PP_CAT(x, i)
|
||||
|
||||
# define param_list(r, n, constness) \
|
||||
BOOST_PP_SEQ_FOR_EACH_I_R(r, param, ~, constness)
|
||||
|
||||
# define call_operator(r, constness) \
|
||||
template<BOOST_PP_ENUM_PARAMS(BOOST_PP_SEQ_SIZE(constness), typename A)> \
|
||||
result_(r, BOOST_PP_SEQ_SIZE(constness), constness)::type \
|
||||
operator ()( param_list(r, BOOST_PP_SEQ_SIZE(constness), constness) ) const \
|
||||
{ \
|
||||
typedef result_(r, BOOST_PP_SEQ_SIZE(constness), constness)::impl impl; \
|
||||
return impl()(BOOST_PP_ENUM_PARAMS(BOOST_PP_SEQ_SIZE(constness), x)); \
|
||||
} \
|
||||
/**/
|
||||
|
||||
# define const_if0
|
||||
# define const_if1 const
|
||||
|
||||
# define bits(z, n, _) ((0)(1))
|
||||
|
||||
# define gen_operator(r, _, n) \
|
||||
BOOST_PP_SEQ_FOR_EACH_PRODUCT_R( \
|
||||
r \
|
||||
, call_operator \
|
||||
, BOOST_PP_REPEAT(n, bits, ~) \
|
||||
) \
|
||||
/**/
|
||||
|
||||
BOOST_PP_SEQ_FOR_EACH(
|
||||
gen_operator
|
||||
, ~
|
||||
, args
|
||||
)
|
||||
|
||||
# undef bits
|
||||
# undef const_if1
|
||||
# undef const_if0
|
||||
# undef call_operator
|
||||
# undef param_list
|
||||
# undef param
|
||||
# undef result_
|
||||
# undef default_
|
||||
# undef arg_type
|
||||
# undef gen_operator
|
||||
# undef function_name
|
||||
|
||||
# undef args
|
||||
};
|
||||
|
||||
}} // namespace boost::detail
|
||||
|
||||
//#endif // BOOST_DETAIL_FUNCTION_N_DWA2006514_HPP
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright David Abrahams 2006. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_DETAIL_POD_SINGLETON_DWA200655_HPP
|
||||
# define BOOST_DETAIL_POD_SINGLETON_DWA200655_HPP
|
||||
|
||||
namespace boost { namespace detail {
|
||||
|
||||
template<typename T>
|
||||
struct pod_singleton
|
||||
{
|
||||
static T instance;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T pod_singleton<T>::instance;
|
||||
|
||||
}} // namespace boost::detail
|
||||
|
||||
#endif // BOOST_DETAIL_POD_SINGLETON_DWA200655_HPP
|
||||
@@ -0,0 +1,537 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file functional.hpp
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
|
||||
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/and.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/type_traits/add_reference.hpp>
|
||||
#include <boost/type_traits/is_empty.hpp>
|
||||
#include <boost/type_traits/is_integral.hpp>
|
||||
#include <boost/type_traits/is_floating_point.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/typeof/typeof.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/numeric/functional_fwd.hpp>
|
||||
#include <boost/accumulators/numeric/detail/function1.hpp>
|
||||
#include <boost/accumulators/numeric/detail/function2.hpp>
|
||||
#include <boost/accumulators/numeric/detail/pod_singleton.hpp>
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
|
||||
# include <boost/accumulators/numeric/functional/vector.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
|
||||
# include <boost/accumulators/numeric/functional/valarray.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
|
||||
# include <boost/accumulators/numeric/functional/complex.hpp>
|
||||
#endif
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_DOXYGEN_INVOKED
|
||||
// Hack to make Doxygen show the inheritance relationships
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
namespace std
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<class Arg, class Ret> struct unary_function {};
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<class Left, class Right, class Ret> struct binary_function {};
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
namespace functional
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename A0, typename A1>
|
||||
struct are_integral
|
||||
: mpl::and_<is_integral<A0>, is_integral<A1> >
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right>
|
||||
struct left_ref
|
||||
{
|
||||
typedef Left &type;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename T>
|
||||
T &lvalue_of();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: handle complex weight, valarray, MTL vectors
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(Name, Op) \
|
||||
namespace functional \
|
||||
{ \
|
||||
template<typename Arg> \
|
||||
struct result_of_ ## Name \
|
||||
{ \
|
||||
BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
|
||||
nested \
|
||||
, Op boost::numeric::functional::detail::lvalue_of<Arg>() \
|
||||
) \
|
||||
typedef typename nested::type type; \
|
||||
}; \
|
||||
template<typename Arg, typename EnableIf> \
|
||||
struct Name ## _base \
|
||||
{ \
|
||||
typedef typename remove_const<Arg>::type argument_type; \
|
||||
typedef typename result_of_ ## Name<Arg>::type result_type; \
|
||||
typename result_of_ ## Name<Arg>::type operator ()(Arg &arg) const \
|
||||
{ \
|
||||
return Op arg; \
|
||||
} \
|
||||
}; \
|
||||
template<typename Arg, typename ArgTag> \
|
||||
struct Name \
|
||||
: Name ## _base<Arg, void> \
|
||||
{}; \
|
||||
} \
|
||||
namespace op \
|
||||
{ \
|
||||
struct Name \
|
||||
: boost::detail::function1<functional::Name<_, functional::tag<_> > > \
|
||||
{}; \
|
||||
} \
|
||||
namespace \
|
||||
{ \
|
||||
op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
|
||||
} \
|
||||
/**/
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \
|
||||
namespace functional \
|
||||
{ \
|
||||
template<typename Left, typename Right, typename EnableIf> \
|
||||
struct result_of_ ## Name \
|
||||
{ \
|
||||
RetType(Left, Op, Right) \
|
||||
}; \
|
||||
template<typename Left, typename Right, typename EnableIf> \
|
||||
struct Name ## _base \
|
||||
{ \
|
||||
typedef typename remove_const<Left>::type first_argument_type; \
|
||||
typedef typename remove_const<Right>::type second_argument_type; \
|
||||
typedef typename result_of_ ## Name<Left, Right>::type result_type; \
|
||||
typename result_of_ ## Name<Left, Right>::type \
|
||||
operator ()(Left &left, Right &right) const \
|
||||
{ \
|
||||
return left Op right; \
|
||||
} \
|
||||
}; \
|
||||
template<typename Left, typename Right, typename LeftTag, typename RightTag> \
|
||||
struct Name \
|
||||
: Name ## _base<Left, Right, void> \
|
||||
{}; \
|
||||
} \
|
||||
namespace op \
|
||||
{ \
|
||||
struct Name \
|
||||
: boost::detail::function2< \
|
||||
functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \
|
||||
> \
|
||||
{}; \
|
||||
} \
|
||||
namespace \
|
||||
{ \
|
||||
op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
|
||||
} \
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(Name) \
|
||||
/**/
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \
|
||||
BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
|
||||
nested \
|
||||
, boost::numeric::functional::detail::lvalue_of<Left>() Op \
|
||||
boost::numeric::functional::detail::lvalue_of<Right>() \
|
||||
) \
|
||||
typedef typename nested::type type; \
|
||||
/**/
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \
|
||||
typedef Left &type; \
|
||||
/**/
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !)
|
||||
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_LEFT
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP
|
||||
|
||||
namespace functional
|
||||
{
|
||||
template<typename Left, typename Right, typename EnableIf>
|
||||
struct min_assign_base
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
if(numeric::less(right, left))
|
||||
{
|
||||
left = right;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Left, typename Right, typename EnableIf>
|
||||
struct max_assign_base
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
if(numeric::greater(right, left))
|
||||
{
|
||||
left = right;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Left, typename Right, typename EnableIf>
|
||||
struct fdiv_base
|
||||
: functional::divides<Left, Right>
|
||||
{};
|
||||
|
||||
// partial specialization that promotes the arguments to double for
|
||||
// integral division.
|
||||
template<typename Left, typename Right>
|
||||
struct fdiv_base<Left, Right, typename enable_if<are_integral<Left, Right> >::type>
|
||||
: functional::divides<double const, double const>
|
||||
{};
|
||||
|
||||
template<typename To, typename From, typename EnableIf>
|
||||
struct promote_base
|
||||
{
|
||||
typedef From argument_type;
|
||||
typedef To result_type;
|
||||
|
||||
To operator ()(From &from) const
|
||||
{
|
||||
return from;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ToFrom>
|
||||
struct promote_base<ToFrom, ToFrom, void>
|
||||
{
|
||||
typedef ToFrom argument_type;
|
||||
typedef ToFrom result_type;
|
||||
|
||||
ToFrom &operator ()(ToFrom &tofrom)
|
||||
{
|
||||
return tofrom;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Arg, typename EnableIf>
|
||||
struct as_min_base
|
||||
{
|
||||
BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
|
||||
|
||||
typedef Arg argument_type;
|
||||
typedef typename remove_const<Arg>::type result_type;
|
||||
|
||||
typename remove_const<Arg>::type operator ()(Arg &) const
|
||||
{
|
||||
return (std::numeric_limits<typename remove_const<Arg>::type>::min)();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Arg>
|
||||
struct as_min_base<Arg, typename enable_if<is_floating_point<Arg> >::type>
|
||||
{
|
||||
BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
|
||||
|
||||
typedef Arg argument_type;
|
||||
typedef typename remove_const<Arg>::type result_type;
|
||||
|
||||
typename remove_const<Arg>::type operator ()(Arg &) const
|
||||
{
|
||||
return -(std::numeric_limits<typename remove_const<Arg>::type>::max)();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Arg, typename EnableIf>
|
||||
struct as_max_base
|
||||
{
|
||||
BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
|
||||
|
||||
typedef Arg argument_type;
|
||||
typedef typename remove_const<Arg>::type result_type;
|
||||
|
||||
typename remove_const<Arg>::type operator ()(Arg &) const
|
||||
{
|
||||
return (std::numeric_limits<typename remove_const<Arg>::type>::max)();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Arg, typename EnableIf>
|
||||
struct as_zero_base
|
||||
{
|
||||
typedef Arg argument_type;
|
||||
typedef typename remove_const<Arg>::type result_type;
|
||||
|
||||
typename remove_const<Arg>::type operator ()(Arg &) const
|
||||
{
|
||||
return numeric::zero<typename remove_const<Arg>::type>::value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Arg, typename EnableIf>
|
||||
struct as_one_base
|
||||
{
|
||||
typedef Arg argument_type;
|
||||
typedef typename remove_const<Arg>::type result_type;
|
||||
|
||||
typename remove_const<Arg>::type operator ()(Arg &) const
|
||||
{
|
||||
return numeric::one<typename remove_const<Arg>::type>::value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename To, typename From, typename ToTag, typename FromTag>
|
||||
struct promote
|
||||
: promote_base<To, From, void>
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right, typename LeftTag, typename RightTag>
|
||||
struct min_assign
|
||||
: min_assign_base<Left, Right, void>
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right, typename LeftTag, typename RightTag>
|
||||
struct max_assign
|
||||
: max_assign_base<Left, Right, void>
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right, typename LeftTag, typename RightTag>
|
||||
struct fdiv
|
||||
: fdiv_base<Left, Right, void>
|
||||
{};
|
||||
|
||||
/// INTERNAL ONLY
|
||||
/// For back-compat only. Use fdiv.
|
||||
template<typename Left, typename Right, typename LeftTag, typename RightTag>
|
||||
struct average
|
||||
: fdiv<Left, Right, LeftTag, RightTag>
|
||||
{};
|
||||
|
||||
template<typename Arg, typename Tag>
|
||||
struct as_min
|
||||
: as_min_base<Arg, void>
|
||||
{};
|
||||
|
||||
template<typename Arg, typename Tag>
|
||||
struct as_max
|
||||
: as_max_base<Arg, void>
|
||||
{};
|
||||
|
||||
template<typename Arg, typename Tag>
|
||||
struct as_zero
|
||||
: as_zero_base<Arg, void>
|
||||
{};
|
||||
|
||||
template<typename Arg, typename Tag>
|
||||
struct as_one
|
||||
: as_one_base<Arg, void>
|
||||
{};
|
||||
}
|
||||
|
||||
namespace op
|
||||
{
|
||||
template<typename To>
|
||||
struct promote
|
||||
: boost::detail::function1<functional::promote<To, _, typename functional::tag<To>::type, functional::tag<_> > >
|
||||
{};
|
||||
|
||||
struct min_assign
|
||||
: boost::detail::function2<functional::min_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
|
||||
{};
|
||||
|
||||
struct max_assign
|
||||
: boost::detail::function2<functional::max_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
|
||||
{};
|
||||
|
||||
struct fdiv
|
||||
: boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
|
||||
{};
|
||||
|
||||
/// INTERNAL ONLY
|
||||
struct average
|
||||
: boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
|
||||
{};
|
||||
|
||||
struct as_min
|
||||
: boost::detail::function1<functional::as_min<_, functional::tag<_> > >
|
||||
{};
|
||||
|
||||
struct as_max
|
||||
: boost::detail::function1<functional::as_max<_, functional::tag<_> > >
|
||||
{};
|
||||
|
||||
struct as_zero
|
||||
: boost::detail::function1<functional::as_zero<_, functional::tag<_> > >
|
||||
{};
|
||||
|
||||
struct as_one
|
||||
: boost::detail::function1<functional::as_one<_, functional::tag<_> > >
|
||||
{};
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
op::min_assign const &min_assign = boost::detail::pod_singleton<op::min_assign>::instance;
|
||||
op::max_assign const &max_assign = boost::detail::pod_singleton<op::max_assign>::instance;
|
||||
op::fdiv const &fdiv = boost::detail::pod_singleton<op::fdiv>::instance;
|
||||
op::fdiv const &average = boost::detail::pod_singleton<op::fdiv>::instance; ///< INTERNAL ONLY
|
||||
op::as_min const &as_min = boost::detail::pod_singleton<op::as_min>::instance;
|
||||
op::as_max const &as_max = boost::detail::pod_singleton<op::as_max>::instance;
|
||||
op::as_zero const &as_zero = boost::detail::pod_singleton<op::as_zero>::instance;
|
||||
op::as_one const &as_one = boost::detail::pod_singleton<op::as_one>::instance;
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(min_assign)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(max_assign)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(fdiv)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(average)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_min)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_max)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_zero)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_one)
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// promote
|
||||
template<typename To, typename From>
|
||||
typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
|
||||
promote(From &from)
|
||||
{
|
||||
return functional::promote<To, From>()(from);
|
||||
}
|
||||
|
||||
template<typename To, typename From>
|
||||
typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
|
||||
promote(From const &from)
|
||||
{
|
||||
return functional::promote<To const, From const>()(from);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct default_
|
||||
{
|
||||
typedef default_ type;
|
||||
typedef T value_type;
|
||||
static T const value;
|
||||
|
||||
operator T const & () const
|
||||
{
|
||||
return default_::value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T const default_<T>::value = T();
|
||||
|
||||
template<typename T>
|
||||
struct one
|
||||
{
|
||||
typedef one type;
|
||||
typedef T value_type;
|
||||
static T const value;
|
||||
|
||||
operator T const & () const
|
||||
{
|
||||
return one::value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T const one<T>::value = T(1);
|
||||
|
||||
template<typename T>
|
||||
struct zero
|
||||
{
|
||||
typedef zero type;
|
||||
typedef T value_type;
|
||||
static T const value;
|
||||
|
||||
operator T const & () const
|
||||
{
|
||||
return zero::value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T const zero<T>::value = T();
|
||||
|
||||
template<typename T>
|
||||
struct one_or_default
|
||||
: mpl::if_<is_empty<T>, default_<T>, one<T> >::type
|
||||
{};
|
||||
|
||||
template<typename T>
|
||||
struct zero_or_default
|
||||
: mpl::if_<is_empty<T>, default_<T>, zero<T> >::type
|
||||
{};
|
||||
|
||||
}} // namespace boost::numeric
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file complex.hpp
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_NUMERIC_FUNCTIONAL_COMPLEX_HPP_EAN_01_17_2006
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_COMPLEX_HPP_EAN_01_17_2006
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
|
||||
# error Include this file before boost/accumulators/numeric/functional.hpp
|
||||
#endif
|
||||
|
||||
#include <complex>
|
||||
#include <boost/mpl/or.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/typeof/std/complex.hpp>
|
||||
#include <boost/accumulators/numeric/functional_fwd.hpp>
|
||||
|
||||
namespace boost { namespace numeric { namespace operators
|
||||
{
|
||||
// So that the stats compile when Sample type is std::complex
|
||||
template<typename T, typename U>
|
||||
typename
|
||||
disable_if<
|
||||
mpl::or_<is_same<T, U>, is_same<std::complex<T>, U> >
|
||||
, std::complex<T>
|
||||
>::type
|
||||
operator *(std::complex<T> ri, U const &u)
|
||||
{
|
||||
// BUGBUG promote result to typeof(T()*u) ?
|
||||
return ri *= static_cast<T>(u);
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
typename
|
||||
disable_if<
|
||||
mpl::or_<is_same<T, U>, is_same<std::complex<T>, U> >
|
||||
, std::complex<T>
|
||||
>::type
|
||||
operator /(std::complex<T> ri, U const &u)
|
||||
{
|
||||
// BUGBUG promote result to typeof(T()*u) ?
|
||||
return ri /= static_cast<T>(u);
|
||||
}
|
||||
|
||||
}}} // namespace boost::numeric::operators
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename T>
|
||||
struct one_complex
|
||||
{
|
||||
static std::complex<T> const value;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::complex<T> const one_complex<T>::value
|
||||
= std::complex<T>(numeric::one<T>::value, numeric::one<T>::value);
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename T>
|
||||
struct one<std::complex<T> >
|
||||
: detail::one_complex<T>
|
||||
{
|
||||
typedef one type;
|
||||
typedef std::complex<T> value_type;
|
||||
operator value_type const & () const
|
||||
{
|
||||
return detail::one_complex<T>::value;
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::numeric
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,362 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file valarray.hpp
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
|
||||
# error Include this file before boost/accumulators/numeric/functional.hpp
|
||||
#endif
|
||||
|
||||
#include <valarray>
|
||||
#include <functional>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/mpl/and.hpp>
|
||||
#include <boost/mpl/not.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_scalar.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/typeof/std/valarray.hpp>
|
||||
#include <boost/accumulators/numeric/functional_fwd.hpp>
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
namespace operators
|
||||
{
|
||||
namespace acc_detail
|
||||
{
|
||||
template<typename Fun>
|
||||
struct make_valarray
|
||||
{
|
||||
typedef std::valarray<typename Fun::result_type> type;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle valarray<Left> / Right where Right is a scalar and Right != Left.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_enable_if<
|
||||
mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
|
||||
, acc_detail::make_valarray<functional::divides<Left, Right> >
|
||||
>::type
|
||||
operator /(std::valarray<Left> const &left, Right const &right)
|
||||
{
|
||||
typedef typename functional::divides<Left, Right>::result_type value_type;
|
||||
std::valarray<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::divides(left[i], right);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle valarray<Left> * Right where Right is a scalar and Right != Left.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_enable_if<
|
||||
mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
|
||||
, acc_detail::make_valarray<functional::multiplies<Left, Right> >
|
||||
>::type
|
||||
operator *(std::valarray<Left> const &left, Right const &right)
|
||||
{
|
||||
typedef typename functional::multiplies<Left, Right>::result_type value_type;
|
||||
std::valarray<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::multiplies(left[i], right);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle valarray<Left> + valarray<Right> where Right != Left.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_disable_if<
|
||||
is_same<Left, Right>
|
||||
, acc_detail::make_valarray<functional::plus<Left, Right> >
|
||||
>::type
|
||||
operator +(std::valarray<Left> const &left, std::valarray<Right> const &right)
|
||||
{
|
||||
typedef typename functional::plus<Left, Right>::result_type value_type;
|
||||
std::valarray<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::plus(left[i], right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
namespace functional
|
||||
{
|
||||
struct std_valarray_tag;
|
||||
|
||||
template<typename T>
|
||||
struct tag<std::valarray<T> >
|
||||
{
|
||||
typedef std_valarray_tag type;
|
||||
};
|
||||
|
||||
#ifdef __GLIBCXX__
|
||||
template<typename T, typename U>
|
||||
struct tag<std::_Expr<T, U> >
|
||||
{
|
||||
typedef std_valarray_tag type;
|
||||
};
|
||||
#endif
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
// This is necessary because the GCC stdlib uses expression templates, and
|
||||
// typeof(som-valarray-expression) is not an instance of std::valarray
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(Name, Op) \
|
||||
template<typename Left, typename Right> \
|
||||
struct Name<Left, Right, std_valarray_tag, std_valarray_tag> \
|
||||
{ \
|
||||
typedef Left first_argument_type; \
|
||||
typedef Right second_argument_type; \
|
||||
typedef typename Left::value_type left_value_type; \
|
||||
typedef typename Right::value_type right_value_type; \
|
||||
typedef \
|
||||
std::valarray< \
|
||||
typename Name<left_value_type, right_value_type>::result_type \
|
||||
> \
|
||||
result_type; \
|
||||
result_type \
|
||||
operator ()(Left &left, Right &right) const \
|
||||
{ \
|
||||
return numeric::promote<std::valarray<left_value_type> >(left) \
|
||||
Op numeric::promote<std::valarray<right_value_type> >(right); \
|
||||
} \
|
||||
}; \
|
||||
template<typename Left, typename Right> \
|
||||
struct Name<Left, Right, std_valarray_tag, void> \
|
||||
{ \
|
||||
typedef Left first_argument_type; \
|
||||
typedef Right second_argument_type; \
|
||||
typedef typename Left::value_type left_value_type; \
|
||||
typedef \
|
||||
std::valarray< \
|
||||
typename Name<left_value_type, Right>::result_type \
|
||||
> \
|
||||
result_type; \
|
||||
result_type \
|
||||
operator ()(Left &left, Right &right) const \
|
||||
{ \
|
||||
return numeric::promote<std::valarray<left_value_type> >(left) Op right;\
|
||||
} \
|
||||
}; \
|
||||
template<typename Left, typename Right> \
|
||||
struct Name<Left, Right, void, std_valarray_tag> \
|
||||
{ \
|
||||
typedef Left first_argument_type; \
|
||||
typedef Right second_argument_type; \
|
||||
typedef typename Right::value_type right_value_type; \
|
||||
typedef \
|
||||
std::valarray< \
|
||||
typename Name<Left, right_value_type>::result_type \
|
||||
> \
|
||||
result_type; \
|
||||
result_type \
|
||||
operator ()(Left &left, Right &right) const \
|
||||
{ \
|
||||
return left Op numeric::promote<std::valarray<right_value_type> >(right);\
|
||||
} \
|
||||
};
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(plus, +)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(minus, -)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(multiplies, *)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(divides, /)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(modulus, %)
|
||||
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// element-wise min of std::valarray
|
||||
template<typename Left, typename Right>
|
||||
struct min_assign<Left, Right, std_valarray_tag, std_valarray_tag>
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
BOOST_ASSERT(left.size() == right.size());
|
||||
for(std::size_t i = 0, size = left.size(); i != size; ++i)
|
||||
{
|
||||
if(numeric::less(right[i], left[i]))
|
||||
{
|
||||
left[i] = right[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// element-wise max of std::valarray
|
||||
template<typename Left, typename Right>
|
||||
struct max_assign<Left, Right, std_valarray_tag, std_valarray_tag>
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
BOOST_ASSERT(left.size() == right.size());
|
||||
for(std::size_t i = 0, size = left.size(); i != size; ++i)
|
||||
{
|
||||
if(numeric::greater(right[i], left[i]))
|
||||
{
|
||||
left[i] = right[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// partial specialization of numeric::fdiv<> for std::valarray.
|
||||
template<typename Left, typename Right, typename RightTag>
|
||||
struct fdiv<Left, Right, std_valarray_tag, RightTag>
|
||||
: mpl::if_<
|
||||
are_integral<typename Left::value_type, Right>
|
||||
, divides<Left, double const>
|
||||
, divides<Left, Right>
|
||||
>::type
|
||||
{};
|
||||
|
||||
// promote
|
||||
template<typename To, typename From>
|
||||
struct promote<To, From, std_valarray_tag, std_valarray_tag>
|
||||
{
|
||||
typedef From argument_type;
|
||||
typedef To result_type;
|
||||
|
||||
To operator ()(From &arr) const
|
||||
{
|
||||
typename remove_const<To>::type res(arr.size());
|
||||
for(std::size_t i = 0, size = arr.size(); i != size; ++i)
|
||||
{
|
||||
res[i] = numeric::promote<typename To::value_type>(arr[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ToFrom>
|
||||
struct promote<ToFrom, ToFrom, std_valarray_tag, std_valarray_tag>
|
||||
{
|
||||
typedef ToFrom argument_type;
|
||||
typedef ToFrom result_type;
|
||||
|
||||
ToFrom &operator ()(ToFrom &tofrom) const
|
||||
{
|
||||
return tofrom;
|
||||
}
|
||||
};
|
||||
|
||||
// for "promoting" a std::valarray<bool> to a bool, useful for
|
||||
// comparing 2 valarrays for equality:
|
||||
// if(numeric::promote<bool>(a == b))
|
||||
template<typename From>
|
||||
struct promote<bool, From, void, std_valarray_tag>
|
||||
{
|
||||
typedef From argument_type;
|
||||
typedef bool result_type;
|
||||
|
||||
bool operator ()(From &arr) const
|
||||
{
|
||||
BOOST_MPL_ASSERT((is_same<bool, typename From::value_type>));
|
||||
for(std::size_t i = 0, size = arr.size(); i != size; ++i)
|
||||
{
|
||||
if(!arr[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename From>
|
||||
struct promote<bool const, From, void, std_valarray_tag>
|
||||
: promote<bool, From, void, std_valarray_tag>
|
||||
{};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_min
|
||||
template<typename T>
|
||||
struct as_min<T, std_valarray_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(numeric::as_min(arr[0]), arr.size());
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_max
|
||||
template<typename T>
|
||||
struct as_max<T, std_valarray_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(numeric::as_max(arr[0]), arr.size());
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_zero
|
||||
template<typename T>
|
||||
struct as_zero<T, std_valarray_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(numeric::as_zero(arr[0]), arr.size());
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_one
|
||||
template<typename T>
|
||||
struct as_one<T, std_valarray_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(numeric::as_one(arr[0]), arr.size());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace functional
|
||||
|
||||
}} // namespace boost::numeric
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file vector.hpp
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_NUMERIC_FUNCTIONAL_VECTOR_HPP_EAN_12_12_2005
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_VECTOR_HPP_EAN_12_12_2005
|
||||
|
||||
#ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
|
||||
# error Include this file before boost/accumulators/numeric/functional.hpp
|
||||
#endif
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/mpl/and.hpp>
|
||||
#include <boost/mpl/not.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_scalar.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/typeof/std/vector.hpp>
|
||||
#include <boost/accumulators/numeric/functional_fwd.hpp>
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
namespace operators
|
||||
{
|
||||
namespace acc_detail
|
||||
{
|
||||
template<typename Fun>
|
||||
struct make_vector
|
||||
{
|
||||
typedef std::vector<typename Fun::result_type> type;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> / Right where Right is a scalar.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_enable_if<
|
||||
is_scalar<Right>
|
||||
, acc_detail::make_vector<functional::divides<Left, Right> >
|
||||
>::type
|
||||
operator /(std::vector<Left> const &left, Right const &right)
|
||||
{
|
||||
typedef typename functional::divides<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::divides(left[i], right);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> / vector<Right>.
|
||||
template<typename Left, typename Right>
|
||||
std::vector<typename functional::divides<Left, Right>::result_type>
|
||||
operator /(std::vector<Left> const &left, std::vector<Right> const &right)
|
||||
{
|
||||
typedef typename functional::divides<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::divides(left[i], right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> * Right where Right is a scalar.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_enable_if<
|
||||
is_scalar<Right>
|
||||
, acc_detail::make_vector<functional::multiplies<Left, Right> >
|
||||
>::type
|
||||
operator *(std::vector<Left> const &left, Right const &right)
|
||||
{
|
||||
typedef typename functional::multiplies<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::multiplies(left[i], right);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle Left * vector<Right> where Left is a scalar.
|
||||
template<typename Left, typename Right>
|
||||
typename lazy_enable_if<
|
||||
is_scalar<Left>
|
||||
, acc_detail::make_vector<functional::multiplies<Left, Right> >
|
||||
>::type
|
||||
operator *(Left const &left, std::vector<Right> const &right)
|
||||
{
|
||||
typedef typename functional::multiplies<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(right.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::multiplies(left, right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> * vector<Right>
|
||||
template<typename Left, typename Right>
|
||||
std::vector<typename functional::multiplies<Left, Right>::result_type>
|
||||
operator *(std::vector<Left> const &left, std::vector<Right> const &right)
|
||||
{
|
||||
typedef typename functional::multiplies<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::multiplies(left[i], right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> + vector<Right>
|
||||
template<typename Left, typename Right>
|
||||
std::vector<typename functional::plus<Left, Right>::result_type>
|
||||
operator +(std::vector<Left> const &left, std::vector<Right> const &right)
|
||||
{
|
||||
typedef typename functional::plus<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::plus(left[i], right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> - vector<Right>
|
||||
template<typename Left, typename Right>
|
||||
std::vector<typename functional::minus<Left, Right>::result_type>
|
||||
operator -(std::vector<Left> const &left, std::vector<Right> const &right)
|
||||
{
|
||||
typedef typename functional::minus<Left, Right>::result_type value_type;
|
||||
std::vector<value_type> result(left.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::minus(left[i], right[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle vector<Left> += vector<Left>
|
||||
template<typename Left>
|
||||
std::vector<Left> &
|
||||
operator +=(std::vector<Left> &left, std::vector<Left> const &right)
|
||||
{
|
||||
BOOST_ASSERT(left.size() == right.size());
|
||||
for(std::size_t i = 0, size = left.size(); i != size; ++i)
|
||||
{
|
||||
numeric::plus_assign(left[i], right[i]);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Handle -vector<Arg>
|
||||
template<typename Arg>
|
||||
std::vector<typename functional::unary_minus<Arg>::result_type>
|
||||
operator -(std::vector<Arg> const &arg)
|
||||
{
|
||||
typedef typename functional::unary_minus<Arg>::result_type value_type;
|
||||
std::vector<value_type> result(arg.size());
|
||||
for(std::size_t i = 0, size = result.size(); i != size; ++i)
|
||||
{
|
||||
result[i] = numeric::unary_minus(arg[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
namespace functional
|
||||
{
|
||||
struct std_vector_tag;
|
||||
|
||||
template<typename T, typename Al>
|
||||
struct tag<std::vector<T, Al> >
|
||||
{
|
||||
typedef std_vector_tag type;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// element-wise min of std::vector
|
||||
template<typename Left, typename Right>
|
||||
struct min_assign<Left, Right, std_vector_tag, std_vector_tag>
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
BOOST_ASSERT(left.size() == right.size());
|
||||
for(std::size_t i = 0, size = left.size(); i != size; ++i)
|
||||
{
|
||||
if(numeric::less(right[i], left[i]))
|
||||
{
|
||||
left[i] = right[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// element-wise max of std::vector
|
||||
template<typename Left, typename Right>
|
||||
struct max_assign<Left, Right, std_vector_tag, std_vector_tag>
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef void result_type;
|
||||
|
||||
void operator ()(Left &left, Right &right) const
|
||||
{
|
||||
BOOST_ASSERT(left.size() == right.size());
|
||||
for(std::size_t i = 0, size = left.size(); i != size; ++i)
|
||||
{
|
||||
if(numeric::greater(right[i], left[i]))
|
||||
{
|
||||
left[i] = right[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// partial specialization for std::vector.
|
||||
template<typename Left, typename Right>
|
||||
struct fdiv<Left, Right, std_vector_tag, void>
|
||||
: mpl::if_<
|
||||
are_integral<typename Left::value_type, Right>
|
||||
, divides<Left, double const>
|
||||
, divides<Left, Right>
|
||||
>::type
|
||||
{};
|
||||
|
||||
// promote
|
||||
template<typename To, typename From>
|
||||
struct promote<To, From, std_vector_tag, std_vector_tag>
|
||||
{
|
||||
typedef From argument_type;
|
||||
typedef To result_type;
|
||||
|
||||
To operator ()(From &arr) const
|
||||
{
|
||||
typename remove_const<To>::type res(arr.size());
|
||||
for(std::size_t i = 0, size = arr.size(); i != size; ++i)
|
||||
{
|
||||
res[i] = numeric::promote<typename To::value_type>(arr[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ToFrom>
|
||||
struct promote<ToFrom, ToFrom, std_vector_tag, std_vector_tag>
|
||||
{
|
||||
typedef ToFrom argument_type;
|
||||
typedef ToFrom result_type;
|
||||
|
||||
ToFrom &operator ()(ToFrom &tofrom) const
|
||||
{
|
||||
return tofrom;
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_min
|
||||
template<typename T>
|
||||
struct as_min<T, std_vector_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(arr.size(), numeric::as_min(arr[0]));
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_max
|
||||
template<typename T>
|
||||
struct as_max<T, std_vector_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(arr.size(), numeric::as_max(arr[0]));
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_zero
|
||||
template<typename T>
|
||||
struct as_zero<T, std_vector_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(arr.size(), numeric::as_zero(arr[0]));
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::as_one
|
||||
template<typename T>
|
||||
struct as_one<T, std_vector_tag>
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef typename remove_const<T>::type result_type;
|
||||
|
||||
typename remove_const<T>::type operator ()(T &arr) const
|
||||
{
|
||||
return 0 == arr.size()
|
||||
? T()
|
||||
: T(arr.size(), numeric::as_one(arr[0]));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace functional
|
||||
|
||||
}} // namespace boost::numeric
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file functional_fwd.hpp
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_NUMERIC_FUNCTIONAL_FWD_HPP_EAN_08_12_2005
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_FWD_HPP_EAN_08_12_2005
|
||||
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_const.hpp>
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
// For using directives -- this namespace may be re-opened elsewhere
|
||||
namespace operators
|
||||
{}
|
||||
|
||||
namespace op
|
||||
{
|
||||
using mpl::_;
|
||||
using mpl::_1;
|
||||
using mpl::_2;
|
||||
}
|
||||
|
||||
namespace functional
|
||||
{
|
||||
using namespace operators;
|
||||
|
||||
template<typename T>
|
||||
struct tag
|
||||
{
|
||||
typedef void type;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct tag<T const>
|
||||
: tag<T>
|
||||
{};
|
||||
|
||||
template<typename T>
|
||||
struct tag<T volatile>
|
||||
: tag<T>
|
||||
{};
|
||||
|
||||
template<typename T>
|
||||
struct tag<T const volatile>
|
||||
: tag<T>
|
||||
{};
|
||||
|
||||
template<typename T>
|
||||
struct static_;
|
||||
|
||||
template<typename A0, typename A1>
|
||||
struct are_integral;
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP(Name, Op) \
|
||||
namespace functional \
|
||||
{ \
|
||||
template<typename Arg, typename EnableIf = void> \
|
||||
struct Name ## _base; \
|
||||
template<typename Arg, typename ArgTag = typename tag<Arg>::type> \
|
||||
struct Name; \
|
||||
} \
|
||||
namespace op \
|
||||
{ \
|
||||
struct Name; \
|
||||
} \
|
||||
namespace \
|
||||
{ \
|
||||
extern op::Name const &Name; \
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
#define BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(Name) \
|
||||
namespace functional \
|
||||
{ \
|
||||
template<typename Left, typename Right, typename EnableIf = void> \
|
||||
struct result_of_ ## Name; \
|
||||
template<typename Left, typename Right, typename EnableIf = void> \
|
||||
struct Name ## _base; \
|
||||
template< \
|
||||
typename Left \
|
||||
, typename Right \
|
||||
, typename LeftTag = typename tag<Left>::type \
|
||||
, typename RightTag = typename tag<Right>::type \
|
||||
> \
|
||||
struct Name; \
|
||||
} \
|
||||
namespace op \
|
||||
{ \
|
||||
struct Name; \
|
||||
} \
|
||||
namespace \
|
||||
{ \
|
||||
extern op::Name const &Name; \
|
||||
}
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(plus)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(minus)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(multiplies)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(divides)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(modulus)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(greater)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(greater_equal)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(less)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(less_equal)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(equal_to)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(not_equal_to)
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(assign)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(plus_assign)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(minus_assign)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(multiplies_assign)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(divides_assign)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP(modulus_assign)
|
||||
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP(unary_plus, +)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP(unary_minus, -)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP(complement, ~)
|
||||
BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP(logical_not, !)
|
||||
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DECLARE_UNARY_OP
|
||||
#undef BOOST_NUMERIC_FUNCTIONAL_DECLARE_BINARY_OP
|
||||
|
||||
|
||||
namespace functional
|
||||
{
|
||||
template<typename To, typename From, typename EnableIf = void>
|
||||
struct promote_base;
|
||||
template<typename Left, typename Right, typename EnableIf = void>
|
||||
struct min_assign_base;
|
||||
template<typename Left, typename Right, typename EnableIf = void>
|
||||
struct max_assign_base;
|
||||
template<typename Left, typename Right, typename EnableIf = void>
|
||||
struct fdiv_base;
|
||||
template<typename Arg, typename EnableIf = void>
|
||||
struct as_min_base;
|
||||
template<typename Arg, typename EnableIf = void>
|
||||
struct as_max_base;
|
||||
template<typename Arg, typename EnableIf = void>
|
||||
struct as_zero_base;
|
||||
template<typename Arg, typename EnableIf = void>
|
||||
struct as_one_base;
|
||||
|
||||
template<typename To, typename From, typename ToTag = typename tag<To>::type, typename FromTag = typename tag<From>::type>
|
||||
struct promote;
|
||||
template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
|
||||
struct min_assign;
|
||||
template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
|
||||
struct max_assign;
|
||||
template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
|
||||
struct fdiv;
|
||||
template<typename Arg, typename Tag = typename tag<Arg>::type>
|
||||
struct as_min;
|
||||
template<typename Arg, typename Tag = typename tag<Arg>::type>
|
||||
struct as_max;
|
||||
template<typename Arg, typename Tag = typename tag<Arg>::type>
|
||||
struct as_zero;
|
||||
template<typename Arg, typename Tag = typename tag<Arg>::type>
|
||||
struct as_one;
|
||||
}
|
||||
|
||||
namespace op
|
||||
{
|
||||
template<typename To>
|
||||
struct promote;
|
||||
struct min_assign;
|
||||
struct max_assign;
|
||||
struct fdiv;
|
||||
struct as_min;
|
||||
struct as_max;
|
||||
struct as_zero;
|
||||
struct as_one;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
extern op::min_assign const &min_assign;
|
||||
extern op::max_assign const &max_assign;
|
||||
extern op::fdiv const &fdiv;
|
||||
extern op::as_min const &as_min;
|
||||
extern op::as_max const &as_max;
|
||||
extern op::as_zero const &as_zero;
|
||||
extern op::as_one const &as_one;
|
||||
}
|
||||
|
||||
template<typename To, typename From>
|
||||
typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
|
||||
promote(From &from);
|
||||
|
||||
template<typename To, typename From>
|
||||
typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
|
||||
promote(From const &from);
|
||||
|
||||
template<typename T>
|
||||
struct default_;
|
||||
|
||||
template<typename T>
|
||||
struct one;
|
||||
|
||||
template<typename T>
|
||||
struct zero;
|
||||
|
||||
template<typename T>
|
||||
struct one_or_default;
|
||||
|
||||
template<typename T>
|
||||
struct zero_or_default;
|
||||
|
||||
}} // namespace boost::numeric
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file statistics.hpp
|
||||
/// Includes all of the Statistical Accumulators Library
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_HPP_EAN_01_17_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_HPP_EAN_01_17_2006
|
||||
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/covariance.hpp>
|
||||
#include <boost/accumulators/statistics/density.hpp>
|
||||
#include <boost/accumulators/statistics/error_of.hpp>
|
||||
#include <boost/accumulators/statistics/error_of_mean.hpp>
|
||||
#include <boost/accumulators/statistics/extended_p_square.hpp>
|
||||
#include <boost/accumulators/statistics/extended_p_square_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/kurtosis.hpp>
|
||||
#include <boost/accumulators/statistics/max.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
#include <boost/accumulators/statistics/median.hpp>
|
||||
#include <boost/accumulators/statistics/min.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp>
|
||||
#include <boost/accumulators/statistics/peaks_over_threshold.hpp>
|
||||
#include <boost/accumulators/statistics/pot_tail_mean.hpp>
|
||||
#include <boost/accumulators/statistics/pot_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/p_square_cumul_dist.hpp>
|
||||
#include <boost/accumulators/statistics/p_square_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/skewness.hpp>
|
||||
#include <boost/accumulators/statistics/stats.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/sum_kahan.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/accumulators/statistics/tail_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/tail_mean.hpp>
|
||||
#include <boost/accumulators/statistics/tail_variate.hpp>
|
||||
#include <boost/accumulators/statistics/tail_variate_means.hpp>
|
||||
#include <boost/accumulators/statistics/variance.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_covariance.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_density.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_kurtosis.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_extended_p_square.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_mean.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_median.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_moment.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_p_square_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_skewness.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_sum.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_sum_kahan.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_tail_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_tail_mean.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_tail_variate_means.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_variance.hpp>
|
||||
#include <boost/accumulators/statistics/with_error.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
#include <boost/accumulators/statistics/variates/covariate.hpp>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// count.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_COUNT_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_COUNT_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/always.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// count_impl
|
||||
struct count_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef std::size_t result_type;
|
||||
|
||||
count_impl(dont_care)
|
||||
: cnt(0)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
++this->cnt;
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->cnt;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & cnt;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t cnt;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::count
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct count
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::count_impl> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::count
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::count> const count = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(count)
|
||||
}
|
||||
|
||||
using extract::count;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,219 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// covariance.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_COVARIANCE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_COVARIANCE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <complex>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/numeric/ublas/io.hpp>
|
||||
#include <boost/numeric/ublas/matrix.hpp>
|
||||
#include <boost/type_traits/is_scalar.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
namespace functional
|
||||
{
|
||||
struct std_vector_tag;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// functional::outer_product
|
||||
template<typename Left, typename Right, typename EnableIf = void>
|
||||
struct outer_product_base
|
||||
: functional::multiplies<Left, Right>
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
|
||||
struct outer_product
|
||||
: outer_product_base<Left, Right, void>
|
||||
{};
|
||||
|
||||
template<typename Left, typename Right>
|
||||
struct outer_product<Left, Right, std_vector_tag, std_vector_tag>
|
||||
{
|
||||
typedef Left first_argument_type;
|
||||
typedef Right second_argument_type;
|
||||
typedef
|
||||
ublas::matrix<
|
||||
typename functional::multiplies<
|
||||
typename Left::value_type
|
||||
, typename Right::value_type
|
||||
>::result_type
|
||||
>
|
||||
result_type;
|
||||
|
||||
result_type
|
||||
operator ()(Left & left, Right & right) const
|
||||
{
|
||||
std::size_t left_size = left.size();
|
||||
std::size_t right_size = right.size();
|
||||
result_type result(left_size, right_size);
|
||||
for (std::size_t i = 0; i < left_size; ++i)
|
||||
for (std::size_t j = 0; j < right_size; ++j)
|
||||
result(i,j) = numeric::multiplies(left[i], right[j]);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace op
|
||||
{
|
||||
struct outer_product
|
||||
: boost::detail::function2<functional::outer_product<_1, _2, functional::tag<_1>, functional::tag<_2> > >
|
||||
{};
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
op::outer_product const &outer_product = boost::detail::pod_singleton<op::outer_product>::instance;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// covariance_impl
|
||||
//
|
||||
/**
|
||||
@brief Covariance Estimator
|
||||
|
||||
An iterative Monte Carlo estimator for the covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
|
||||
and \f$X'\f$ is a variate, is given by:
|
||||
|
||||
\f[
|
||||
\hat{c}_n = \frac{n-1}{n} \hat{c}_{n-1} + \frac{1}{n-1}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),\quad n\ge2,\quad\hat{c}_1 = 0,
|
||||
\f]
|
||||
|
||||
\f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the means of the samples and variates.
|
||||
*/
|
||||
template<typename Sample, typename VariateType, typename VariateTag>
|
||||
struct covariance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type sample_type;
|
||||
typedef typename numeric::functional::fdiv<VariateType, std::size_t>::result_type variate_type;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::outer_product<sample_type, variate_type>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
covariance_impl(Args const &args)
|
||||
: cov_(
|
||||
numeric::outer_product(
|
||||
numeric::fdiv(args[sample | Sample()], (std::size_t)1)
|
||||
, numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
|
||||
)
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
if (cnt > 1)
|
||||
{
|
||||
extractor<tag::mean_of_variates<VariateType, VariateTag> > const some_mean_of_variates = {};
|
||||
|
||||
this->cov_ = this->cov_*(cnt-1.)/cnt
|
||||
+ numeric::outer_product(
|
||||
some_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
|
||||
, mean(args) - args[sample]
|
||||
) / (cnt-1.);
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->cov_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & cov_;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type cov_;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::covariance
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct covariance
|
||||
: depends_on<count, mean, mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
typedef accumulators::impl::covariance_impl<mpl::_1, VariateType, VariateTag> impl;
|
||||
};
|
||||
|
||||
struct abstract_covariance
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::covariance
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_covariance> const covariance = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariance)
|
||||
}
|
||||
|
||||
using extract::covariance;
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::covariance<VariateType, VariateTag> >
|
||||
: feature_of<tag::abstract_covariance>
|
||||
{
|
||||
};
|
||||
|
||||
// So that covariance can be automatically substituted with
|
||||
// weighted_covariance when the weight parameter is non-void.
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_weighted_feature<tag::covariance<VariateType, VariateTag> >
|
||||
{
|
||||
typedef tag::weighted_covariance<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::weighted_covariance<VariateType, VariateTag> >
|
||||
: feature_of<tag::covariance<VariateType, VariateTag> >
|
||||
{};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,266 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// density.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_DENSITY_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_DENSITY_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/max.hpp>
|
||||
#include <boost/accumulators/statistics/min.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
#include <boost/serialization/utility.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// cache_size and num_bins named parameters
|
||||
//
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, density_cache_size, cache_size)
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, density_num_bins, num_bins)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_cache_size)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_num_bins)
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// density_impl
|
||||
// density histogram
|
||||
/**
|
||||
@brief Histogram density estimator
|
||||
|
||||
The histogram density estimator returns a histogram of the sample distribution. The positions and sizes of the bins
|
||||
are determined using a specifiable number of cached samples (cache_size). The range between the minimum and the
|
||||
maximum of the cached samples is subdivided into a specifiable number of bins (num_bins) of same size. Additionally,
|
||||
an under- and an overflow bin is added to capture future under- and overflow samples. Once the bins are determined,
|
||||
the cached samples and all subsequent samples are added to the correct bins. At the end, a range of std::pair is
|
||||
return, where each pair contains the position of the bin (lower bound) and the samples count (normalized with the
|
||||
total number of samples).
|
||||
|
||||
@param density_cache_size Number of first samples used to determine min and max.
|
||||
@param density_num_bins Number of bins (two additional bins collect under- and overflow samples).
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct density_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<typename histogram_type::iterator> result_type;
|
||||
|
||||
template<typename Args>
|
||||
density_impl(Args const &args)
|
||||
: cache_size(args[density_cache_size])
|
||||
, cache(cache_size)
|
||||
, num_bins(args[density_num_bins])
|
||||
, samples_in_bin(num_bins + 2, 0.)
|
||||
, bin_positions(num_bins + 2)
|
||||
, histogram(
|
||||
num_bins + 2
|
||||
, std::make_pair(
|
||||
numeric::fdiv(args[sample | Sample()],(std::size_t)1)
|
||||
, numeric::fdiv(args[sample | Sample()],(std::size_t)1)
|
||||
)
|
||||
)
|
||||
, is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// Fill up cache with cache_size first samples
|
||||
if (cnt <= this->cache_size)
|
||||
{
|
||||
this->cache[cnt - 1] = args[sample];
|
||||
}
|
||||
|
||||
// Once cache_size samples have been accumulated, create num_bins bins of same size between
|
||||
// the minimum and maximum of the cached samples as well as under and overflow bins.
|
||||
// Store their lower bounds (bin_positions) and fill the bins with the cached samples (samples_in_bin).
|
||||
if (cnt == this->cache_size)
|
||||
{
|
||||
float_type minimum = numeric::fdiv((min)(args), (std::size_t)1);
|
||||
float_type maximum = numeric::fdiv((max)(args), (std::size_t)1);
|
||||
float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins );
|
||||
|
||||
// determine bin positions (their lower bounds)
|
||||
for (std::size_t i = 0; i < this->num_bins + 2; ++i)
|
||||
{
|
||||
this->bin_positions[i] = minimum + (i - 1.) * bin_size;
|
||||
}
|
||||
|
||||
for (typename array_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
|
||||
{
|
||||
if (*iter < this->bin_positions[1])
|
||||
{
|
||||
++(this->samples_in_bin[0]);
|
||||
}
|
||||
else if (*iter >= this->bin_positions[this->num_bins + 1])
|
||||
{
|
||||
++(this->samples_in_bin[this->num_bins + 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it = std::upper_bound(
|
||||
this->bin_positions.begin()
|
||||
, this->bin_positions.end()
|
||||
, *iter
|
||||
);
|
||||
|
||||
std::size_t d = std::distance(this->bin_positions.begin(), it);
|
||||
++(this->samples_in_bin[d - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add each subsequent sample to the correct bin
|
||||
else if (cnt > this->cache_size)
|
||||
{
|
||||
if (args[sample] < this->bin_positions[1])
|
||||
{
|
||||
++(this->samples_in_bin[0]);
|
||||
}
|
||||
else if (args[sample] >= this->bin_positions[this->num_bins + 1])
|
||||
{
|
||||
++(this->samples_in_bin[this->num_bins + 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it = std::upper_bound(
|
||||
this->bin_positions.begin()
|
||||
, this->bin_positions.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
std::size_t d = std::distance(this->bin_positions.begin(), it);
|
||||
++(this->samples_in_bin[d - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@pre The number of samples must meet or exceed the cache size
|
||||
*/
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
// creates a vector of std::pair where each pair i holds
|
||||
// the values bin_positions[i] (x-axis of histogram) and
|
||||
// samples_in_bin[i] / cnt (y-axis of histogram).
|
||||
|
||||
for (std::size_t i = 0; i < this->num_bins + 2; ++i)
|
||||
{
|
||||
this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], count(args)));
|
||||
}
|
||||
}
|
||||
// returns a range of pairs
|
||||
return make_iterator_range(this->histogram);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO split to save/load and check on parameters provided in ctor
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & cache_size;
|
||||
ar & cache;
|
||||
ar & num_bins;
|
||||
ar & samples_in_bin;
|
||||
ar & bin_positions;
|
||||
ar & histogram;
|
||||
ar & is_dirty;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t cache_size; // number of cached samples
|
||||
array_type cache; // cache to store the first cache_size samples
|
||||
std::size_t num_bins; // number of bins
|
||||
array_type samples_in_bin; // number of samples in each bin
|
||||
array_type bin_positions; // lower bounds of bins
|
||||
mutable histogram_type histogram; // histogram
|
||||
mutable bool is_dirty;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::density
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct density
|
||||
: depends_on<count, min, max>
|
||||
, density_cache_size
|
||||
, density_num_bins
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::density_impl<mpl::_1> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::density::cache_size named parameter
|
||||
/// tag::density::num_bins named parameter
|
||||
static boost::parameter::keyword<density_cache_size> const cache_size;
|
||||
static boost::parameter::keyword<density_num_bins> const num_bins;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::density
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::density> const density = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(density)
|
||||
}
|
||||
|
||||
using extract::density;
|
||||
|
||||
// So that density can be automatically substituted
|
||||
// with weighted_density when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::density>
|
||||
{
|
||||
typedef tag::weighted_density type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_density>
|
||||
: feature_of<tag::density>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,99 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// error_of.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ERROR_OF_HPP_EAN_29_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ERROR_OF_HPP_EAN_29_11_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Feature>
|
||||
struct this_feature_has_no_error_calculation
|
||||
: mpl::false_
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// error_of_impl
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Sample, typename Feature>
|
||||
struct error_of_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// TODO: specialize this on the specific features that have errors we're
|
||||
// interested in.
|
||||
BOOST_MPL_ASSERT((this_feature_has_no_error_calculation<Feature>));
|
||||
|
||||
// for boost::result_of
|
||||
typedef int result_type;
|
||||
|
||||
error_of_impl(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::error_of
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename Feature>
|
||||
struct error_of
|
||||
: depends_on<Feature>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::error_of_impl<mpl::_1, Feature> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::error_of
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, error_of, (typename))
|
||||
}
|
||||
|
||||
using extract::error_of;
|
||||
|
||||
// make tag::error_of<tag::feature(modifier)> work
|
||||
template<typename Feature>
|
||||
struct as_feature<tag::error_of<Feature> >
|
||||
{
|
||||
typedef tag::error_of<typename as_feature<Feature>::type> type;
|
||||
};
|
||||
|
||||
// make error_of<tag::mean> work with non-void weights (should become
|
||||
// error_of<tag::weighted_mean>
|
||||
template<typename Feature>
|
||||
struct as_weighted_feature<tag::error_of<Feature> >
|
||||
{
|
||||
typedef tag::error_of<typename as_weighted_feature<Feature>::type> type;
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// error_of.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ERROR_OF_MEAN_HPP_EAN_27_03_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ERROR_OF_MEAN_HPP_EAN_27_03_2006
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/error_of.hpp>
|
||||
#include <boost/accumulators/statistics/variance.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// error_of_mean_impl
|
||||
template<typename Sample, typename Variance>
|
||||
struct error_of_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
error_of_mean_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
using namespace std;
|
||||
extractor<Variance> const variance = {};
|
||||
return sqrt(numeric::fdiv(variance(args), count(args) - 1));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::error_of
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<>
|
||||
struct error_of<mean>
|
||||
: depends_on<lazy_variance, count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::error_of_mean_impl<mpl::_1, lazy_variance> impl;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct error_of<immediate_mean>
|
||||
: depends_on<variance, count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::error_of_mean_impl<mpl::_1, variance> impl;
|
||||
};
|
||||
}
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,309 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extended_p_square.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_EXTENDED_SINGLE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_EXTENDED_SINGLE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/times2_iterator.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// probabilities named parameter
|
||||
//
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, extended_p_square_probabilities, probabilities)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_probabilities)
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extended_p_square_impl
|
||||
// multiple quantile estimation
|
||||
/**
|
||||
@brief Multiple quantile estimation with the extended \f$P^2\f$ algorithm
|
||||
|
||||
Extended \f$P^2\f$ algorithm for estimation of several quantiles without storing samples.
|
||||
Assume that \f$m\f$ quantiles \f$\xi_{p_1}, \ldots, \xi_{p_m}\f$ are to be estimated.
|
||||
Instead of storing the whole sample cumulative distribution, the algorithm maintains only
|
||||
\f$m+2\f$ principal markers and \f$m+1\f$ middle markers, whose positions are updated
|
||||
with each sample and whose heights are adjusted (if necessary) using a piecewise-parablic
|
||||
formula. The heights of these central markers are the current estimates of the quantiles
|
||||
and returned as an iterator range.
|
||||
|
||||
For further details, see
|
||||
|
||||
K. E. E. Raatikainen, Simultaneous estimation of several quantiles, Simulation, Volume 49,
|
||||
Number 4 (October), 1986, p. 159-164.
|
||||
|
||||
The extended \f$ P^2 \f$ algorithm generalizes the \f$ P^2 \f$ algorithm of
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param extended_p_square_probabilities A vector of quantile probabilities.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct extended_p_square_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<
|
||||
detail::lvalue_index_iterator<
|
||||
permutation_iterator<
|
||||
typename array_type::const_iterator
|
||||
, detail::times2_iterator
|
||||
>
|
||||
>
|
||||
> result_type;
|
||||
|
||||
template<typename Args>
|
||||
extended_p_square_impl(Args const &args)
|
||||
: probabilities(
|
||||
boost::begin(args[extended_p_square_probabilities])
|
||||
, boost::end(args[extended_p_square_probabilities])
|
||||
)
|
||||
, heights(2 * probabilities.size() + 3)
|
||||
, actual_positions(heights.size())
|
||||
, desired_positions(heights.size())
|
||||
, positions_increments(heights.size())
|
||||
{
|
||||
std::size_t num_quantiles = this->probabilities.size();
|
||||
std::size_t num_markers = this->heights.size();
|
||||
|
||||
for(std::size_t i = 0; i < num_markers; ++i)
|
||||
{
|
||||
this->actual_positions[i] = i + 1;
|
||||
}
|
||||
|
||||
this->positions_increments[0] = 0.;
|
||||
this->positions_increments[num_markers - 1] = 1.;
|
||||
|
||||
for(std::size_t i = 0; i < num_quantiles; ++i)
|
||||
{
|
||||
this->positions_increments[2 * i + 2] = probabilities[i];
|
||||
}
|
||||
|
||||
for(std::size_t i = 0; i <= num_quantiles; ++i)
|
||||
{
|
||||
this->positions_increments[2 * i + 1] =
|
||||
0.5 * (this->positions_increments[2 * i] + this->positions_increments[2 * i + 2]);
|
||||
}
|
||||
|
||||
for(std::size_t i = 0; i < num_markers; ++i)
|
||||
{
|
||||
this->desired_positions[i] = 1. + 2. * (num_quantiles + 1.) * this->positions_increments[i];
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// m+2 principal markers and m+1 middle markers
|
||||
std::size_t num_markers = 2 * this->probabilities.size() + 3;
|
||||
|
||||
// first accumulate num_markers samples
|
||||
if(cnt <= num_markers)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
|
||||
// complete the initialization of heights by sorting
|
||||
if(cnt == num_markers)
|
||||
{
|
||||
std::sort(this->heights.begin(), this->heights.end());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::size_t sample_cell = 1;
|
||||
|
||||
// find cell k = sample_cell such that heights[k-1] <= sample < heights[k]
|
||||
if(args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if(args[sample] >= this->heights[num_markers - 1])
|
||||
{
|
||||
this->heights[num_markers - 1] = args[sample];
|
||||
sample_cell = num_markers - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef typename array_type::iterator iterator;
|
||||
iterator it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// update actual positions of all markers above sample_cell index
|
||||
for(std::size_t i = sample_cell; i < num_markers; ++i)
|
||||
{
|
||||
++this->actual_positions[i];
|
||||
}
|
||||
|
||||
// update desired positions of all markers
|
||||
for(std::size_t i = 0; i < num_markers; ++i)
|
||||
{
|
||||
this->desired_positions[i] += this->positions_increments[i];
|
||||
}
|
||||
|
||||
// adjust heights and actual positions of markers 1 to num_markers-2 if necessary
|
||||
for(std::size_t i = 1; i <= num_markers - 2; ++i)
|
||||
{
|
||||
// offset to desired position
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i+1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i-1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i+1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i-1] - this->heights[i]) / dm;
|
||||
|
||||
if((d >= 1 && dp > 1) || (d <= -1 && dm < -1))
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm)*hp
|
||||
+ (dp - sign_d) * hm);
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
if(this->heights[i - 1] < h && h < this->heights[i + 1])
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if(d > 0)
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if(d < 0)
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
// for i in [1,probabilities.size()], return heights[i * 2]
|
||||
detail::times2_iterator idx_begin = detail::make_times2_iterator(1);
|
||||
detail::times2_iterator idx_end = detail::make_times2_iterator(this->probabilities.size() + 1);
|
||||
|
||||
return result_type(
|
||||
make_permutation_iterator(this->heights.begin(), idx_begin)
|
||||
, make_permutation_iterator(this->heights.begin(), idx_end)
|
||||
);
|
||||
}
|
||||
|
||||
public:
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that the parameters did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & probabilities;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
ar & positions_increments;
|
||||
}
|
||||
|
||||
private:
|
||||
array_type probabilities; // the quantile probabilities
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // d_i
|
||||
array_type positions_increments; // f_i
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::extended_p_square
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct extended_p_square
|
||||
: depends_on<count>
|
||||
, extended_p_square_probabilities
|
||||
{
|
||||
typedef accumulators::impl::extended_p_square_impl<mpl::_1> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::extended_p_square::probabilities named parameter
|
||||
static boost::parameter::keyword<tag::probabilities> const probabilities;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::extended_p_square
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::extended_p_square> const extended_p_square = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square)
|
||||
}
|
||||
|
||||
using extract::extended_p_square;
|
||||
|
||||
// So that extended_p_square can be automatically substituted with
|
||||
// weighted_extended_p_square when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::extended_p_square>
|
||||
{
|
||||
typedef tag::weighted_extended_p_square type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_extended_p_square>
|
||||
: feature_of<tag::extended_p_square>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,331 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extended_p_square_quantile.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_EXTENDED_SINGLE_QUANTILE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_EXTENDED_SINGLE_QUANTILE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
#include <boost/accumulators/statistics/extended_p_square.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_extended_p_square.hpp>
|
||||
#include <boost/accumulators/statistics/times2_iterator.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extended_p_square_quantile_impl
|
||||
// single quantile estimation
|
||||
/**
|
||||
@brief Quantile estimation using the extended \f$P^2\f$ algorithm for weighted and unweighted samples
|
||||
|
||||
Uses the quantile estimates calculated by the extended \f$P^2\f$ algorithm to compute
|
||||
intermediate quantile estimates by means of quadratic interpolation.
|
||||
|
||||
@param quantile_probability The probability of the quantile to be estimated.
|
||||
*/
|
||||
template<typename Sample, typename Impl1, typename Impl2> // Impl1: weighted/unweighted // Impl2: linear/quadratic
|
||||
struct extended_p_square_quantile_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
typedef iterator_range<
|
||||
detail::lvalue_index_iterator<
|
||||
permutation_iterator<
|
||||
typename array_type::const_iterator
|
||||
, detail::times2_iterator
|
||||
>
|
||||
>
|
||||
> range_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
extended_p_square_quantile_impl(Args const &args)
|
||||
: probabilities(
|
||||
boost::begin(args[extended_p_square_probabilities])
|
||||
, boost::end(args[extended_p_square_probabilities])
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<Impl1, weighted>
|
||||
, tag::weighted_extended_p_square
|
||||
, tag::extended_p_square
|
||||
>::type
|
||||
extended_p_square_tag;
|
||||
|
||||
extractor<extended_p_square_tag> const some_extended_p_square = {};
|
||||
|
||||
array_type heights(some_extended_p_square(args).size());
|
||||
std::copy(some_extended_p_square(args).begin(), some_extended_p_square(args).end(), heights.begin());
|
||||
|
||||
this->probability = args[quantile_probability];
|
||||
|
||||
typename array_type::const_iterator iter_probs = std::lower_bound(this->probabilities.begin(), this->probabilities.end(), this->probability);
|
||||
std::size_t dist = std::distance(this->probabilities.begin(), iter_probs);
|
||||
typename array_type::const_iterator iter_heights = heights.begin() + dist;
|
||||
|
||||
// If this->probability is not in a valid range return NaN or throw exception
|
||||
if (this->probability < *this->probabilities.begin() || this->probability > *(this->probabilities.end() - 1))
|
||||
{
|
||||
if (std::numeric_limits<result_type>::has_quiet_NaN)
|
||||
{
|
||||
return std::numeric_limits<result_type>::quiet_NaN();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "probability = " << this->probability << " is not in valid range (";
|
||||
msg << *this->probabilities.begin() << ", " << *(this->probabilities.end() - 1) << ")";
|
||||
boost::throw_exception(std::runtime_error(msg.str()));
|
||||
return Sample(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (*iter_probs == this->probability)
|
||||
{
|
||||
return heights[dist];
|
||||
}
|
||||
else
|
||||
{
|
||||
result_type res;
|
||||
|
||||
if (is_same<Impl2, linear>::value)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// LINEAR INTERPOLATION
|
||||
//
|
||||
float_type p1 = *iter_probs;
|
||||
float_type p0 = *(iter_probs - 1);
|
||||
float_type h1 = *iter_heights;
|
||||
float_type h0 = *(iter_heights - 1);
|
||||
|
||||
float_type a = numeric::fdiv(h1 - h0, p1 - p0);
|
||||
float_type b = h1 - p1 * a;
|
||||
|
||||
res = a * this->probability + b;
|
||||
}
|
||||
else
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// QUADRATIC INTERPOLATION
|
||||
//
|
||||
float_type p0, p1, p2;
|
||||
float_type h0, h1, h2;
|
||||
|
||||
if ( (dist == 1 || *iter_probs - this->probability <= this->probability - *(iter_probs - 1) ) && dist != this->probabilities.size() - 1 )
|
||||
{
|
||||
p0 = *(iter_probs - 1);
|
||||
p1 = *iter_probs;
|
||||
p2 = *(iter_probs + 1);
|
||||
h0 = *(iter_heights - 1);
|
||||
h1 = *iter_heights;
|
||||
h2 = *(iter_heights + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
p0 = *(iter_probs - 2);
|
||||
p1 = *(iter_probs - 1);
|
||||
p2 = *iter_probs;
|
||||
h0 = *(iter_heights - 2);
|
||||
h1 = *(iter_heights - 1);
|
||||
h2 = *iter_heights;
|
||||
}
|
||||
|
||||
float_type hp21 = numeric::fdiv(h2 - h1, p2 - p1);
|
||||
float_type hp10 = numeric::fdiv(h1 - h0, p1 - p0);
|
||||
float_type p21 = numeric::fdiv(p2 * p2 - p1 * p1, p2 - p1);
|
||||
float_type p10 = numeric::fdiv(p1 * p1 - p0 * p0, p1 - p0);
|
||||
|
||||
float_type a = numeric::fdiv(hp21 - hp10, p21 - p10);
|
||||
float_type b = hp21 - a * p21;
|
||||
float_type c = h2 - a * p2 * p2 - b * p2;
|
||||
|
||||
res = a * this->probability * this-> probability + b * this->probability + c;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that the parameters did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & probabilities;
|
||||
ar & probability;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
array_type probabilities;
|
||||
mutable float_type probability;
|
||||
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::extended_p_square_quantile
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct extended_p_square_quantile
|
||||
: depends_on<extended_p_square>
|
||||
{
|
||||
typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, linear> impl;
|
||||
};
|
||||
struct extended_p_square_quantile_quadratic
|
||||
: depends_on<extended_p_square>
|
||||
{
|
||||
typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, quadratic> impl;
|
||||
};
|
||||
struct weighted_extended_p_square_quantile
|
||||
: depends_on<weighted_extended_p_square>
|
||||
{
|
||||
typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, linear> impl;
|
||||
};
|
||||
struct weighted_extended_p_square_quantile_quadratic
|
||||
: depends_on<weighted_extended_p_square>
|
||||
{
|
||||
typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, quadratic> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::extended_p_square_quantile
|
||||
// extract::weighted_extended_p_square_quantile
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::extended_p_square_quantile> const extended_p_square_quantile = {};
|
||||
extractor<tag::extended_p_square_quantile_quadratic> const extended_p_square_quantile_quadratic = {};
|
||||
extractor<tag::weighted_extended_p_square_quantile> const weighted_extended_p_square_quantile = {};
|
||||
extractor<tag::weighted_extended_p_square_quantile_quadratic> const weighted_extended_p_square_quantile_quadratic = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile_quadratic)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile_quadratic)
|
||||
}
|
||||
|
||||
using extract::extended_p_square_quantile;
|
||||
using extract::extended_p_square_quantile_quadratic;
|
||||
using extract::weighted_extended_p_square_quantile;
|
||||
using extract::weighted_extended_p_square_quantile_quadratic;
|
||||
|
||||
// extended_p_square_quantile(linear) -> extended_p_square_quantile
|
||||
template<>
|
||||
struct as_feature<tag::extended_p_square_quantile(linear)>
|
||||
{
|
||||
typedef tag::extended_p_square_quantile type;
|
||||
};
|
||||
|
||||
// extended_p_square_quantile(quadratic) -> extended_p_square_quantile_quadratic
|
||||
template<>
|
||||
struct as_feature<tag::extended_p_square_quantile(quadratic)>
|
||||
{
|
||||
typedef tag::extended_p_square_quantile_quadratic type;
|
||||
};
|
||||
|
||||
// weighted_extended_p_square_quantile(linear) -> weighted_extended_p_square_quantile
|
||||
template<>
|
||||
struct as_feature<tag::weighted_extended_p_square_quantile(linear)>
|
||||
{
|
||||
typedef tag::weighted_extended_p_square_quantile type;
|
||||
};
|
||||
|
||||
// weighted_extended_p_square_quantile(quadratic) -> weighted_extended_p_square_quantile_quadratic
|
||||
template<>
|
||||
struct as_feature<tag::weighted_extended_p_square_quantile(quadratic)>
|
||||
{
|
||||
typedef tag::weighted_extended_p_square_quantile_quadratic type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// extended_p_square_quantile and weighted_extended_p_square_quantile
|
||||
// provide the same feature as quantile
|
||||
template<>
|
||||
struct feature_of<tag::extended_p_square_quantile>
|
||||
: feature_of<tag::quantile>
|
||||
{
|
||||
};
|
||||
template<>
|
||||
struct feature_of<tag::extended_p_square_quantile_quadratic>
|
||||
: feature_of<tag::quantile>
|
||||
{
|
||||
};
|
||||
// So that extended_p_square_quantile can be automatically substituted with
|
||||
// weighted_extended_p_square_quantile when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::extended_p_square_quantile>
|
||||
{
|
||||
typedef tag::weighted_extended_p_square_quantile type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_extended_p_square_quantile>
|
||||
: feature_of<tag::extended_p_square_quantile>
|
||||
{
|
||||
};
|
||||
|
||||
// So that extended_p_square_quantile_quadratic can be automatically substituted with
|
||||
// weighted_extended_p_square_quantile_quadratic when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::extended_p_square_quantile_quadratic>
|
||||
{
|
||||
typedef tag::weighted_extended_p_square_quantile_quadratic type;
|
||||
};
|
||||
template<>
|
||||
struct feature_of<tag::weighted_extended_p_square_quantile_quadratic>
|
||||
: feature_of<tag::extended_p_square_quantile_quadratic>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// kurtosis.hpp
|
||||
//
|
||||
// Copyright 2006 Olivier Gygi, Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_KURTOSIS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_KURTOSIS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <limits>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// kurtosis_impl
|
||||
/**
|
||||
@brief Kurtosis estimation
|
||||
|
||||
The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central
|
||||
moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution
|
||||
has zero kurtosis. The kurtosis can also be expressed by the simple moments:
|
||||
|
||||
\f[
|
||||
\hat{g}_2 =
|
||||
\frac
|
||||
{\widehat{m}_n^{(4)}-4\widehat{m}_n^{(3)}\hat{\mu}_n+6\widehat{m}_n^{(2)}\hat{\mu}_n^2-3\hat{\mu}_n^4}
|
||||
{\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3,
|
||||
\f]
|
||||
|
||||
where \f$ \widehat{m}_n^{(i)} \f$ are the \f$ i \f$-th moment and \f$ \hat{\mu}_n \f$ the mean (first moment) of the
|
||||
\f$ n \f$ samples.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct kurtosis_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, Sample>::result_type result_type;
|
||||
|
||||
kurtosis_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(
|
||||
accumulators::moment<4>(args)
|
||||
- 4. * accumulators::moment<3>(args) * mean(args)
|
||||
+ 6. * accumulators::moment<2>(args) * mean(args) * mean(args)
|
||||
- 3. * mean(args) * mean(args) * mean(args) * mean(args)
|
||||
, ( accumulators::moment<2>(args) - mean(args) * mean(args) )
|
||||
* ( accumulators::moment<2>(args) - mean(args) * mean(args) )
|
||||
) - 3.;
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::kurtosis
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct kurtosis
|
||||
: depends_on<mean, moment<2>, moment<3>, moment<4> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::kurtosis_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::kurtosis
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::kurtosis> const kurtosis = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(kurtosis)
|
||||
}
|
||||
|
||||
using extract::kurtosis;
|
||||
|
||||
// So that kurtosis can be automatically substituted with
|
||||
// weighted_kurtosis when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::kurtosis>
|
||||
{
|
||||
typedef tag::weighted_kurtosis type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_kurtosis>
|
||||
: feature_of<tag::kurtosis>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// max.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_MAX_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_MAX_HPP_EAN_28_10_2005
|
||||
|
||||
#include <limits>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// max_impl
|
||||
template<typename Sample>
|
||||
struct max_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef Sample result_type;
|
||||
|
||||
template<typename Args>
|
||||
max_impl(Args const &args)
|
||||
: max_(numeric::as_min(args[sample | Sample()]))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
numeric::max_assign(this->max_, args[sample]);
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->max_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & max_;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample max_;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::max
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct max
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::max_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::max
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::max> const max = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(max)
|
||||
}
|
||||
|
||||
using extract::max;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,308 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// mean.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// mean_impl
|
||||
// lazy, by default
|
||||
template<typename Sample, typename SumFeature>
|
||||
struct mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
mean_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
extractor<SumFeature> sum;
|
||||
return numeric::fdiv(sum(args), count(args));
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
template<typename Sample, typename Tag>
|
||||
struct immediate_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
immediate_mean_impl(Args const &args)
|
||||
: mean(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
this->mean = numeric::fdiv(
|
||||
(this->mean * (cnt - 1)) + args[parameter::keyword<Tag>::get()]
|
||||
, cnt
|
||||
);
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->mean;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & mean;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type mean;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::mean
|
||||
// tag::immediate_mean
|
||||
// tag::mean_of_weights
|
||||
// tag::immediate_mean_of_weights
|
||||
// tag::mean_of_variates
|
||||
// tag::immediate_mean_of_variates
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct mean
|
||||
: depends_on<count, sum>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
|
||||
};
|
||||
struct immediate_mean
|
||||
: depends_on<count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl;
|
||||
};
|
||||
struct mean_of_weights
|
||||
: depends_on<count, sum_of_weights>
|
||||
{
|
||||
typedef mpl::true_ is_weight_accumulator;
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::mean_impl<mpl::_2, sum_of_weights> impl;
|
||||
};
|
||||
struct immediate_mean_of_weights
|
||||
: depends_on<count>
|
||||
{
|
||||
typedef mpl::true_ is_weight_accumulator;
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_mean_impl<mpl::_2, tag::weight> impl;
|
||||
};
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct mean_of_variates
|
||||
: depends_on<count, sum_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::mean_impl<VariateType, sum_of_variates<VariateType, VariateTag> > > impl;
|
||||
};
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct immediate_mean_of_variates
|
||||
: depends_on<count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::mean
|
||||
// extract::mean_of_weights
|
||||
// extract::mean_of_variates
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::mean> const mean = {};
|
||||
extractor<tag::mean_of_weights> const mean_of_weights = {};
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, mean_of_variates, (typename)(typename))
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean_of_weights)
|
||||
}
|
||||
|
||||
using extract::mean;
|
||||
using extract::mean_of_weights;
|
||||
using extract::mean_of_variates;
|
||||
|
||||
// mean(lazy) -> mean
|
||||
template<>
|
||||
struct as_feature<tag::mean(lazy)>
|
||||
{
|
||||
typedef tag::mean type;
|
||||
};
|
||||
|
||||
// mean(immediate) -> immediate_mean
|
||||
template<>
|
||||
struct as_feature<tag::mean(immediate)>
|
||||
{
|
||||
typedef tag::immediate_mean type;
|
||||
};
|
||||
|
||||
// mean_of_weights(lazy) -> mean_of_weights
|
||||
template<>
|
||||
struct as_feature<tag::mean_of_weights(lazy)>
|
||||
{
|
||||
typedef tag::mean_of_weights type;
|
||||
};
|
||||
|
||||
// mean_of_weights(immediate) -> immediate_mean_of_weights
|
||||
template<>
|
||||
struct as_feature<tag::mean_of_weights(immediate)>
|
||||
{
|
||||
typedef tag::immediate_mean_of_weights type;
|
||||
};
|
||||
|
||||
// mean_of_variates<VariateType, VariateTag>(lazy) -> mean_of_variates<VariateType, VariateTag>
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(lazy)>
|
||||
{
|
||||
typedef tag::mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
// mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_mean_of_variates<VariateType, VariateTag>
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(immediate)>
|
||||
{
|
||||
typedef tag::immediate_mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_mean provides the same feature as mean
|
||||
template<>
|
||||
struct feature_of<tag::immediate_mean>
|
||||
: feature_of<tag::mean>
|
||||
{
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_mean provides the same feature as mean
|
||||
template<>
|
||||
struct feature_of<tag::immediate_mean_of_weights>
|
||||
: feature_of<tag::mean_of_weights>
|
||||
{
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_mean provides the same feature as mean
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
|
||||
: feature_of<tag::mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
};
|
||||
|
||||
// So that mean can be automatically substituted with
|
||||
// weighted_mean when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::mean>
|
||||
{
|
||||
typedef tag::weighted_mean type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_mean>
|
||||
: feature_of<tag::mean>
|
||||
{};
|
||||
|
||||
// So that immediate_mean can be automatically substituted with
|
||||
// immediate_weighted_mean when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::immediate_mean>
|
||||
{
|
||||
typedef tag::immediate_weighted_mean type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::immediate_weighted_mean>
|
||||
: feature_of<tag::immediate_mean>
|
||||
{};
|
||||
|
||||
// So that mean_of_weights<> can be automatically substituted with
|
||||
// weighted_mean_of_variates<> when the weight parameter is non-void.
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_weighted_feature<tag::mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::weighted_mean_of_variates<VariateType, VariateTag> >
|
||||
: feature_of<tag::mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
};
|
||||
|
||||
// So that immediate_mean_of_weights<> can be automatically substituted with
|
||||
// immediate_weighted_mean_of_variates<> when the weight parameter is non-void.
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_weighted_feature<tag::immediate_mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> >
|
||||
: feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//// droppable_accumulator<mean_impl>
|
||||
//// need to specialize droppable lazy mean to cache the result at the
|
||||
//// point the accumulator is dropped.
|
||||
///// INTERNAL ONLY
|
||||
/////
|
||||
//template<typename Sample, typename SumFeature>
|
||||
//struct droppable_accumulator<impl::mean_impl<Sample, SumFeature> >
|
||||
// : droppable_accumulator_base<
|
||||
// with_cached_result<impl::mean_impl<Sample, SumFeature> >
|
||||
// >
|
||||
//{
|
||||
// template<typename Args>
|
||||
// droppable_accumulator(Args const &args)
|
||||
// : droppable_accumulator::base(args)
|
||||
// {
|
||||
// }
|
||||
//};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,324 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// median.hpp
|
||||
//
|
||||
// Copyright 2006 Eric Niebler, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_MEDIAN_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_MEDIAN_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/p_square_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/density.hpp>
|
||||
#include <boost/accumulators/statistics/p_square_cumul_dist.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation based on the \f$P^2\f$ quantile estimator
|
||||
|
||||
The \f$P^2\f$ algorithm is invoked with a quantile probability of 0.5.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
median_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return p_square_quantile_for_median(args);
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// with_density_median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation based on the density estimator
|
||||
|
||||
The algorithm determines the bin in which the \f$0.5*cnt\f$-th sample lies, \f$cnt\f$ being
|
||||
the total number of samples. It returns the approximate horizontal position of this sample,
|
||||
based on a linear interpolation inside the bin.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct with_density_median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef iterator_range<typename histogram_type::iterator> range_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
with_density_median_impl(Args const &args)
|
||||
: sum(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
}
|
||||
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
range_type histogram = density(args);
|
||||
typename range_type::iterator it = histogram.begin();
|
||||
while (this->sum < 0.5 * cnt)
|
||||
{
|
||||
this->sum += it->second * cnt;
|
||||
++it;
|
||||
}
|
||||
--it;
|
||||
float_type over = numeric::fdiv(this->sum - 0.5 * cnt, it->second * cnt);
|
||||
this->median = it->first * over + (it + 1)->first * (1. - over);
|
||||
}
|
||||
|
||||
return this->median;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
ar & is_dirty;
|
||||
ar & median;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
mutable float_type sum;
|
||||
mutable bool is_dirty;
|
||||
mutable float_type median;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// with_p_square_cumulative_distribution_median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation based on the \f$P^2\f$ cumulative distribution estimator
|
||||
|
||||
The algorithm determines the first (leftmost) bin with a height exceeding 0.5. It
|
||||
returns the approximate horizontal position of where the cumulative distribution
|
||||
equals 0.5, based on a linear interpolation inside the bin.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct with_p_square_cumulative_distribution_median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef iterator_range<typename histogram_type::iterator> range_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
with_p_square_cumulative_distribution_median_impl(dont_care)
|
||||
: is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
range_type histogram = p_square_cumulative_distribution(args);
|
||||
typename range_type::iterator it = histogram.begin();
|
||||
while (it->second < 0.5)
|
||||
{
|
||||
++it;
|
||||
}
|
||||
float_type over = numeric::fdiv(it->second - 0.5, it->second - (it - 1)->second);
|
||||
this->median = it->first * over + (it + 1)->first * ( 1. - over );
|
||||
}
|
||||
|
||||
return this->median;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & is_dirty;
|
||||
ar & median;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
mutable bool is_dirty;
|
||||
mutable float_type median;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::median
|
||||
// tag::with_densisty_median
|
||||
// tag::with_p_square_cumulative_distribution_median
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct median
|
||||
: depends_on<p_square_quantile_for_median>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::median_impl<mpl::_1> impl;
|
||||
};
|
||||
struct with_density_median
|
||||
: depends_on<count, density>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::with_density_median_impl<mpl::_1> impl;
|
||||
};
|
||||
struct with_p_square_cumulative_distribution_median
|
||||
: depends_on<p_square_cumulative_distribution>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::with_p_square_cumulative_distribution_median_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::median
|
||||
// extract::with_density_median
|
||||
// extract::with_p_square_cumulative_distribution_median
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::median> const median = {};
|
||||
extractor<tag::with_density_median> const with_density_median = {};
|
||||
extractor<tag::with_p_square_cumulative_distribution_median> const with_p_square_cumulative_distribution_median = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(median)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(with_density_median)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(with_p_square_cumulative_distribution_median)
|
||||
}
|
||||
|
||||
using extract::median;
|
||||
using extract::with_density_median;
|
||||
using extract::with_p_square_cumulative_distribution_median;
|
||||
|
||||
// median(with_p_square_quantile) -> median
|
||||
template<>
|
||||
struct as_feature<tag::median(with_p_square_quantile)>
|
||||
{
|
||||
typedef tag::median type;
|
||||
};
|
||||
|
||||
// median(with_density) -> with_density_median
|
||||
template<>
|
||||
struct as_feature<tag::median(with_density)>
|
||||
{
|
||||
typedef tag::with_density_median type;
|
||||
};
|
||||
|
||||
// median(with_p_square_cumulative_distribution) -> with_p_square_cumulative_distribution_median
|
||||
template<>
|
||||
struct as_feature<tag::median(with_p_square_cumulative_distribution)>
|
||||
{
|
||||
typedef tag::with_p_square_cumulative_distribution_median type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// with_density_median and with_p_square_cumulative_distribution_median
|
||||
// provide the same feature as median
|
||||
template<>
|
||||
struct feature_of<tag::with_density_median>
|
||||
: feature_of<tag::median>
|
||||
{
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::with_p_square_cumulative_distribution_median>
|
||||
: feature_of<tag::median>
|
||||
{
|
||||
};
|
||||
|
||||
// So that median can be automatically substituted with
|
||||
// weighted_median when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::median>
|
||||
{
|
||||
typedef tag::weighted_median type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_median>
|
||||
: feature_of<tag::median>
|
||||
{
|
||||
};
|
||||
|
||||
// So that with_density_median can be automatically substituted with
|
||||
// with_density_weighted_median when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::with_density_median>
|
||||
{
|
||||
typedef tag::with_density_weighted_median type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::with_density_weighted_median>
|
||||
: feature_of<tag::with_density_median>
|
||||
{
|
||||
};
|
||||
|
||||
// So that with_p_square_cumulative_distribution_median can be automatically substituted with
|
||||
// with_p_square_cumulative_distribution_weighted_median when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::with_p_square_cumulative_distribution_median>
|
||||
{
|
||||
typedef tag::with_p_square_cumulative_distribution_weighted_median type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::with_p_square_cumulative_distribution_weighted_median>
|
||||
: feature_of<tag::with_p_square_cumulative_distribution_median>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// min.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_MIN_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_MIN_HPP_EAN_28_10_2005
|
||||
|
||||
#include <limits>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// min_impl
|
||||
template<typename Sample>
|
||||
struct min_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef Sample result_type;
|
||||
|
||||
template<typename Args>
|
||||
min_impl(Args const &args)
|
||||
: min_(numeric::as_max(args[sample | Sample()]))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
numeric::min_assign(this->min_, args[sample]);
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->min_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & min_;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample min_;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::min
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct min
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::min_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::min
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::min> const min = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(min)
|
||||
}
|
||||
|
||||
using extract::min;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// moment.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005
|
||||
|
||||
#include <boost/config/no_tr1/cmath.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
|
||||
namespace boost { namespace numeric
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename T>
|
||||
T const &pow(T const &x, mpl::int_<1>)
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename T, int N>
|
||||
T pow(T const &x, mpl::int_<N>)
|
||||
{
|
||||
using namespace operators;
|
||||
T y = numeric::pow(x, mpl::int_<N/2>());
|
||||
T z = y * y;
|
||||
return (N % 2) ? (z * x) : z;
|
||||
}
|
||||
}}
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// moment_impl
|
||||
template<typename N, typename Sample>
|
||||
struct moment_impl
|
||||
: accumulator_base // TODO: also depends_on sum of powers
|
||||
{
|
||||
BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
moment_impl(Args const &args)
|
||||
: sum(args[sample | Sample()])
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->sum += numeric::pow(args[sample], N());
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(this->sum, count(args));
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample sum;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::moment
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<int N>
|
||||
struct moment
|
||||
: depends_on<count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::moment_impl<mpl::int_<N>, mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::moment
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, moment, (int))
|
||||
}
|
||||
|
||||
using extract::moment;
|
||||
|
||||
// So that moment<N> can be automatically substituted with
|
||||
// weighted_moment<N> when the weight parameter is non-void
|
||||
template<int N>
|
||||
struct as_weighted_feature<tag::moment<N> >
|
||||
{
|
||||
typedef tag::weighted_moment<N> type;
|
||||
};
|
||||
|
||||
template<int N>
|
||||
struct feature_of<tag::weighted_moment<N> >
|
||||
: feature_of<tag::moment<N> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,279 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// p_square_cumulative_distribution.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
#include <boost/serialization/utility.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// num_cells named parameter
|
||||
//
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, p_square_cumulative_distribution_num_cells, num_cells)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution_num_cells)
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// p_square_cumulative_distribution_impl
|
||||
// cumulative_distribution calculation (as histogram)
|
||||
/**
|
||||
@brief Histogram calculation of the cumulative distribution with the \f$P^2\f$ algorithm
|
||||
|
||||
A histogram of the sample cumulative distribution is computed dynamically without storing samples
|
||||
based on the \f$ P^2 \f$ algorithm. The returned histogram has a specifiable amount (num_cells)
|
||||
equiprobable (and not equal-sized) cells.
|
||||
|
||||
For further details, see
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param p_square_cumulative_distribution_num_cells.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct p_square_cumulative_distribution_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<typename histogram_type::iterator> result_type;
|
||||
|
||||
template<typename Args>
|
||||
p_square_cumulative_distribution_impl(Args const &args)
|
||||
: num_cells(args[p_square_cumulative_distribution_num_cells])
|
||||
, heights(num_cells + 1)
|
||||
, actual_positions(num_cells + 1)
|
||||
, desired_positions(num_cells + 1)
|
||||
, positions_increments(num_cells + 1)
|
||||
, histogram(num_cells + 1)
|
||||
, is_dirty(true)
|
||||
{
|
||||
std::size_t b = this->num_cells;
|
||||
|
||||
for (std::size_t i = 0; i < b + 1; ++i)
|
||||
{
|
||||
this->actual_positions[i] = i + 1.;
|
||||
this->desired_positions[i] = i + 1.;
|
||||
this->positions_increments[i] = numeric::fdiv(i, b);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
std::size_t sample_cell = 1; // k
|
||||
std::size_t b = this->num_cells;
|
||||
|
||||
// accumulate num_cells + 1 first samples
|
||||
if (cnt <= b + 1)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
|
||||
// complete the initialization of heights by sorting
|
||||
if (cnt == b + 1)
|
||||
{
|
||||
std::sort(this->heights.begin(), this->heights.end());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
|
||||
if (args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if (this->heights[b] <= args[sample])
|
||||
{
|
||||
this->heights[b] = args[sample];
|
||||
sample_cell = b;
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it;
|
||||
it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// increment positions of markers above sample_cell
|
||||
for (std::size_t i = sample_cell; i < b + 1; ++i)
|
||||
{
|
||||
++this->actual_positions[i];
|
||||
}
|
||||
|
||||
// update desired position of markers 2 to num_cells + 1
|
||||
// (desired position of first marker is always 1)
|
||||
for (std::size_t i = 1; i < b + 1; ++i)
|
||||
{
|
||||
this->desired_positions[i] += this->positions_increments[i];
|
||||
}
|
||||
|
||||
// adjust heights of markers 2 to num_cells if necessary
|
||||
for (std::size_t i = 1; i < b; ++i)
|
||||
{
|
||||
// offset to desire position
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
||||
|
||||
if ( ( d >= 1. && dp > 1. ) || ( d <= -1. && dm < -1. ) )
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ( (sign_d - dm) * hp + (dp - sign_d) * hm );
|
||||
|
||||
if ( this->heights[i - 1] < h && h < this->heights[i + 1] )
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if (d>0)
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if (d<0)
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
// creates a vector of std::pair where each pair i holds
|
||||
// the values heights[i] (x-axis of histogram) and
|
||||
// actual_positions[i] / cnt (y-axis of histogram)
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
for (std::size_t i = 0; i < this->histogram.size(); ++i)
|
||||
{
|
||||
this->histogram[i] = std::make_pair(this->heights[i], numeric::fdiv(this->actual_positions[i], cnt));
|
||||
}
|
||||
}
|
||||
//return histogram;
|
||||
return make_iterator_range(this->histogram);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO split to save/load and check on parameters provided in ctor
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & num_cells;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
ar & positions_increments;
|
||||
ar & histogram;
|
||||
ar & is_dirty;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t num_cells; // number of cells b
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // n'_i
|
||||
array_type positions_increments; // dn'_i
|
||||
mutable histogram_type histogram; // histogram
|
||||
mutable bool is_dirty;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::p_square_cumulative_distribution
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct p_square_cumulative_distribution
|
||||
: depends_on<count>
|
||||
, p_square_cumulative_distribution_num_cells
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::p_square_cumulative_distribution_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::p_square_cumulative_distribution
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::p_square_cumulative_distribution> const p_square_cumulative_distribution = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution)
|
||||
}
|
||||
|
||||
using extract::p_square_cumulative_distribution;
|
||||
|
||||
// So that p_square_cumulative_distribution can be automatically substituted with
|
||||
// weighted_p_square_cumulative_distribution when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::p_square_cumulative_distribution>
|
||||
{
|
||||
typedef tag::weighted_p_square_cumulative_distribution type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_p_square_cumulative_distribution>
|
||||
: feature_of<tag::p_square_cumulative_distribution>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// p_square_cumulative_distribution.hpp
|
||||
//
|
||||
// Copyright 2012 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMULATIVE_DISTRIBUTION_HPP_03_19_2012
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMULATIVE_DISTRIBUTION_HPP_03_19_2012
|
||||
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__)
|
||||
# pragma message ("Warning: This header is deprecated. Please use: boost/accumulators/statistics/p_square_cumul_dist.hpp")
|
||||
#elif defined(__GNUC__) || defined(__HP_aCC) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
|
||||
# warning "This header is deprecated. Please use: boost/accumulators/statistics/p_square_cumul_dist.hpp"
|
||||
#endif
|
||||
|
||||
#include <boost/accumulators/statistics/p_square_cumul_dist.hpp>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,271 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// p_square_quantile.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
||||
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
#include <boost/array.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
#include <boost/serialization/boost_array.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// p_square_quantile_impl
|
||||
// single quantile estimation
|
||||
/**
|
||||
@brief Single quantile estimation with the \f$P^2\f$ algorithm
|
||||
|
||||
The \f$P^2\f$ algorithm estimates a quantile dynamically without storing samples. Instead of
|
||||
storing the whole sample cumulative distribution, only five points (markers) are stored. The heights
|
||||
of these markers are the minimum and the maximum of the samples and the current estimates of the
|
||||
\f$(p/2)\f$-, \f$p\f$- and \f$(1+p)/2\f$-quantiles. Their positions are equal to the number
|
||||
of samples that are smaller or equal to the markers. Each time a new samples is recorded, the
|
||||
positions of the markers are updated and if necessary their heights are adjusted using a piecewise-
|
||||
parabolic formula.
|
||||
|
||||
For further details, see
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param quantile_probability
|
||||
*/
|
||||
template<typename Sample, typename Impl>
|
||||
struct p_square_quantile_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef array<float_type, 5> array_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
p_square_quantile_impl(Args const &args)
|
||||
: p(is_same<Impl, for_median>::value ? float_type(0.5) : args[quantile_probability | float_type(0.5)])
|
||||
, heights()
|
||||
, actual_positions()
|
||||
, desired_positions()
|
||||
, positions_increments()
|
||||
{
|
||||
for(std::size_t i = 0; i < 5; ++i)
|
||||
{
|
||||
this->actual_positions[i] = i + float_type(1.);
|
||||
}
|
||||
|
||||
this->desired_positions[0] = float_type(1.);
|
||||
this->desired_positions[1] = float_type(1.) + float_type(2.) * this->p;
|
||||
this->desired_positions[2] = float_type(1.) + float_type(4.) * this->p;
|
||||
this->desired_positions[3] = float_type(3.) + float_type(2.) * this->p;
|
||||
this->desired_positions[4] = float_type(5.);
|
||||
|
||||
|
||||
this->positions_increments[0] = float_type(0.);
|
||||
this->positions_increments[1] = this->p / float_type(2.);
|
||||
this->positions_increments[2] = this->p;
|
||||
this->positions_increments[3] = (float_type(1.) + this->p) / float_type(2.);
|
||||
this->positions_increments[4] = float_type(1.);
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// accumulate 5 first samples
|
||||
if(cnt <= 5)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
|
||||
// complete the initialization of heights by sorting
|
||||
if(cnt == 5)
|
||||
{
|
||||
std::sort(this->heights.begin(), this->heights.end());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::size_t sample_cell = 1; // k
|
||||
|
||||
// find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
|
||||
if (args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if (this->heights[4] <= args[sample])
|
||||
{
|
||||
this->heights[4] = args[sample];
|
||||
sample_cell = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef typename array_type::iterator iterator;
|
||||
iterator it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// update positions of markers above sample_cell
|
||||
for(std::size_t i = sample_cell; i < 5; ++i)
|
||||
{
|
||||
++this->actual_positions[i];
|
||||
}
|
||||
|
||||
// update desired positions of all markers
|
||||
for(std::size_t i = 0; i < 5; ++i)
|
||||
{
|
||||
this->desired_positions[i] += this->positions_increments[i];
|
||||
}
|
||||
|
||||
// adjust heights and actual positions of markers 1 to 3 if necessary
|
||||
for(std::size_t i = 1; i <= 3; ++i)
|
||||
{
|
||||
// offset to desired positions
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
||||
|
||||
if((d >= float_type(1.) && dp > float_type(1.)) || (d <= float_type(-1.) && dm < float_type(-1.)))
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm) * hp
|
||||
+ (dp - sign_d) * hm);
|
||||
|
||||
if(this->heights[i - 1] < h && h < this->heights[i + 1])
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if(d > float_type(0))
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if(d < float_type(0))
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->heights[2];
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that P did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & p;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
ar & positions_increments;
|
||||
}
|
||||
|
||||
private:
|
||||
float_type p; // the quantile probability p
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // n'_i
|
||||
array_type positions_increments; // dn'_i
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::p_square_quantile
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct p_square_quantile
|
||||
: depends_on<count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::p_square_quantile_impl<mpl::_1, regular> impl;
|
||||
};
|
||||
struct p_square_quantile_for_median
|
||||
: depends_on<count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::p_square_quantile_impl<mpl::_1, for_median> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::p_square_quantile
|
||||
// extract::p_square_quantile_for_median
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::p_square_quantile> const p_square_quantile = {};
|
||||
extractor<tag::p_square_quantile_for_median> const p_square_quantile_for_median = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile_for_median)
|
||||
}
|
||||
|
||||
using extract::p_square_quantile;
|
||||
using extract::p_square_quantile_for_median;
|
||||
|
||||
// So that p_square_quantile can be automatically substituted with
|
||||
// weighted_p_square_quantile when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::p_square_quantile>
|
||||
{
|
||||
typedef tag::weighted_p_square_quantile type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_p_square_quantile>
|
||||
: feature_of<tag::p_square_quantile>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// quantile_probability.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_PARAMETERS_QUANTILE_PROBABILITY_HPP_EAN_03_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_PARAMETERS_QUANTILE_PROBABILITY_HPP_EAN_03_11_2005
|
||||
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
BOOST_PARAMETER_KEYWORD(tag, quantile_probability)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(quantile_probability)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,434 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// peaks_over_threshold.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_PEAKS_OVER_THRESHOLD_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <boost/config/no_tr1/cmath.hpp> // pow
|
||||
#include <sstream> // stringstream
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// threshold_probability and threshold named parameters
|
||||
//
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_value, threshold_value)
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, pot_threshold_probability, threshold_probability)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(pot_threshold_value)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(pot_threshold_probability)
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// peaks_over_threshold_impl
|
||||
// works with an explicit threshold value and does not depend on order statistics
|
||||
/**
|
||||
@brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
|
||||
|
||||
According to the theorem of Pickands-Balkema-de Haan, the distribution function \f$F_u(x)\f$ of
|
||||
the excesses \f$x\f$ over some sufficiently high threshold \f$u\f$ of a distribution function \f$F(x)\f$
|
||||
may be approximated by a generalized Pareto distribution
|
||||
\f[
|
||||
G_{\xi,\beta}(x) =
|
||||
\left\{
|
||||
\begin{array}{ll}
|
||||
\beta^{-1}\left(1+\frac{\xi x}{\beta}\right)^{-1/\xi-1} & \textrm{if }\xi\neq0\\
|
||||
\beta^{-1}\exp\left(-\frac{x}{\beta}\right) & \textrm{if }\xi=0,
|
||||
\end{array}
|
||||
\right.
|
||||
\f]
|
||||
with suitable parameters \f$\xi\f$ and \f$\beta\f$ that can be estimated, e.g., with the method of moments, cf.
|
||||
Hosking and Wallis (1987),
|
||||
\f[
|
||||
\begin{array}{lll}
|
||||
\hat{\xi} & = & \frac{1}{2}\left[1-\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}\right]\\
|
||||
\hat{\beta} & = & \frac{\hat{\mu}-u}{2}\left[\frac{(\hat{\mu}-u)^2}{\hat{\sigma}^2}+1\right],
|
||||
\end{array}
|
||||
\f]
|
||||
\f$\hat{\mu}\f$ and \f$\hat{\sigma}^2\f$ being the empirical mean and variance of the samples over
|
||||
the threshold \f$u\f$. Equivalently, the distribution function
|
||||
\f$F_u(x-u)\f$ of the exceedances \f$x-u\f$ can be approximated by
|
||||
\f$G_{\xi,\beta}(x-u)=G_{\xi,\beta,u}(x)\f$. Since for \f$x\geq u\f$ the distribution function \f$F(x)\f$
|
||||
can be written as
|
||||
\f[
|
||||
F(x) = [1 - \P(X \leq u)]F_u(x - u) + \P(X \leq u)
|
||||
\f]
|
||||
and the probability \f$\P(X \leq u)\f$ can be approximated by the empirical distribution function
|
||||
\f$F_n(u)\f$ evaluated at \f$u\f$, an estimator of \f$F(x)\f$ is given by
|
||||
\f[
|
||||
\widehat{F}(x) = [1 - F_n(u)]G_{\xi,\beta,u}(x) + F_n(u).
|
||||
\f]
|
||||
It can be shown that \f$\widehat{F}(x)\f$ is a generalized
|
||||
Pareto distribution \f$G_{\xi,\bar{\beta},\bar{u}}(x)\f$ with \f$\bar{\beta}=\beta[1-F_n(u)]^{\xi}\f$
|
||||
and \f$\bar{u}=u-\bar{\beta}\left\{[1-F_n(u)]^{-\xi}-1\right\}/\xi\f$. By inverting \f$\widehat{F}(x)\f$,
|
||||
one obtains an estimator for the \f$\alpha\f$-quantile,
|
||||
\f[
|
||||
\hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right],
|
||||
\f]
|
||||
and similarly an estimator for the (coherent) tail mean,
|
||||
\f[
|
||||
\widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},
|
||||
\f]
|
||||
cf. McNeil and Frey (2000).
|
||||
|
||||
Note that in case extreme values of the left tail are fitted, the distribution is mirrored with respect to the
|
||||
\f$y\f$ axis such that the left tail can be treated as a right tail. The computed fit parameters thus define
|
||||
the Pareto distribution that fits the mirrored left tail. When quantities like a quantile or a tail mean are
|
||||
computed using the fit parameters obtained from the mirrored data, the result is mirrored back, yielding the
|
||||
correct result.
|
||||
|
||||
For further details, see
|
||||
|
||||
J. R. M. Hosking and J. R. Wallis, Parameter and quantile estimation for the generalized Pareto distribution,
|
||||
Technometrics, Volume 29, 1987, p. 339-349
|
||||
|
||||
A. J. McNeil and R. Frey, Estimation of Tail-Related Risk Measures for Heteroscedastic Financial Time Series:
|
||||
an Extreme Value Approach, Journal of Empirical Finance, Volume 7, 2000, p. 271-300
|
||||
|
||||
@param quantile_probability
|
||||
@param pot_threshold_value
|
||||
*/
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct peaks_over_threshold_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef boost::tuple<float_type, float_type, float_type> result_type;
|
||||
// for left tail fitting, mirror the extreme values
|
||||
typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;
|
||||
|
||||
template<typename Args>
|
||||
peaks_over_threshold_impl(Args const &args)
|
||||
: Nu_(0)
|
||||
, mu_(sign::value * numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, threshold_(sign::value * args[pot_threshold_value])
|
||||
, fit_parameters_(boost::make_tuple(0., 0., 0.))
|
||||
, is_dirty_(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->is_dirty_ = true;
|
||||
|
||||
if (sign::value * args[sample] > this->threshold_)
|
||||
{
|
||||
this->mu_ += args[sample];
|
||||
this->sigma2_ += args[sample] * args[sample];
|
||||
++this->Nu_;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty_)
|
||||
{
|
||||
this->is_dirty_ = false;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
this->mu_ = sign::value * numeric::fdiv(this->mu_, this->Nu_);
|
||||
this->sigma2_ = numeric::fdiv(this->sigma2_, this->Nu_);
|
||||
this->sigma2_ -= this->mu_ * this->mu_;
|
||||
|
||||
float_type threshold_probability = numeric::fdiv(cnt - this->Nu_, cnt);
|
||||
|
||||
float_type tmp = numeric::fdiv(( this->mu_ - this->threshold_ )*( this->mu_ - this->threshold_ ), this->sigma2_);
|
||||
float_type xi_hat = 0.5 * ( 1. - tmp );
|
||||
float_type beta_hat = 0.5 * ( this->mu_ - this->threshold_ ) * ( 1. + tmp );
|
||||
float_type beta_bar = beta_hat * std::pow(1. - threshold_probability, xi_hat);
|
||||
float_type u_bar = this->threshold_ - beta_bar * ( std::pow(1. - threshold_probability, -xi_hat) - 1.)/xi_hat;
|
||||
this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);
|
||||
}
|
||||
|
||||
return this->fit_parameters_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that threshold did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & Nu_;
|
||||
ar & mu_;
|
||||
ar & sigma2_;
|
||||
ar & threshold_;
|
||||
ar & get<0>(fit_parameters_);
|
||||
ar & get<1>(fit_parameters_);
|
||||
ar & get<2>(fit_parameters_);
|
||||
ar & is_dirty_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t Nu_; // number of samples larger than threshold
|
||||
mutable float_type mu_; // mean of Nu_ largest samples
|
||||
mutable float_type sigma2_; // variance of Nu_ largest samples
|
||||
float_type threshold_;
|
||||
mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
|
||||
mutable bool is_dirty_;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// peaks_over_threshold_prob_impl
|
||||
// determines threshold from a given threshold probability using order statistics
|
||||
/**
|
||||
@brief Peaks over Threshold Method for Quantile and Tail Mean Estimation
|
||||
|
||||
@sa peaks_over_threshold_impl
|
||||
|
||||
@param quantile_probability
|
||||
@param pot_threshold_probability
|
||||
*/
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct peaks_over_threshold_prob_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef boost::tuple<float_type, float_type, float_type> result_type;
|
||||
// for left tail fitting, mirror the extreme values
|
||||
typedef mpl::int_<is_same<LeftRight, left>::value ? -1 : 1> sign;
|
||||
|
||||
template<typename Args>
|
||||
peaks_over_threshold_prob_impl(Args const &args)
|
||||
: mu_(sign::value * numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, sigma2_(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, threshold_probability_(args[pot_threshold_probability])
|
||||
, fit_parameters_(boost::make_tuple(0., 0., 0.))
|
||||
, is_dirty_(true)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
this->is_dirty_ = true;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty_)
|
||||
{
|
||||
this->is_dirty_ = false;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// the n'th cached sample provides an approximate threshold value u
|
||||
std::size_t n = static_cast<std::size_t>(
|
||||
std::ceil(
|
||||
cnt * ( ( is_same<LeftRight, left>::value ) ? this->threshold_probability_ : 1. - this->threshold_probability_ )
|
||||
)
|
||||
);
|
||||
|
||||
// If n is in a valid range, return result, otherwise return NaN or throw exception
|
||||
if ( n >= static_cast<std::size_t>(tail(args).size()))
|
||||
{
|
||||
if (std::numeric_limits<float_type>::has_quiet_NaN)
|
||||
{
|
||||
return boost::make_tuple(
|
||||
std::numeric_limits<float_type>::quiet_NaN()
|
||||
, std::numeric_limits<float_type>::quiet_NaN()
|
||||
, std::numeric_limits<float_type>::quiet_NaN()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
||||
boost::throw_exception(std::runtime_error(msg.str()));
|
||||
return boost::make_tuple(Sample(0), Sample(0), Sample(0));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float_type u = *(tail(args).begin() + n - 1) * sign::value;
|
||||
|
||||
// compute mean and variance of samples above/under threshold value u
|
||||
for (std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
mu_ += *(tail(args).begin() + i);
|
||||
sigma2_ += *(tail(args).begin() + i) * (*(tail(args).begin() + i));
|
||||
}
|
||||
|
||||
this->mu_ = sign::value * numeric::fdiv(this->mu_, n);
|
||||
this->sigma2_ = numeric::fdiv(this->sigma2_, n);
|
||||
this->sigma2_ -= this->mu_ * this->mu_;
|
||||
|
||||
if (is_same<LeftRight, left>::value)
|
||||
this->threshold_probability_ = 1. - this->threshold_probability_;
|
||||
|
||||
float_type tmp = numeric::fdiv(( this->mu_ - u )*( this->mu_ - u ), this->sigma2_);
|
||||
float_type xi_hat = 0.5 * ( 1. - tmp );
|
||||
float_type beta_hat = 0.5 * ( this->mu_ - u ) * ( 1. + tmp );
|
||||
float_type beta_bar = beta_hat * std::pow(1. - threshold_probability_, xi_hat);
|
||||
float_type u_bar = u - beta_bar * ( std::pow(1. - threshold_probability_, -xi_hat) - 1.)/xi_hat;
|
||||
this->fit_parameters_ = boost::make_tuple(u_bar, beta_bar, xi_hat);
|
||||
}
|
||||
}
|
||||
|
||||
return this->fit_parameters_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that threshold did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & mu_;
|
||||
ar & sigma2_;
|
||||
ar & threshold_probability_;
|
||||
ar & get<0>(fit_parameters_);
|
||||
ar & get<1>(fit_parameters_);
|
||||
ar & get<2>(fit_parameters_);
|
||||
ar & is_dirty_;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable float_type mu_; // mean of samples above threshold u
|
||||
mutable float_type sigma2_; // variance of samples above threshold u
|
||||
mutable float_type threshold_probability_;
|
||||
mutable result_type fit_parameters_; // boost::tuple that stores fit parameters
|
||||
mutable bool is_dirty_;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::peaks_over_threshold
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct peaks_over_threshold
|
||||
: depends_on<count>
|
||||
, pot_threshold_value
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::peaks_over_threshold_impl<mpl::_1, LeftRight> impl;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct peaks_over_threshold_prob
|
||||
: depends_on<count, tail<LeftRight> >
|
||||
, pot_threshold_probability
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::peaks_over_threshold_prob_impl<mpl::_1, LeftRight> impl;
|
||||
};
|
||||
|
||||
struct abstract_peaks_over_threshold
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::peaks_over_threshold
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_peaks_over_threshold> const peaks_over_threshold = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(peaks_over_threshold)
|
||||
}
|
||||
|
||||
using extract::peaks_over_threshold;
|
||||
|
||||
// peaks_over_threshold<LeftRight>(with_threshold_value) -> peaks_over_threshold<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_value)>
|
||||
{
|
||||
typedef tag::peaks_over_threshold<LeftRight> type;
|
||||
};
|
||||
|
||||
// peaks_over_threshold<LeftRight>(with_threshold_probability) -> peaks_over_threshold_prob<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::peaks_over_threshold<LeftRight>(with_threshold_probability)>
|
||||
{
|
||||
typedef tag::peaks_over_threshold_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::peaks_over_threshold<LeftRight> >
|
||||
: feature_of<tag::abstract_peaks_over_threshold>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::peaks_over_threshold_prob<LeftRight> >
|
||||
: feature_of<tag::abstract_peaks_over_threshold>
|
||||
{
|
||||
};
|
||||
|
||||
// So that peaks_over_threshold can be automatically substituted
|
||||
// with weighted_peaks_over_threshold when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::peaks_over_threshold<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_peaks_over_threshold<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_peaks_over_threshold<LeftRight> >
|
||||
: feature_of<tag::peaks_over_threshold<LeftRight> >
|
||||
{};
|
||||
|
||||
// So that peaks_over_threshold_prob can be automatically substituted
|
||||
// with weighted_peaks_over_threshold_prob when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::peaks_over_threshold_prob<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_peaks_over_threshold_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_peaks_over_threshold_prob<LeftRight> >
|
||||
: feature_of<tag::peaks_over_threshold_prob<LeftRight> >
|
||||
{};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,212 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pot_quantile.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_POT_QUANTILE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_POT_QUANTILE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/accumulators/statistics/peaks_over_threshold.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pot_quantile_impl
|
||||
//
|
||||
/**
|
||||
@brief Quantile Estimation based on Peaks over Threshold Method (for both left and right tails)
|
||||
|
||||
Computes an estimate
|
||||
\f[
|
||||
\hat{q}_{\alpha} = \bar{u} + \frac{\bar{\beta}}{\xi}\left[(1-\alpha)^{-\xi}-1\right]
|
||||
\f]
|
||||
for a right or left extreme quantile, \f$\bar[u]\f$, \f$\bar{\beta}\f$ and \f$\xi\f$ being the parameters of the
|
||||
generalized Pareto distribution that approximates the right tail of the distribution (or the mirrored left tail,
|
||||
in case the left tail is used). In the latter case, the result is mirrored back, yielding the correct result.
|
||||
*/
|
||||
template<typename Sample, typename Impl, typename LeftRight>
|
||||
struct pot_quantile_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
pot_quantile_impl(dont_care)
|
||||
: sign_((is_same<LeftRight, left>::value) ? -1 : 1)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<Impl, weighted>
|
||||
, tag::weighted_peaks_over_threshold<LeftRight>
|
||||
, tag::peaks_over_threshold<LeftRight>
|
||||
>::type
|
||||
peaks_over_threshold_tag;
|
||||
|
||||
extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {};
|
||||
|
||||
float_type u_bar = some_peaks_over_threshold(args).template get<0>();
|
||||
float_type beta_bar = some_peaks_over_threshold(args).template get<1>();
|
||||
float_type xi_hat = some_peaks_over_threshold(args).template get<2>();
|
||||
|
||||
return this->sign_ * (u_bar + beta_bar/xi_hat * ( std::pow(
|
||||
is_same<LeftRight, left>::value ? args[quantile_probability] : 1. - args[quantile_probability]
|
||||
, -xi_hat
|
||||
) - 1.));
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sign_;
|
||||
}
|
||||
|
||||
private:
|
||||
short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::pot_quantile<>
|
||||
// tag::pot_quantile_prob<>
|
||||
// tag::weighted_pot_quantile<>
|
||||
// tag::weighted_pot_quantile_prob<>
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct pot_quantile
|
||||
: depends_on<peaks_over_threshold<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct pot_quantile_prob
|
||||
: depends_on<peaks_over_threshold_prob<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_quantile_impl<mpl::_1, unweighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct weighted_pot_quantile
|
||||
: depends_on<weighted_peaks_over_threshold<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct weighted_pot_quantile_prob
|
||||
: depends_on<weighted_peaks_over_threshold_prob<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_quantile_impl<mpl::_1, weighted, LeftRight> impl;
|
||||
};
|
||||
}
|
||||
|
||||
// pot_quantile<LeftRight>(with_threshold_value) -> pot_quantile<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::pot_quantile<LeftRight>(with_threshold_value)>
|
||||
{
|
||||
typedef tag::pot_quantile<LeftRight> type;
|
||||
};
|
||||
|
||||
// pot_quantile<LeftRight>(with_threshold_probability) -> pot_quantile_prob<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::pot_quantile<LeftRight>(with_threshold_probability)>
|
||||
{
|
||||
typedef tag::pot_quantile_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
// weighted_pot_quantile<LeftRight>(with_threshold_value) -> weighted_pot_quantile<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::weighted_pot_quantile<LeftRight>(with_threshold_value)>
|
||||
{
|
||||
typedef tag::weighted_pot_quantile<LeftRight> type;
|
||||
};
|
||||
|
||||
// weighted_pot_quantile<LeftRight>(with_threshold_probability) -> weighted_pot_quantile_prob<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::weighted_pot_quantile<LeftRight>(with_threshold_probability)>
|
||||
{
|
||||
typedef tag::weighted_pot_quantile_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// pot_quantile<LeftRight> and pot_quantile_prob<LeftRight> provide
|
||||
// the same feature as quantile
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::pot_quantile<LeftRight> >
|
||||
: feature_of<tag::quantile>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::pot_quantile_prob<LeftRight> >
|
||||
: feature_of<tag::quantile>
|
||||
{
|
||||
};
|
||||
|
||||
// So that pot_quantile can be automatically substituted
|
||||
// with weighted_pot_quantile when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::pot_quantile<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_pot_quantile<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_pot_quantile<LeftRight> >
|
||||
: feature_of<tag::pot_quantile<LeftRight> >
|
||||
{
|
||||
};
|
||||
|
||||
// So that pot_quantile_prob can be automatically substituted
|
||||
// with weighted_pot_quantile_prob when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::pot_quantile_prob<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_pot_quantile_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_pot_quantile_prob<LeftRight> >
|
||||
: feature_of<tag::pot_quantile_prob<LeftRight> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,219 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pot_tail_mean.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_POT_TAIL_MEAN_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_POT_TAIL_MEAN_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/peaks_over_threshold.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_peaks_over_threshold.hpp>
|
||||
#include <boost/accumulators/statistics/pot_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/tail_mean.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pot_tail_mean_impl
|
||||
//
|
||||
/**
|
||||
@brief Estimation of the (coherent) tail mean based on the peaks over threshold method (for both left and right tails)
|
||||
|
||||
Computes an estimate for the (coherent) tail mean
|
||||
\f[
|
||||
\widehat{CTM}_{\alpha} = \hat{q}_{\alpha} - \frac{\bar{\beta}}{\xi-1}(1-\alpha)^{-\xi},
|
||||
\f]
|
||||
where \f$\bar[u]\f$, \f$\bar{\beta}\f$ and \f$\xi\f$ are the parameters of the
|
||||
generalized Pareto distribution that approximates the right tail of the distribution (or the
|
||||
mirrored left tail, in case the left tail is used). In the latter case, the result is mirrored
|
||||
back, yielding the correct result.
|
||||
*/
|
||||
template<typename Sample, typename Impl, typename LeftRight>
|
||||
struct pot_tail_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
pot_tail_mean_impl(dont_care)
|
||||
: sign_((is_same<LeftRight, left>::value) ? -1 : 1)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<Impl, weighted>
|
||||
, tag::weighted_peaks_over_threshold<LeftRight>
|
||||
, tag::peaks_over_threshold<LeftRight>
|
||||
>::type
|
||||
peaks_over_threshold_tag;
|
||||
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<Impl, weighted>
|
||||
, tag::weighted_pot_quantile<LeftRight>
|
||||
, tag::pot_quantile<LeftRight>
|
||||
>::type
|
||||
pot_quantile_tag;
|
||||
|
||||
extractor<peaks_over_threshold_tag> const some_peaks_over_threshold = {};
|
||||
extractor<pot_quantile_tag> const some_pot_quantile = {};
|
||||
|
||||
float_type beta_bar = some_peaks_over_threshold(args).template get<1>();
|
||||
float_type xi_hat = some_peaks_over_threshold(args).template get<2>();
|
||||
|
||||
return some_pot_quantile(args) - this->sign_ * beta_bar/( xi_hat - 1. ) * std::pow(
|
||||
is_same<LeftRight, left>::value ? args[quantile_probability] : 1. - args[quantile_probability]
|
||||
, -xi_hat);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sign_;
|
||||
}
|
||||
|
||||
private:
|
||||
short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::pot_tail_mean
|
||||
// tag::pot_tail_mean_prob
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct pot_tail_mean
|
||||
: depends_on<peaks_over_threshold<LeftRight>, pot_quantile<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct pot_tail_mean_prob
|
||||
: depends_on<peaks_over_threshold_prob<LeftRight>, pot_quantile_prob<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, unweighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct weighted_pot_tail_mean
|
||||
: depends_on<weighted_peaks_over_threshold<LeftRight>, weighted_pot_quantile<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
|
||||
};
|
||||
template<typename LeftRight>
|
||||
struct weighted_pot_tail_mean_prob
|
||||
: depends_on<weighted_peaks_over_threshold_prob<LeftRight>, weighted_pot_quantile_prob<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::pot_tail_mean_impl<mpl::_1, weighted, LeftRight> impl;
|
||||
};
|
||||
}
|
||||
|
||||
// pot_tail_mean<LeftRight>(with_threshold_value) -> pot_tail_mean<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_value)>
|
||||
{
|
||||
typedef tag::pot_tail_mean<LeftRight> type;
|
||||
};
|
||||
|
||||
// pot_tail_mean<LeftRight>(with_threshold_probability) -> pot_tail_mean_prob<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::pot_tail_mean<LeftRight>(with_threshold_probability)>
|
||||
{
|
||||
typedef tag::pot_tail_mean_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
// weighted_pot_tail_mean<LeftRight>(with_threshold_value) -> weighted_pot_tail_mean<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_value)>
|
||||
{
|
||||
typedef tag::weighted_pot_tail_mean<LeftRight> type;
|
||||
};
|
||||
|
||||
// weighted_pot_tail_mean<LeftRight>(with_threshold_probability) -> weighted_pot_tail_mean_prob<LeftRight>
|
||||
template<typename LeftRight>
|
||||
struct as_feature<tag::weighted_pot_tail_mean<LeftRight>(with_threshold_probability)>
|
||||
{
|
||||
typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// pot_tail_mean<LeftRight> and pot_tail_mean_prob<LeftRight> provide
|
||||
// the same feature as tail_mean
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::pot_tail_mean<LeftRight> >
|
||||
: feature_of<tag::tail_mean>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::pot_tail_mean_prob<LeftRight> >
|
||||
: feature_of<tag::tail_mean>
|
||||
{
|
||||
};
|
||||
|
||||
// So that pot_tail_mean can be automatically substituted
|
||||
// with weighted_pot_tail_mean when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::pot_tail_mean<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_pot_tail_mean<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_pot_tail_mean<LeftRight> >
|
||||
: feature_of<tag::pot_tail_mean<LeftRight> >
|
||||
{
|
||||
};
|
||||
|
||||
// So that pot_tail_mean_prob can be automatically substituted
|
||||
// with weighted_pot_tail_mean_prob when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::pot_tail_mean_prob<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_pot_tail_mean_prob<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_pot_tail_mean_prob<LeftRight> >
|
||||
: feature_of<tag::pot_tail_mean_prob<LeftRight> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_count.hpp
|
||||
//
|
||||
// Copyright 2008 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_COUNT_HPP_EAN_26_12_2008
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_COUNT_HPP_EAN_26_12_2008
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_window.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_count_impl
|
||||
// returns the count of elements in the rolling window
|
||||
template<typename Sample>
|
||||
struct rolling_count_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef std::size_t result_type;
|
||||
|
||||
rolling_count_impl(dont_care)
|
||||
{}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return static_cast<std::size_t>(rolling_window_plus1(args).size()) - is_rolling_window_plus1_full(args);
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::rolling_count
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct rolling_count
|
||||
: depends_on< rolling_window_plus1 >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::rolling_count_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::rolling_count
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::rolling_count> const rolling_count = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_count)
|
||||
}
|
||||
|
||||
using extract::rolling_count;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,196 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_mean.hpp
|
||||
// Copyright (C) 2008 Eric Niebler.
|
||||
// Copyright (C) 2012 Pieter Bastiaan Ober (Integricom).
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_sum.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_count.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// lazy_rolling_mean_impl
|
||||
// returns the mean over the rolling window and is calculated only
|
||||
// when the result is requested
|
||||
template<typename Sample>
|
||||
struct lazy_rolling_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t, void, void>::result_type result_type;
|
||||
|
||||
lazy_rolling_mean_impl(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(rolling_sum(args), rolling_count(args));
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// immediate_rolling_mean_impl
|
||||
// The non-lazy version computes the rolling mean recursively when a new
|
||||
// sample is added
|
||||
template<typename Sample>
|
||||
struct immediate_rolling_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
immediate_rolling_mean_impl(Args const &args)
|
||||
: mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator()(Args const &args)
|
||||
{
|
||||
if(is_rolling_window_plus1_full(args))
|
||||
{
|
||||
if (rolling_window_plus1(args).front() > args[sample])
|
||||
mean_ -= numeric::fdiv(rolling_window_plus1(args).front()-args[sample],rolling_count(args));
|
||||
else if (rolling_window_plus1(args).front() < args[sample])
|
||||
mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
|
||||
}
|
||||
else
|
||||
{
|
||||
result_type prev_mean = mean_;
|
||||
if (prev_mean > args[sample])
|
||||
mean_ -= numeric::fdiv(prev_mean-args[sample],rolling_count(args));
|
||||
else if (prev_mean < args[sample])
|
||||
mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &) const
|
||||
{
|
||||
return mean_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & mean_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
result_type mean_;
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::lazy_rolling_mean
|
||||
// tag::immediate_rolling_mean
|
||||
// tag::rolling_mean
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct lazy_rolling_mean
|
||||
: depends_on< rolling_sum, rolling_count >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct immediate_rolling_mean
|
||||
: depends_on< rolling_window_plus1, rolling_count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
// make immediate_rolling_mean the default implementation
|
||||
struct rolling_mean : immediate_rolling_mean {};
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::lazy_rolling_mean
|
||||
// extract::immediate_rolling_mean
|
||||
// extract::rolling_mean
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {};
|
||||
extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {};
|
||||
extractor<tag::rolling_mean> const rolling_mean = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
|
||||
}
|
||||
|
||||
using extract::lazy_rolling_mean;
|
||||
using extract::immediate_rolling_mean;
|
||||
using extract::rolling_mean;
|
||||
|
||||
// rolling_mean(lazy) -> lazy_rolling_mean
|
||||
template<>
|
||||
struct as_feature<tag::rolling_mean(lazy)>
|
||||
{
|
||||
typedef tag::lazy_rolling_mean type;
|
||||
};
|
||||
|
||||
// rolling_mean(immediate) -> immediate_rolling_mean
|
||||
template<>
|
||||
struct as_feature<tag::rolling_mean(immediate)>
|
||||
{
|
||||
typedef tag::immediate_rolling_mean type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_rolling_mean provides the same feature as rolling_mean
|
||||
template<>
|
||||
struct feature_of<tag::immediate_rolling_mean>
|
||||
: feature_of<tag::rolling_mean>
|
||||
{
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// lazy_rolling_mean provides the same feature as rolling_mean
|
||||
template<>
|
||||
struct feature_of<tag::lazy_rolling_mean>
|
||||
: feature_of<tag::rolling_mean>
|
||||
{
|
||||
};
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_moment.hpp
|
||||
// Copyright 2005 Eric Niebler.
|
||||
// Copyright (C) 2014 Pieter Bastiaan Ober (Integricom).
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MOMENT_HPP_EAN_27_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MOMENT_HPP_EAN_27_11_2005
|
||||
|
||||
#include <boost/config/no_tr1/cmath.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_count.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_moment_impl
|
||||
template<typename N, typename Sample>
|
||||
struct rolling_moment_impl
|
||||
: accumulator_base
|
||||
{
|
||||
BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
rolling_moment_impl(Args const &args)
|
||||
: sum_(args[sample | Sample()])
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
if(is_rolling_window_plus1_full(args))
|
||||
{
|
||||
this->sum_ -= numeric::pow(rolling_window_plus1(args).front(), N());
|
||||
}
|
||||
this->sum_ += numeric::pow(args[sample], N());
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(this->sum_, rolling_count(args));
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum_;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type sum_;
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::rolling_moment
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<int N>
|
||||
struct rolling_moment
|
||||
: depends_on< rolling_window_plus1, rolling_count>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::rolling_moment_impl<mpl::int_<N>, mpl::_1> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::rolling_moment
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, rolling_moment, (int))
|
||||
}
|
||||
|
||||
using extract::rolling_moment;
|
||||
|
||||
// There is no weighted_rolling_moment (yet)...
|
||||
//
|
||||
//// So that rolling_moment<N> can be automatically substituted with
|
||||
//// weighted_rolling_moment<N> when the weight parameter is non-void
|
||||
//template<int N>
|
||||
//struct as_weighted_feature<tag::rolling_moment<N> >
|
||||
//{
|
||||
// typedef tag::weighted_rolling_moment<N> type;
|
||||
//};
|
||||
//
|
||||
//template<int N>
|
||||
//struct feature_of<tag::weighted_rolling_moment<N> >
|
||||
// : feature_of<tag::rolling_moment<N> >
|
||||
//{
|
||||
//};
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,98 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_sum.hpp
|
||||
//
|
||||
// Copyright 2008 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_SUM_HPP_EAN_26_12_2008
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_SUM_HPP_EAN_26_12_2008
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_window.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_sum_impl
|
||||
// returns the sum of the samples in the rolling window
|
||||
template<typename Sample>
|
||||
struct rolling_sum_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef Sample result_type;
|
||||
|
||||
template<typename Args>
|
||||
rolling_sum_impl(Args const &args)
|
||||
: sum_(args[sample | Sample()])
|
||||
{}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
if(is_rolling_window_plus1_full(args))
|
||||
{
|
||||
this->sum_ -= rolling_window_plus1(args).front();
|
||||
}
|
||||
this->sum_ += args[sample];
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const & /*args*/) const
|
||||
{
|
||||
return this->sum_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum_;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample sum_;
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::rolling_sum
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct rolling_sum
|
||||
: depends_on< rolling_window_plus1 >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::rolling_sum_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::rolling_sum
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::rolling_sum> const rolling_sum = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_sum)
|
||||
}
|
||||
|
||||
using extract::rolling_sum;
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,259 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_variance.hpp
|
||||
// Copyright (C) 2005 Eric Niebler
|
||||
// Copyright (C) 2014 Pieter Bastiaan Ober (Integricom).
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
|
||||
|
||||
#include <boost/accumulators/accumulators.hpp>
|
||||
#include <boost/accumulators/statistics/stats.hpp>
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_mean.hpp>
|
||||
#include <boost/accumulators/statistics/rolling_moment.hpp>
|
||||
|
||||
#include <boost/type_traits/is_arithmetic.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
//! Immediate (lazy) calculation of the rolling variance.
|
||||
/*!
|
||||
Calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
|
||||
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
|
||||
For a rolling window of size \f$N\f$, when \f$n <= N\f$, the variance is computed according to the formula
|
||||
\f[
|
||||
\sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2.
|
||||
\f]
|
||||
When \f$n > N\f$, the sample variance over the window becomes:
|
||||
\f[
|
||||
\sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2.
|
||||
\f]
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// lazy_rolling_variance_impl
|
||||
//
|
||||
template<typename Sample>
|
||||
struct lazy_rolling_variance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
|
||||
|
||||
lazy_rolling_variance_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
result_type mean = rolling_mean(args);
|
||||
size_t nr_samples = rolling_count(args);
|
||||
if (nr_samples < 2) return result_type();
|
||||
return nr_samples*(rolling_moment<2>(args) - mean*mean)/(nr_samples-1);
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
//! Iterative calculation of the rolling variance.
|
||||
/*!
|
||||
Iterative calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
|
||||
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
|
||||
For a rolling window of size \f$N\f$, for the first \f$N\f$ samples, the variance is computed according to the formula
|
||||
\f[
|
||||
\sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
|
||||
\f]
|
||||
where the sum of squares \f$M_{2,n}\f$ can be recursively computed as:
|
||||
\f[
|
||||
M_{2,n} = \sum_{i = 1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}),
|
||||
\f]
|
||||
and the estimate of the sample mean as:
|
||||
\f[
|
||||
\mu_n = \frac{1}{n} \sum_{i = 1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - \mu_{n-1}).
|
||||
\f]
|
||||
For further samples, when the rolling window is fully filled with data, one has to take into account that the oldest
|
||||
sample \f$x_{n-N}\f$ is dropped from the window. The sample variance over the window now becomes:
|
||||
\f[
|
||||
\sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
|
||||
\f]
|
||||
where the sum of squares \f$M_{2,n}\f$ now equals:
|
||||
\f[
|
||||
M_{2,n} = \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}) - (x_{n-N} - \mu_n)(x_{n-N} - \mu_{n-1}),
|
||||
\f]
|
||||
and the estimated mean is:
|
||||
\f[
|
||||
\mu_n = \frac{1}{N} \sum_{i = n-N+1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - x_{n-N}).
|
||||
\f]
|
||||
|
||||
Note that the sample variance is not defined for \f$n <= 1\f$.
|
||||
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// immediate_rolling_variance_impl
|
||||
//
|
||||
template<typename Sample>
|
||||
struct immediate_rolling_variance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
immediate_rolling_variance_impl(Args const &args)
|
||||
: previous_mean_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
|
||||
, sum_of_squares_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator()(Args const &args)
|
||||
{
|
||||
Sample added_sample = args[sample];
|
||||
|
||||
result_type mean = immediate_rolling_mean(args);
|
||||
sum_of_squares_ += (added_sample-mean)*(added_sample-previous_mean_);
|
||||
|
||||
if(is_rolling_window_plus1_full(args))
|
||||
{
|
||||
Sample removed_sample = rolling_window_plus1(args).front();
|
||||
sum_of_squares_ -= (removed_sample-mean)*(removed_sample-previous_mean_);
|
||||
prevent_underflow(sum_of_squares_);
|
||||
}
|
||||
previous_mean_ = mean;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
size_t nr_samples = rolling_count(args);
|
||||
if (nr_samples < 2) return result_type();
|
||||
return numeric::fdiv(sum_of_squares_,(nr_samples-1));
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & previous_mean_;
|
||||
ar & sum_of_squares_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
result_type previous_mean_;
|
||||
result_type sum_of_squares_;
|
||||
|
||||
template<typename T>
|
||||
void prevent_underflow(T &non_negative_number,typename boost::enable_if<boost::is_arithmetic<T>,T>::type* = 0)
|
||||
{
|
||||
if (non_negative_number < T(0)) non_negative_number = T(0);
|
||||
}
|
||||
template<typename T>
|
||||
void prevent_underflow(T &non_arithmetic_quantity,typename boost::disable_if<boost::is_arithmetic<T>,T>::type* = 0)
|
||||
{
|
||||
}
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag:: lazy_rolling_variance
|
||||
// tag:: immediate_rolling_variance
|
||||
// tag:: rolling_variance
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct lazy_rolling_variance
|
||||
: depends_on< rolling_count, rolling_mean, rolling_moment<2> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct immediate_rolling_variance
|
||||
: depends_on< rolling_window_plus1, rolling_count, immediate_rolling_mean>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::window_size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
// make immediate_rolling_variance the default implementation
|
||||
struct rolling_variance : immediate_rolling_variance {};
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::lazy_rolling_variance
|
||||
// extract::immediate_rolling_variance
|
||||
// extract::rolling_variance
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::lazy_rolling_variance> const lazy_rolling_variance = {};
|
||||
extractor<tag::immediate_rolling_variance> const immediate_rolling_variance = {};
|
||||
extractor<tag::rolling_variance> const rolling_variance = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_variance)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_variance)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_variance)
|
||||
}
|
||||
|
||||
using extract::lazy_rolling_variance;
|
||||
using extract::immediate_rolling_variance;
|
||||
using extract::rolling_variance;
|
||||
|
||||
// rolling_variance(lazy) -> lazy_rolling_variance
|
||||
template<>
|
||||
struct as_feature<tag::rolling_variance(lazy)>
|
||||
{
|
||||
typedef tag::lazy_rolling_variance type;
|
||||
};
|
||||
|
||||
// rolling_variance(immediate) -> immediate_rolling_variance
|
||||
template<>
|
||||
struct as_feature<tag::rolling_variance(immediate)>
|
||||
{
|
||||
typedef tag::immediate_rolling_variance type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// lazy_rolling_variance provides the same feature as rolling_variance
|
||||
template<>
|
||||
struct feature_of<tag::lazy_rolling_variance>
|
||||
: feature_of<tag::rolling_variance>
|
||||
{
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_rolling_variance provides the same feature as rolling_variance
|
||||
template<>
|
||||
struct feature_of<tag::immediate_rolling_variance>
|
||||
: feature_of<tag::rolling_variance>
|
||||
{
|
||||
};
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,221 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_window.hpp
|
||||
//
|
||||
// Copyright 2008 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_WINDOW_HPP_EAN_26_12_2008
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_WINDOW_HPP_EAN_26_12_2008
|
||||
|
||||
#include <cstddef>
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/circular_buffer.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/parameters/accumulator.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/serialization/split_free.hpp>
|
||||
|
||||
namespace boost { namespace serialization {
|
||||
|
||||
// implement serialization for boost::circular_buffer
|
||||
template <class Archive, class T>
|
||||
void save(Archive& ar, const circular_buffer<T>& b, const unsigned int /* version */)
|
||||
{
|
||||
typename circular_buffer<T>::size_type size = b.size();
|
||||
ar << b.capacity();
|
||||
ar << size;
|
||||
const typename circular_buffer<T>::const_array_range one = b.array_one();
|
||||
const typename circular_buffer<T>::const_array_range two = b.array_two();
|
||||
ar.save_binary(one.first, one.second*sizeof(T));
|
||||
ar.save_binary(two.first, two.second*sizeof(T));
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void load(Archive& ar, circular_buffer<T>& b, const unsigned int /* version */)
|
||||
{
|
||||
typename circular_buffer<T>::capacity_type capacity;
|
||||
typename circular_buffer<T>::size_type size;
|
||||
ar >> capacity;
|
||||
b.set_capacity(capacity);
|
||||
ar >> size;
|
||||
b.clear();
|
||||
const typename circular_buffer<T>::pointer buff = new T[size*sizeof(T)];
|
||||
ar.load_binary(buff, size*sizeof(T));
|
||||
b.insert(b.begin(), buff, buff+size);
|
||||
delete[] buff;
|
||||
}
|
||||
|
||||
template<class Archive, class T>
|
||||
inline void serialize(Archive & ar, circular_buffer<T>& b, const unsigned int version)
|
||||
{
|
||||
split_free(ar, b, version);
|
||||
}
|
||||
|
||||
} } // end namespace boost::serialization
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::rolling_window::size named parameter
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, rolling_window_size, window_size)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window_size)
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_window_plus1_impl
|
||||
// stores the latest N+1 samples, where N is specified at construction time
|
||||
// with the rolling_window_size named parameter
|
||||
template<typename Sample>
|
||||
struct rolling_window_plus1_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename circular_buffer<Sample>::const_iterator const_iterator;
|
||||
typedef iterator_range<const_iterator> result_type;
|
||||
|
||||
template<typename Args>
|
||||
rolling_window_plus1_impl(Args const & args)
|
||||
: buffer_(args[rolling_window_size] + 1)
|
||||
{}
|
||||
|
||||
#if BOOST_VERSION < 103600
|
||||
// Before Boost 1.36, copying a circular buffer didn't copy
|
||||
// it's capacity, and we need that behavior.
|
||||
rolling_window_plus1_impl(rolling_window_plus1_impl const &that)
|
||||
: buffer_(that.buffer_)
|
||||
{
|
||||
this->buffer_.set_capacity(that.buffer_.capacity());
|
||||
}
|
||||
|
||||
rolling_window_plus1_impl &operator =(rolling_window_plus1_impl const &that)
|
||||
{
|
||||
this->buffer_ = that.buffer_;
|
||||
this->buffer_.set_capacity(that.buffer_.capacity());
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->buffer_.push_back(args[sample]);
|
||||
}
|
||||
|
||||
bool full() const
|
||||
{
|
||||
return this->buffer_.full();
|
||||
}
|
||||
|
||||
// The result of a shifted rolling window is the range including
|
||||
// everything except the most recently added element.
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return result_type(this->buffer_.begin(), this->buffer_.end());
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version)
|
||||
{
|
||||
ar & buffer_;
|
||||
}
|
||||
|
||||
private:
|
||||
circular_buffer<Sample> buffer_;
|
||||
};
|
||||
|
||||
template<typename Args>
|
||||
bool is_rolling_window_plus1_full(Args const &args)
|
||||
{
|
||||
return find_accumulator<tag::rolling_window_plus1>(args[accumulator]).full();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// rolling_window_impl
|
||||
// stores the latest N samples, where N is specified at construction type
|
||||
// with the rolling_window_size named parameter
|
||||
template<typename Sample>
|
||||
struct rolling_window_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename circular_buffer<Sample>::const_iterator const_iterator;
|
||||
typedef iterator_range<const_iterator> result_type;
|
||||
|
||||
rolling_window_impl(dont_care)
|
||||
{}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return rolling_window_plus1(args).advance_begin(is_rolling_window_plus1_full(args));
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::rolling_window_plus1
|
||||
// tag::rolling_window
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct rolling_window_plus1
|
||||
: depends_on<>
|
||||
, tag::rolling_window_size
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::rolling_window_plus1_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct rolling_window
|
||||
: depends_on< rolling_window_plus1 >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::rolling_window_impl< mpl::_1 > impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::rolling_window::size named parameter
|
||||
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::rolling_window_plus1
|
||||
// extract::rolling_window
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::rolling_window_plus1> const rolling_window_plus1 = {};
|
||||
extractor<tag::rolling_window> const rolling_window = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window_plus1)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window)
|
||||
}
|
||||
|
||||
using extract::rolling_window_plus1;
|
||||
using extract::rolling_window;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,118 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// skewness.hpp
|
||||
//
|
||||
// Copyright 2006 Olivier Gygi, Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_SKEWNESS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_SKEWNESS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <limits>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// skewness_impl
|
||||
/**
|
||||
@brief Skewness estimation
|
||||
|
||||
The skewness of a sample distribution is defined as the ratio of the 3rd central moment and the \f$ 3/2 \f$-th power
|
||||
of the 2nd central moment (the variance) of the samples 3. The skewness can also be expressed by the simple moments:
|
||||
|
||||
\f[
|
||||
\hat{g}_1 =
|
||||
\frac
|
||||
{\widehat{m}_n^{(3)}-3\widehat{m}_n^{(2)}\hat{\mu}_n+2\hat{\mu}_n^3}
|
||||
{\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^{3/2}}
|
||||
\f]
|
||||
|
||||
where \f$ \widehat{m}_n^{(i)} \f$ are the \f$ i \f$-th moment and \f$ \hat{\mu}_n \f$ the mean (first moment) of the
|
||||
\f$ n \f$ samples.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct skewness_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, Sample>::result_type result_type;
|
||||
|
||||
skewness_impl(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(
|
||||
accumulators::moment<3>(args)
|
||||
- 3. * accumulators::moment<2>(args) * mean(args)
|
||||
+ 2. * mean(args) * mean(args) * mean(args)
|
||||
, ( accumulators::moment<2>(args) - mean(args) * mean(args) )
|
||||
* std::sqrt( accumulators::moment<2>(args) - mean(args) * mean(args) )
|
||||
);
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::skewness
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct skewness
|
||||
: depends_on<mean, moment<2>, moment<3> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::skewness_impl<mpl::_1> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::skewness
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::skewness> const skewness = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(skewness)
|
||||
}
|
||||
|
||||
using extract::skewness;
|
||||
|
||||
// So that skewness can be automatically substituted with
|
||||
// weighted_skewness when the weight parameter is non-void
|
||||
template<>
|
||||
struct as_weighted_feature<tag::skewness>
|
||||
{
|
||||
typedef tag::weighted_skewness type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_skewness>
|
||||
: feature_of<tag::skewness>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \file stats.hpp
|
||||
/// Contains the stats<> template.
|
||||
///
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_STATS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_STATS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// An MPL sequence of statistics.
|
||||
template<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, typename Stat)>
|
||||
struct stats
|
||||
: mpl::vector<BOOST_PP_ENUM_PARAMS(BOOST_ACCUMULATORS_MAX_FEATURES, Stat)>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,146 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// sum.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_SUM_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_SUM_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/parameters/weight.hpp>
|
||||
#include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// sum_impl
|
||||
template<typename Sample, typename Tag>
|
||||
struct sum_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef Sample result_type;
|
||||
|
||||
template<typename Args>
|
||||
sum_impl(Args const &args)
|
||||
: sum(args[parameter::keyword<Tag>::get() | Sample()])
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
// what about overflow?
|
||||
this->sum += args[parameter::keyword<Tag>::get()];
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->sum;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample sum;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::sum
|
||||
// tag::sum_of_weights
|
||||
// tag::sum_of_variates
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct sum
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::sum_impl<mpl::_1, tag::sample> impl;
|
||||
};
|
||||
|
||||
struct sum_of_weights
|
||||
: depends_on<>
|
||||
{
|
||||
typedef mpl::true_ is_weight_accumulator;
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::sum_impl<mpl::_2, tag::weight> impl;
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct sum_of_variates
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::sum_impl<VariateType, VariateTag> > impl;
|
||||
};
|
||||
|
||||
struct abstract_sum_of_variates
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::sum
|
||||
// extract::sum_of_weights
|
||||
// extract::sum_of_variates
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::sum> const sum = {};
|
||||
extractor<tag::sum_of_weights> const sum_of_weights = {};
|
||||
extractor<tag::abstract_sum_of_variates> const sum_of_variates = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates)
|
||||
}
|
||||
|
||||
using extract::sum;
|
||||
using extract::sum_of_weights;
|
||||
using extract::sum_of_variates;
|
||||
|
||||
// So that mean can be automatically substituted with
|
||||
// weighted_mean when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::sum>
|
||||
{
|
||||
typedef tag::weighted_sum type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_sum>
|
||||
: feature_of<tag::sum>
|
||||
{};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::sum_of_variates<VariateType, VariateTag> >
|
||||
: feature_of<tag::abstract_sum_of_variates>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,196 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// sum_kahan.hpp
|
||||
//
|
||||
// Copyright 2010 Gaetano Mendola, 2011 Simon West. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_SUM_KAHAN_HPP_EAN_26_07_2010
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_SUM_KAHAN_HPP_EAN_26_07_2010
|
||||
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_sum_kahan.hpp>
|
||||
#include <boost/numeric/conversion/cast.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
|
||||
#if _MSC_VER > 1400
|
||||
# pragma float_control(push)
|
||||
# pragma float_control(precise, on)
|
||||
#endif
|
||||
|
||||
template<typename Sample, typename Tag>
|
||||
struct sum_kahan_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef Sample result_type;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// sum_kahan_impl
|
||||
/**
|
||||
@brief Kahan summation algorithm
|
||||
|
||||
The Kahan summation algorithm reduces the numerical error obtained with standard
|
||||
sequential sum.
|
||||
|
||||
*/
|
||||
template<typename Args>
|
||||
sum_kahan_impl(Args const & args)
|
||||
: sum(args[parameter::keyword<Tag>::get() | Sample()]),
|
||||
compensation(boost::numeric_cast<Sample>(0.0))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void
|
||||
#if BOOST_ACCUMULATORS_GCC_VERSION > 40305
|
||||
__attribute__((__optimize__("no-associative-math")))
|
||||
#endif
|
||||
operator ()(Args const & args)
|
||||
{
|
||||
const Sample myTmp1 = args[parameter::keyword<Tag>::get()] - this->compensation;
|
||||
const Sample myTmp2 = this->sum + myTmp1;
|
||||
this->compensation = (myTmp2 - this->sum) - myTmp1;
|
||||
this->sum = myTmp2;
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->sum;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
ar & compensation;
|
||||
}
|
||||
|
||||
private:
|
||||
Sample sum;
|
||||
Sample compensation;
|
||||
};
|
||||
|
||||
#if _MSC_VER > 1400
|
||||
# pragma float_control(pop)
|
||||
#endif
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::sum_kahan
|
||||
// tag::sum_of_weights_kahan
|
||||
// tag::sum_of_variates_kahan
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
|
||||
struct sum_kahan
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef impl::sum_kahan_impl< mpl::_1, tag::sample > impl;
|
||||
};
|
||||
|
||||
struct sum_of_weights_kahan
|
||||
: depends_on<>
|
||||
{
|
||||
typedef mpl::true_ is_weight_accumulator;
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::sum_kahan_impl<mpl::_2, tag::weight> impl;
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct sum_of_variates_kahan
|
||||
: depends_on<>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::sum_kahan_impl<VariateType, VariateTag> > impl;
|
||||
};
|
||||
|
||||
} // namespace tag
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::sum_kahan
|
||||
// extract::sum_of_weights_kahan
|
||||
// extract::sum_of_variates_kahan
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::sum_kahan> const sum_kahan = {};
|
||||
extractor<tag::sum_of_weights_kahan> const sum_of_weights_kahan = {};
|
||||
extractor<tag::abstract_sum_of_variates> const sum_of_variates_kahan = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_kahan)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights_kahan)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates_kahan)
|
||||
} // namespace extract
|
||||
|
||||
using extract::sum_kahan;
|
||||
using extract::sum_of_weights_kahan;
|
||||
using extract::sum_of_variates_kahan;
|
||||
|
||||
// sum(kahan) -> sum_kahan
|
||||
template<>
|
||||
struct as_feature<tag::sum(kahan)>
|
||||
{
|
||||
typedef tag::sum_kahan type;
|
||||
};
|
||||
|
||||
// sum_of_weights(kahan) -> sum_of_weights_kahan
|
||||
template<>
|
||||
struct as_feature<tag::sum_of_weights(kahan)>
|
||||
{
|
||||
typedef tag::sum_of_weights_kahan type;
|
||||
};
|
||||
|
||||
// So that sum_kahan can be automatically substituted with
|
||||
// weighted_sum_kahan when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::sum_kahan>
|
||||
{
|
||||
typedef tag::weighted_sum_kahan type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct feature_of<tag::weighted_sum_kahan>
|
||||
: feature_of<tag::sum>
|
||||
{};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// sum_kahan provides the same feature as sum
|
||||
template<>
|
||||
struct feature_of<tag::sum_kahan>
|
||||
: feature_of<tag::sum>
|
||||
{
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// sum_of_weights_kahan provides the same feature as sum_of_weights
|
||||
template<>
|
||||
struct feature_of<tag::sum_of_weights_kahan>
|
||||
: feature_of<tag::sum_of_weights>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::sum_of_variates_kahan<VariateType, VariateTag> >
|
||||
: feature_of<tag::abstract_sum_of_variates>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler, Michael Gauckler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_TAIL_HPP_EAN_28_10_2005
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/mpl/or.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/iterator/reverse_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// cache_size named parameters
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, right_tail_cache_size, cache_size)
|
||||
BOOST_PARAMETER_NESTED_KEYWORD(tag, left_tail_cache_size, cache_size)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(right_tail_cache_size)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(left_tail_cache_size)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_range
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename ElementIterator, typename IndexIterator>
|
||||
struct tail_range
|
||||
{
|
||||
typedef boost::iterator_range<
|
||||
boost::reverse_iterator<boost::permutation_iterator<ElementIterator, IndexIterator> >
|
||||
> type;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// make_tail_range
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename ElementIterator, typename IndexIterator>
|
||||
typename tail_range<ElementIterator, IndexIterator>::type
|
||||
make_tail_range(ElementIterator elem_begin, IndexIterator index_begin, IndexIterator index_end)
|
||||
{
|
||||
return boost::make_iterator_range(
|
||||
boost::make_reverse_iterator(
|
||||
boost::make_permutation_iterator(elem_begin, index_end)
|
||||
)
|
||||
, boost::make_reverse_iterator(
|
||||
boost::make_permutation_iterator(elem_begin, index_begin)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// stat_assign_visitor
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Args>
|
||||
struct stat_assign_visitor
|
||||
{
|
||||
stat_assign_visitor(Args const &a, std::size_t i)
|
||||
: args(a)
|
||||
, index(i)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Stat>
|
||||
void operator ()(Stat &stat) const
|
||||
{
|
||||
stat.assign(this->args, this->index);
|
||||
}
|
||||
|
||||
private:
|
||||
stat_assign_visitor &operator =(stat_assign_visitor const &);
|
||||
Args const &args;
|
||||
std::size_t index;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// stat_assign
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Args>
|
||||
inline stat_assign_visitor<Args> const stat_assign(Args const &args, std::size_t index)
|
||||
{
|
||||
return stat_assign_visitor<Args>(args, index);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// is_tail_variate_feature
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename Stat, typename LeftRight>
|
||||
struct is_tail_variate_feature
|
||||
: mpl::false_
|
||||
{
|
||||
};
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename VariateType, typename VariateTag, typename LeftRight>
|
||||
struct is_tail_variate_feature<tag::tail_variate<VariateType, VariateTag, LeftRight>, LeftRight>
|
||||
: mpl::true_
|
||||
{
|
||||
};
|
||||
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
template<typename LeftRight>
|
||||
struct is_tail_variate_feature<tag::tail_weights<LeftRight>, LeftRight>
|
||||
: mpl::true_
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_impl
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct tail_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// LeftRight must be either right or left
|
||||
BOOST_MPL_ASSERT((
|
||||
mpl::or_<is_same<LeftRight, right>, is_same<LeftRight, left> >
|
||||
));
|
||||
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<LeftRight, right>
|
||||
, numeric::functional::greater<Sample const, Sample const>
|
||||
, numeric::functional::less<Sample const, Sample const>
|
||||
>::type
|
||||
predicate_type;
|
||||
|
||||
// for boost::result_of
|
||||
typedef typename detail::tail_range<
|
||||
typename std::vector<Sample>::const_iterator
|
||||
, std::vector<std::size_t>::iterator
|
||||
>::type result_type;
|
||||
|
||||
template<typename Args>
|
||||
tail_impl(Args const &args)
|
||||
: is_sorted(false)
|
||||
, indices()
|
||||
, samples(args[tag::tail<LeftRight>::cache_size], args[sample | Sample()])
|
||||
{
|
||||
this->indices.reserve(this->samples.size());
|
||||
}
|
||||
|
||||
tail_impl(tail_impl const &that)
|
||||
: is_sorted(that.is_sorted)
|
||||
, indices(that.indices)
|
||||
, samples(that.samples)
|
||||
{
|
||||
this->indices.reserve(this->samples.size());
|
||||
}
|
||||
|
||||
// This just stores the heap and the samples.
|
||||
// In operator()() below, if we are adding a new sample
|
||||
// to the sample cache, we force all the
|
||||
// tail_variates to update also. (It's not
|
||||
// good enough to wait for the accumulator_set to do it
|
||||
// for us because then information about whether a sample
|
||||
// was stored and where is lost, and would need to be
|
||||
// queried at runtime, which would be slow.) This is
|
||||
// implemented as a filtered visitation over the stats,
|
||||
// which we can access because args[accumulator] gives us
|
||||
// all the stats.
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
if(this->indices.size() < this->samples.size())
|
||||
{
|
||||
this->indices.push_back(this->indices.size());
|
||||
this->assign(args, this->indices.back());
|
||||
}
|
||||
else if(predicate_type()(args[sample], this->samples[this->indices[0]]))
|
||||
{
|
||||
std::pop_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
|
||||
this->assign(args, this->indices.back());
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
if(!this->is_sorted)
|
||||
{
|
||||
// Must use the same predicate here as in push_heap/pop_heap above.
|
||||
std::sort_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
|
||||
// sort_heap puts elements in reverse order. Calling std::reverse
|
||||
// turns the sorted sequence back into a valid heap.
|
||||
std::reverse(this->indices.begin(), this->indices.end());
|
||||
this->is_sorted = true;
|
||||
}
|
||||
|
||||
return detail::make_tail_range(
|
||||
this->samples.begin()
|
||||
, this->indices.begin()
|
||||
, this->indices.end()
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct is_tail_variate
|
||||
{
|
||||
template<typename T>
|
||||
struct apply
|
||||
: detail::is_tail_variate_feature<
|
||||
typename detail::feature_tag<T>::type
|
||||
, LeftRight
|
||||
>
|
||||
{};
|
||||
};
|
||||
|
||||
template<typename Args>
|
||||
void assign(Args const &args, std::size_t index)
|
||||
{
|
||||
BOOST_ASSERT(index < this->samples.size());
|
||||
this->samples[index] = args[sample];
|
||||
std::push_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
|
||||
this->is_sorted = false;
|
||||
// Tell the tail variates to store their values also
|
||||
args[accumulator].template visit_if<is_tail_variate>(detail::stat_assign(args, index));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
struct indirect_cmp
|
||||
{
|
||||
typedef std::size_t first_argument_type;
|
||||
typedef std::size_t second_argument_type;
|
||||
typedef bool result_type;
|
||||
|
||||
indirect_cmp(std::vector<Sample> const &s)
|
||||
: samples(s)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator ()(std::size_t left, std::size_t right) const
|
||||
{
|
||||
return predicate_type()(this->samples[left], this->samples[right]);
|
||||
}
|
||||
|
||||
private:
|
||||
indirect_cmp &operator =(indirect_cmp const &);
|
||||
std::vector<Sample> const &samples;
|
||||
};
|
||||
|
||||
public:
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & is_sorted;
|
||||
ar & indices;
|
||||
ar & samples;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable bool is_sorted;
|
||||
mutable std::vector<std::size_t> indices;
|
||||
std::vector<Sample> samples;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
// TODO The templatized tag::tail below should inherit from the correct named parameter.
|
||||
// The following lines provide a workaround, but there must be a better way of doing this.
|
||||
template<typename T>
|
||||
struct tail_cache_size_named_arg
|
||||
{
|
||||
};
|
||||
template<>
|
||||
struct tail_cache_size_named_arg<left>
|
||||
: tag::left_tail_cache_size
|
||||
{
|
||||
};
|
||||
template<>
|
||||
struct tail_cache_size_named_arg<right>
|
||||
: tag::right_tail_cache_size
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::tail<>
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct tail
|
||||
: depends_on<>
|
||||
, tail_cache_size_named_arg<LeftRight>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::tail_impl<mpl::_1, LeftRight> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
/// tag::tail<LeftRight>::cache_size named parameter
|
||||
static boost::parameter::keyword<tail_cache_size_named_arg<LeftRight> > const cache_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct abstract_tail
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::tail
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_tail> const tail = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail)
|
||||
}
|
||||
|
||||
using extract::tail;
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::tail<LeftRight> >
|
||||
: feature_of<tag::abstract_tail>
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,254 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_mean.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_MEAN_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_TAIL_MEAN_HPP_DE_01_01_2006
|
||||
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/accumulators/statistics/tail_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// coherent_tail_mean_impl
|
||||
//
|
||||
/**
|
||||
@brief Estimation of the coherent tail mean based on order statistics (for both left and right tails)
|
||||
|
||||
The coherent tail mean \f$\widehat{CTM}_{n,\alpha}(X)\f$ is equal to the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$
|
||||
plus a correction term that ensures coherence in case of non-continuous distributions.
|
||||
|
||||
\f[
|
||||
\widehat{CTM}_{n,\alpha}^{\mathrm{right}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) +
|
||||
\frac{1}{\lceil n(1-\alpha)\rceil}\hat{q}_{n,\alpha}(X)\left(1 - \alpha - \frac{1}{n}\lceil n(1-\alpha)\rceil \right)
|
||||
\f]
|
||||
|
||||
\f[
|
||||
\widehat{CTM}_{n,\alpha}^{\mathrm{left}}(X) = \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) +
|
||||
\frac{1}{\lceil n\alpha\rceil}\hat{q}_{n,\alpha}(X)\left(\alpha - \frac{1}{n}\lceil n\alpha\rceil \right)
|
||||
\f]
|
||||
*/
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct coherent_tail_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
coherent_tail_mean_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
std::size_t n = static_cast<std::size_t>(
|
||||
std::ceil(
|
||||
cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
||||
)
|
||||
);
|
||||
|
||||
extractor<tag::non_coherent_tail_mean<LeftRight> > const some_non_coherent_tail_mean = {};
|
||||
|
||||
return some_non_coherent_tail_mean(args)
|
||||
+ numeric::fdiv(quantile(args), n)
|
||||
* (
|
||||
( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability]
|
||||
- numeric::fdiv(n, count(args))
|
||||
);
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// non_coherent_tail_mean_impl
|
||||
//
|
||||
/**
|
||||
@brief Estimation of the (non-coherent) tail mean based on order statistics (for both left and right tails)
|
||||
|
||||
An estimation of the non-coherent tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the mean of the
|
||||
\f$\lceil n\alpha\rceil\f$ smallest samples (left tail) or the mean of the \f$\lceil n(1-\alpha)\rceil\f$
|
||||
largest samples (right tail), \f$n\f$ being the total number of samples and \f$\alpha\f$ the quantile level:
|
||||
|
||||
\f[
|
||||
\widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{1}{\lceil n(1-\alpha)\rceil} \sum_{i=\lceil \alpha n \rceil}^n X_{i:n}
|
||||
\f]
|
||||
|
||||
\f[
|
||||
\widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{1}{\lceil n\alpha\rceil} \sum_{i=1}^{\lceil \alpha n \rceil} X_{i:n}
|
||||
\f]
|
||||
|
||||
It thus requires the caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$
|
||||
largest samples.
|
||||
|
||||
@param quantile_probability
|
||||
*/
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct non_coherent_tail_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
non_coherent_tail_mean_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
std::size_t n = static_cast<std::size_t>(
|
||||
std::ceil(
|
||||
cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
||||
)
|
||||
);
|
||||
|
||||
// If n is in a valid range, return result, otherwise return NaN or throw exception
|
||||
if (n <= static_cast<std::size_t>(tail(args).size()))
|
||||
return numeric::fdiv(
|
||||
std::accumulate(
|
||||
tail(args).begin()
|
||||
, tail(args).begin() + n
|
||||
, Sample(0)
|
||||
)
|
||||
, n
|
||||
);
|
||||
else
|
||||
{
|
||||
if (std::numeric_limits<result_type>::has_quiet_NaN)
|
||||
{
|
||||
return std::numeric_limits<result_type>::quiet_NaN();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
||||
boost::throw_exception(std::runtime_error(msg.str()));
|
||||
return Sample(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::coherent_tail_mean<>
|
||||
// tag::non_coherent_tail_mean<>
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct coherent_tail_mean
|
||||
: depends_on<count, quantile, non_coherent_tail_mean<LeftRight> >
|
||||
{
|
||||
typedef accumulators::impl::coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct non_coherent_tail_mean
|
||||
: depends_on<count, tail<LeftRight> >
|
||||
{
|
||||
typedef accumulators::impl::non_coherent_tail_mean_impl<mpl::_1, LeftRight> impl;
|
||||
};
|
||||
|
||||
struct abstract_non_coherent_tail_mean
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::non_coherent_tail_mean;
|
||||
// extract::coherent_tail_mean;
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_tail_mean = {};
|
||||
extractor<tag::tail_mean> const coherent_tail_mean = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_tail_mean)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(coherent_tail_mean)
|
||||
}
|
||||
|
||||
using extract::non_coherent_tail_mean;
|
||||
using extract::coherent_tail_mean;
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// coherent_tail_mean<LeftRight> provides the same feature as tail_mean
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::coherent_tail_mean<LeftRight> >
|
||||
: feature_of<tag::tail_mean>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::non_coherent_tail_mean<LeftRight> >
|
||||
: feature_of<tag::abstract_non_coherent_tail_mean>
|
||||
{
|
||||
};
|
||||
|
||||
// So that non_coherent_tail_mean can be automatically substituted
|
||||
// with weighted_non_coherent_tail_mean when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::non_coherent_tail_mean<LeftRight> >
|
||||
{
|
||||
typedef tag::non_coherent_weighted_tail_mean<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::non_coherent_weighted_tail_mean<LeftRight> >
|
||||
: feature_of<tag::non_coherent_tail_mean<LeftRight> >
|
||||
{};
|
||||
|
||||
// NOTE that non_coherent_tail_mean cannot be feature-grouped with tail_mean,
|
||||
// which is the base feature for coherent tail means, since (at least for
|
||||
// non-continuous distributions) non_coherent_tail_mean is a different measure!
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,162 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_quantile.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <boost/config/no_tr1/cmath.hpp> // For ceil
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_quantile_impl
|
||||
// Tail quantile estimation based on order statistics
|
||||
/**
|
||||
@brief Tail quantile estimation based on order statistics (for both left and right tails)
|
||||
|
||||
The estimation of a tail quantile \f$\hat{q}\f$ with level \f$\alpha\f$ based on order statistics requires the
|
||||
caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$ largest samples,
|
||||
\f$n\f$ being the total number of samples. The largest of the \f$\lceil n\alpha\rceil\f$ smallest samples or the
|
||||
smallest of the \f$\lceil n(1-\alpha)\rceil\f$ largest samples provides an estimate for the quantile:
|
||||
|
||||
\f[
|
||||
\hat{q}_{n,\alpha} = X_{\lceil \alpha n \rceil:n}
|
||||
\f]
|
||||
|
||||
@param quantile_probability
|
||||
*/
|
||||
template<typename Sample, typename LeftRight>
|
||||
struct tail_quantile_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef Sample result_type;
|
||||
|
||||
tail_quantile_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
std::size_t n = static_cast<std::size_t>(
|
||||
std::ceil(
|
||||
cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
||||
)
|
||||
);
|
||||
|
||||
// If n is in a valid range, return result, otherwise return NaN or throw exception
|
||||
if ( n < static_cast<std::size_t>(tail(args).size()))
|
||||
{
|
||||
// Note that the cached samples of the left are sorted in ascending order,
|
||||
// whereas the samples of the right tail are sorted in descending order
|
||||
return *(boost::begin(tail(args)) + n - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (std::numeric_limits<result_type>::has_quiet_NaN)
|
||||
{
|
||||
return std::numeric_limits<result_type>::quiet_NaN();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
||||
boost::throw_exception(std::runtime_error(msg.str()));
|
||||
return Sample(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::tail_quantile<>
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight>
|
||||
struct tail_quantile
|
||||
: depends_on<count, tail<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::tail_quantile
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::quantile> const tail_quantile = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
|
||||
}
|
||||
|
||||
using extract::tail_quantile;
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// tail_quantile<LeftRight> provide the same feature as quantile
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::tail_quantile<LeftRight> >
|
||||
: feature_of<tag::quantile>
|
||||
{
|
||||
};
|
||||
|
||||
// So that tail_quantile can be automatically substituted with
|
||||
// weighted_tail_quantile when the weight parameter is non-void.
|
||||
template<typename LeftRight>
|
||||
struct as_weighted_feature<tag::tail_quantile<LeftRight> >
|
||||
{
|
||||
typedef tag::weighted_tail_quantile<LeftRight> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::weighted_tail_quantile<LeftRight> >
|
||||
: feature_of<tag::tail_quantile<LeftRight> >
|
||||
{};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,150 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_variate.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler, Michael Gauckler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_STAT_STATISTICS_TAIL_VARIATE_HPP_EAN_28_10_2005
|
||||
#define BOOST_STAT_STATISTICS_TAIL_VARIATE_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/mpl/always.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/iterator/reverse_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_variate_impl
|
||||
template<typename VariateType, typename VariateTag, typename LeftRight>
|
||||
struct tail_variate_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef
|
||||
typename detail::tail_range<
|
||||
typename std::vector<VariateType>::const_iterator
|
||||
, std::vector<std::size_t>::iterator
|
||||
>::type
|
||||
result_type;
|
||||
|
||||
template<typename Args>
|
||||
tail_variate_impl(Args const &args)
|
||||
: variates(args[tag::tail<LeftRight>::cache_size], args[parameter::keyword<VariateTag>::get() | VariateType()])
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void assign(Args const &args, std::size_t index)
|
||||
{
|
||||
this->variates[index] = args[parameter::keyword<VariateTag>::get()];
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
// getting the order result causes the indices vector to be sorted.
|
||||
extractor<tag::tail<LeftRight> > const some_tail = {};
|
||||
return this->do_result(some_tail(args));
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename TailRng>
|
||||
result_type do_result(TailRng const &rng) const
|
||||
{
|
||||
return detail::make_tail_range(
|
||||
this->variates.begin()
|
||||
, rng.end().base().base() // the index iterator
|
||||
, rng.begin().base().base() // (begin and end reversed because these are reverse iterators)
|
||||
);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & variates;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<VariateType> variates;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::tail_variate<>
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename VariateType, typename VariateTag, typename LeftRight>
|
||||
struct tail_variate
|
||||
: depends_on<tail<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef mpl::always<accumulators::impl::tail_variate_impl<VariateType, VariateTag, LeftRight> > impl;
|
||||
};
|
||||
|
||||
struct abstract_tail_variate
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct tail_weights
|
||||
: depends_on<tail<LeftRight> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::tail_variate_impl<mpl::_2, tag::weight, LeftRight> impl;
|
||||
};
|
||||
|
||||
struct abstract_tail_weights
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::tail_variate
|
||||
// extract::tail_weights
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_tail_variate> const tail_variate = {};
|
||||
extractor<tag::abstract_tail_weights> const tail_weights = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_weights)
|
||||
}
|
||||
|
||||
using extract::tail_variate;
|
||||
using extract::tail_weights;
|
||||
|
||||
template<typename VariateType, typename VariateTag, typename LeftRight>
|
||||
struct feature_of<tag::tail_variate<VariateType, VariateTag, LeftRight> >
|
||||
: feature_of<tag::abstract_tail_variate>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename LeftRight>
|
||||
struct feature_of<tag::tail_weights<LeftRight> >
|
||||
{
|
||||
typedef tag::abstract_tail_weights type;
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,270 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_variate_means.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006
|
||||
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/tail.hpp>
|
||||
#include <boost/accumulators/statistics/tail_variate.hpp>
|
||||
#include <boost/accumulators/statistics/tail_mean.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
/**
|
||||
@brief Estimation of the absolute and relative tail variate means (for both left and right tails)
|
||||
|
||||
For all \f$j\f$-th variates associated to the \f$\lceil n(1-\alpha)\rceil\f$ largest samples (or the
|
||||
\f$\lceil n(1-\alpha)\rceil\f$ smallest samples in case of the left tail), the absolute tail means
|
||||
\f$\widehat{ATM}_{n,\alpha}(X, j)\f$ are computed and returned as an iterator range. Alternatively,
|
||||
the relative tail means \f$\widehat{RTM}_{n,\alpha}(X, j)\f$ are returned, which are the absolute
|
||||
tail means normalized with the (non-coherent) sample tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$.
|
||||
|
||||
\f[
|
||||
\widehat{ATM}_{n,\alpha}^{\mathrm{right}}(X, j) =
|
||||
\frac{1}{\lceil n(1-\alpha) \rceil}
|
||||
\sum_{i=\lceil \alpha n \rceil}^n \xi_{j,i}
|
||||
\f]
|
||||
|
||||
\f[
|
||||
\widehat{ATM}_{n,\alpha}^{\mathrm{left}}(X, j) =
|
||||
\frac{1}{\lceil n\alpha \rceil}
|
||||
\sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}
|
||||
\f]
|
||||
|
||||
\f[
|
||||
\widehat{RTM}_{n,\alpha}^{\mathrm{right}}(X, j) =
|
||||
\frac{\sum_{i=\lceil n\alpha \rceil}^n \xi_{j,i}}
|
||||
{\lceil n(1-\alpha)\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X)}
|
||||
\f]
|
||||
|
||||
\f[
|
||||
\widehat{RTM}_{n,\alpha}^{\mathrm{left}}(X, j) =
|
||||
\frac{\sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}}
|
||||
{\lceil n\alpha\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X)}
|
||||
\f]
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tail_variate_means_impl
|
||||
// by default: absolute tail_variate_means
|
||||
template<typename Sample, typename Impl, typename LeftRight, typename VariateTag>
|
||||
struct tail_variate_means_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<typename array_type::iterator> result_type;
|
||||
|
||||
tail_variate_means_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
std::size_t n = static_cast<std::size_t>(
|
||||
std::ceil(
|
||||
cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
|
||||
)
|
||||
);
|
||||
|
||||
std::size_t num_variates = tail_variate(args).begin()->size();
|
||||
|
||||
this->tail_means_.clear();
|
||||
this->tail_means_.resize(num_variates, Sample(0));
|
||||
|
||||
// If n is in a valid range, return result, otherwise return NaN or throw exception
|
||||
if (n < static_cast<std::size_t>(tail(args).size()))
|
||||
{
|
||||
this->tail_means_ = std::accumulate(
|
||||
tail_variate(args).begin()
|
||||
, tail_variate(args).begin() + n
|
||||
, this->tail_means_
|
||||
, numeric::plus
|
||||
);
|
||||
|
||||
float_type factor = n * ( (is_same<Impl, relative>::value) ? non_coherent_tail_mean(args) : 1. );
|
||||
|
||||
std::transform(
|
||||
this->tail_means_.begin()
|
||||
, this->tail_means_.end()
|
||||
, this->tail_means_.begin()
|
||||
#ifdef BOOST_NO_CXX98_BINDERS
|
||||
, std::bind(std::divides<float_type>(), std::placeholders::_1, factor)
|
||||
#else
|
||||
, std::bind2nd(std::divides<float_type>(), factor)
|
||||
#endif
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (std::numeric_limits<float_type>::has_quiet_NaN)
|
||||
{
|
||||
std::fill(
|
||||
this->tail_means_.begin()
|
||||
, this->tail_means_.end()
|
||||
, std::numeric_limits<float_type>::quiet_NaN()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
|
||||
boost::throw_exception(std::runtime_error(msg.str()));
|
||||
}
|
||||
}
|
||||
return make_iterator_range(this->tail_means_);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & tail_means_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
mutable array_type tail_means_;
|
||||
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::absolute_tail_variate_means
|
||||
// tag::relative_tail_variate_means
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct absolute_tail_variate_means
|
||||
: depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
|
||||
{
|
||||
typedef accumulators::impl::tail_variate_means_impl<mpl::_1, absolute, LeftRight, VariateTag> impl;
|
||||
};
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct relative_tail_variate_means
|
||||
: depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
|
||||
{
|
||||
typedef accumulators::impl::tail_variate_means_impl<mpl::_1, relative, LeftRight, VariateTag> impl;
|
||||
};
|
||||
struct abstract_absolute_tail_variate_means
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
struct abstract_relative_tail_variate_means
|
||||
: depends_on<>
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::tail_variate_means
|
||||
// extract::relative_tail_variate_means
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_absolute_tail_variate_means> const tail_variate_means = {};
|
||||
extractor<tag::abstract_relative_tail_variate_means> const relative_tail_variate_means = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate_means)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(relative_tail_variate_means)
|
||||
}
|
||||
|
||||
using extract::tail_variate_means;
|
||||
using extract::relative_tail_variate_means;
|
||||
|
||||
// tail_variate_means<LeftRight, VariateType, VariateTag>(absolute) -> absolute_tail_variate_means<LeftRight, VariateType, VariateTag>
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(absolute)>
|
||||
{
|
||||
typedef tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
// tail_variate_means<LeftRight, VariateType, VariateTag>(relative) ->relative_tail_variate_means<LeftRight, VariateType, VariateTag>
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(relative)>
|
||||
{
|
||||
typedef tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
// Provides non-templatized extractor
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
: feature_of<tag::abstract_absolute_tail_variate_means>
|
||||
{
|
||||
};
|
||||
|
||||
// Provides non-templatized extractor
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
: feature_of<tag::abstract_relative_tail_variate_means>
|
||||
{
|
||||
};
|
||||
|
||||
// So that absolute_tail_means can be automatically substituted
|
||||
// with absolute_weighted_tail_means when the weight parameter is non-void.
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct as_weighted_feature<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
{
|
||||
typedef tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
: feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
{
|
||||
};
|
||||
|
||||
// So that relative_tail_means can be automatically substituted
|
||||
// with relative_weighted_tail_means when the weight parameter is non-void.
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct as_weighted_feature<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
{
|
||||
typedef tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
template<typename LeftRight, typename VariateType, typename VariateTag>
|
||||
struct feature_of<tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
: feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
|
||||
{
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// times2_iterator.hpp
|
||||
//
|
||||
// Copyright 2006 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_TIMES2_ITERATOR_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_TIMES2_ITERATOR_HPP_DE_01_01_2006
|
||||
|
||||
#include <functional>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
typedef transform_iterator<
|
||||
#ifdef BOOST_NO_CXX98_BINDERS
|
||||
decltype(std::bind(std::multiplies<std::size_t>(), 2, std::placeholders::_1))
|
||||
#else
|
||||
std::binder1st<std::multiplies<std::size_t> >
|
||||
#endif
|
||||
, counting_iterator<std::size_t>
|
||||
> times2_iterator;
|
||||
|
||||
inline times2_iterator make_times2_iterator(std::size_t i)
|
||||
{
|
||||
return make_transform_iterator(
|
||||
make_counting_iterator(i)
|
||||
#ifdef BOOST_NO_CXX98_BINDERS
|
||||
, std::bind(std::multiplies<std::size_t>(), 2, std::placeholders::_1)
|
||||
#else
|
||||
, std::bind1st(std::multiplies<std::size_t>(), 2)
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// lvalue_index_iterator
|
||||
template<typename Base>
|
||||
struct lvalue_index_iterator
|
||||
: Base
|
||||
{
|
||||
lvalue_index_iterator()
|
||||
: Base()
|
||||
{}
|
||||
|
||||
lvalue_index_iterator(Base base)
|
||||
: Base(base)
|
||||
{
|
||||
}
|
||||
|
||||
typename Base::reference operator [](typename Base::difference_type n) const
|
||||
{
|
||||
return *(*this + n);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,247 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// variance.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff, Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_VARIANCE_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_VARIANCE_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
//! Lazy calculation of variance.
|
||||
/*!
|
||||
Default sample variance implementation based on the second moment \f$ M_n^{(2)} \f$ moment<2>, mean and count.
|
||||
\f[
|
||||
\sigma_n^2 = M_n^{(2)} - \mu_n^2.
|
||||
\f]
|
||||
where
|
||||
\f[
|
||||
\mu_n = \frac{1}{n} \sum_{i = 1}^n x_i.
|
||||
\f]
|
||||
is the estimate of the sample mean and \f$n\f$ is the number of samples.
|
||||
*/
|
||||
template<typename Sample, typename MeanFeature>
|
||||
struct lazy_variance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
lazy_variance_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
extractor<MeanFeature> mean;
|
||||
result_type tmp = mean(args);
|
||||
return accumulators::moment<2>(args) - tmp * tmp;
|
||||
}
|
||||
|
||||
// serialization is done by accumulators it depends on
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version) {}
|
||||
};
|
||||
|
||||
//! Iterative calculation of variance.
|
||||
/*!
|
||||
Iterative calculation of sample variance \f$\sigma_n^2\f$ according to the formula
|
||||
\f[
|
||||
\sigma_n^2 = \frac{1}{n} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{n-1}{n} \sigma_{n-1}^2 + \frac{1}{n-1}(x_n - \mu_n)^2.
|
||||
\f]
|
||||
where
|
||||
\f[
|
||||
\mu_n = \frac{1}{n} \sum_{i = 1}^n x_i.
|
||||
\f]
|
||||
is the estimate of the sample mean and \f$n\f$ is the number of samples.
|
||||
|
||||
Note that the sample variance is not defined for \f$n <= 1\f$.
|
||||
|
||||
A simplification can be obtained by the approximate recursion
|
||||
\f[
|
||||
\sigma_n^2 \approx \frac{n-1}{n} \sigma_{n-1}^2 + \frac{1}{n}(x_n - \mu_n)^2.
|
||||
\f]
|
||||
because the difference
|
||||
\f[
|
||||
\left(\frac{1}{n-1} - \frac{1}{n}\right)(x_n - \mu_n)^2 = \frac{1}{n(n-1)}(x_n - \mu_n)^2.
|
||||
\f]
|
||||
converges to zero as \f$n \rightarrow \infty\f$. However, for small \f$ n \f$ the difference
|
||||
can be non-negligible.
|
||||
*/
|
||||
template<typename Sample, typename MeanFeature, typename Tag>
|
||||
struct variance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
variance_impl(Args const &args)
|
||||
: variance(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
if(cnt > 1)
|
||||
{
|
||||
extractor<MeanFeature> mean;
|
||||
result_type tmp = args[parameter::keyword<Tag>::get()] - mean(args);
|
||||
this->variance =
|
||||
numeric::fdiv(this->variance * (cnt - 1), cnt)
|
||||
+ numeric::fdiv(tmp * tmp, cnt - 1);
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->variance;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & variance;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type variance;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::variance
|
||||
// tag::immediate_variance
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct lazy_variance
|
||||
: depends_on<moment<2>, mean>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::lazy_variance_impl<mpl::_1, mean> impl;
|
||||
};
|
||||
|
||||
struct variance
|
||||
: depends_on<count, immediate_mean>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::variance_impl<mpl::_1, mean, sample> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::lazy_variance
|
||||
// extract::variance
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::lazy_variance> const lazy_variance = {};
|
||||
extractor<tag::variance> const variance = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_variance)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(variance)
|
||||
}
|
||||
|
||||
using extract::lazy_variance;
|
||||
using extract::variance;
|
||||
|
||||
// variance(lazy) -> lazy_variance
|
||||
template<>
|
||||
struct as_feature<tag::variance(lazy)>
|
||||
{
|
||||
typedef tag::lazy_variance type;
|
||||
};
|
||||
|
||||
// variance(immediate) -> variance
|
||||
template<>
|
||||
struct as_feature<tag::variance(immediate)>
|
||||
{
|
||||
typedef tag::variance type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_variance provides the same feature as variance
|
||||
template<>
|
||||
struct feature_of<tag::lazy_variance>
|
||||
: feature_of<tag::variance>
|
||||
{
|
||||
};
|
||||
|
||||
// So that variance can be automatically substituted with
|
||||
// weighted_variance when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::variance>
|
||||
{
|
||||
typedef tag::weighted_variance type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// weighted_variance provides the same feature as variance
|
||||
template<>
|
||||
struct feature_of<tag::weighted_variance>
|
||||
: feature_of<tag::variance>
|
||||
{
|
||||
};
|
||||
|
||||
// So that immediate_variance can be automatically substituted with
|
||||
// immediate_weighted_variance when the weight parameter is non-void.
|
||||
template<>
|
||||
struct as_weighted_feature<tag::lazy_variance>
|
||||
{
|
||||
typedef tag::lazy_weighted_variance type;
|
||||
};
|
||||
|
||||
// for the purposes of feature-based dependency resolution,
|
||||
// immediate_weighted_variance provides the same feature as immediate_variance
|
||||
template<>
|
||||
struct feature_of<tag::lazy_weighted_variance>
|
||||
: feature_of<tag::lazy_variance>
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//// droppable_accumulator<variance_impl>
|
||||
//// need to specialize droppable lazy variance to cache the result at the
|
||||
//// point the accumulator is dropped.
|
||||
///// INTERNAL ONLY
|
||||
/////
|
||||
//template<typename Sample, typename MeanFeature>
|
||||
//struct droppable_accumulator<impl::variance_impl<Sample, MeanFeature> >
|
||||
// : droppable_accumulator_base<
|
||||
// with_cached_result<impl::variance_impl<Sample, MeanFeature> >
|
||||
// >
|
||||
//{
|
||||
// template<typename Args>
|
||||
// droppable_accumulator(Args const &args)
|
||||
// : droppable_accumulator::base(args)
|
||||
// {
|
||||
// }
|
||||
//};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weight.hpp
|
||||
//
|
||||
// Copyright 2005 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_VARIATES_COVARIATE_HPP_EAN_03_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_VARIATES_COVARIATE_HPP_EAN_03_11_2005
|
||||
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/accumulators/accumulators_fwd.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
BOOST_PARAMETER_KEYWORD(tag, covariate1)
|
||||
BOOST_PARAMETER_KEYWORD(tag, covariate2)
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariate1)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariate2)
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,140 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_covariance.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <complex>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/numeric/ublas/io.hpp>
|
||||
#include <boost/numeric/ublas/matrix.hpp>
|
||||
#include <boost/type_traits/is_scalar.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/covariance.hpp> // for numeric::outer_product() and type traits
|
||||
#include <boost/accumulators/statistics/weighted_mean.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_covariance_impl
|
||||
//
|
||||
/**
|
||||
@brief Weighted Covariance Estimator
|
||||
|
||||
An iterative Monte Carlo estimator for the weighted covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
|
||||
and \f$X'\f$ a variate, is given by:
|
||||
|
||||
\f[
|
||||
\hat{c}_n = \frac{\bar{w}_n-w_n}{\bar{w}_n} \hat{c}_{n-1} + \frac{w_n}{\bar{w}_n-w_n}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),
|
||||
\quad n\ge2,\quad\hat{c}_1 = 0,
|
||||
\f]
|
||||
|
||||
\f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the weighted means of the samples and variates and
|
||||
\f$\bar{w}_n\f$ the sum of the \f$n\f$ first weights \f$w_i\f$.
|
||||
*/
|
||||
template<typename Sample, typename Weight, typename VariateType, typename VariateTag>
|
||||
struct weighted_covariance_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<Sample, std::size_t>::result_type>::result_type weighted_sample_type;
|
||||
typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<VariateType, std::size_t>::result_type>::result_type weighted_variate_type;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::outer_product<weighted_sample_type, weighted_variate_type>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_covariance_impl(Args const &args)
|
||||
: cov_(
|
||||
numeric::outer_product(
|
||||
numeric::fdiv(args[sample | Sample()], (std::size_t)1)
|
||||
* numeric::one<Weight>::value
|
||||
, numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
|
||||
* numeric::one<Weight>::value
|
||||
)
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
if (cnt > 1)
|
||||
{
|
||||
extractor<tag::weighted_mean_of_variates<VariateType, VariateTag> > const some_weighted_mean_of_variates = {};
|
||||
|
||||
this->cov_ = this->cov_ * (sum_of_weights(args) - args[weight]) / sum_of_weights(args)
|
||||
+ numeric::outer_product(
|
||||
some_weighted_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
|
||||
, weighted_mean(args) - args[sample]
|
||||
) * args[weight] / (sum_of_weights(args) - args[weight]);
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->cov_;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & cov_;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type cov_;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_covariance
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct weighted_covariance
|
||||
: depends_on<count, sum_of_weights, weighted_mean, weighted_mean_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
typedef accumulators::impl::weighted_covariance_impl<mpl::_1, mpl::_2, VariateType, VariateTag> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_covariance
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::abstract_covariance> const weighted_covariance = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_covariance)
|
||||
}
|
||||
|
||||
using extract::weighted_covariance;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,237 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_density.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <functional>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/max.hpp>
|
||||
#include <boost/accumulators/statistics/min.hpp>
|
||||
#include <boost/accumulators/statistics/density.hpp> // for named parameters density_cache_size and density_num_bins
|
||||
#include <boost/serialization/vector.hpp>
|
||||
#include <boost/serialization/utility.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_density_impl
|
||||
// density histogram for weighted samples
|
||||
/**
|
||||
@brief Histogram density estimator for weighted samples
|
||||
|
||||
The histogram density estimator returns a histogram of the sample distribution. The positions and sizes of the bins
|
||||
are determined using a specifiable number of cached samples (cache_size). The range between the minimum and the
|
||||
maximum of the cached samples is subdivided into a specifiable number of bins (num_bins) of same size. Additionally,
|
||||
an under- and an overflow bin is added to capture future under- and overflow samples. Once the bins are determined,
|
||||
the cached samples and all subsequent samples are added to the correct bins. At the end, a range of std::pair is
|
||||
returned, where each pair contains the position of the bin (lower bound) and the sum of the weights (normalized with the
|
||||
sum of all weights).
|
||||
|
||||
@param density_cache_size Number of first samples used to determine min and max.
|
||||
@param density_num_bins Number of bins (two additional bins collect under- and overflow samples).
|
||||
*/
|
||||
template<typename Sample, typename Weight>
|
||||
struct weighted_density_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<typename histogram_type::iterator> result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_density_impl(Args const &args)
|
||||
: cache_size(args[density_cache_size])
|
||||
, cache(cache_size)
|
||||
, num_bins(args[density_num_bins])
|
||||
, samples_in_bin(num_bins + 2, 0.)
|
||||
, bin_positions(num_bins + 2)
|
||||
, histogram(
|
||||
num_bins + 2
|
||||
, std::make_pair(
|
||||
numeric::fdiv(args[sample | Sample()],(std::size_t)1)
|
||||
, numeric::fdiv(args[sample | Sample()],(std::size_t)1)
|
||||
)
|
||||
)
|
||||
, is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// Fill up cache with cache_size first samples
|
||||
if (cnt <= this->cache_size)
|
||||
{
|
||||
this->cache[cnt - 1] = std::make_pair(args[sample], args[weight]);
|
||||
}
|
||||
|
||||
// Once cache_size samples have been accumulated, create num_bins bins of same size between
|
||||
// the minimum and maximum of the cached samples as well as an under- and an overflow bin.
|
||||
// Store their lower bounds (bin_positions) and fill the bins with the cached samples (samples_in_bin).
|
||||
if (cnt == this->cache_size)
|
||||
{
|
||||
float_type minimum = numeric::fdiv((min)(args),(std::size_t)1);
|
||||
float_type maximum = numeric::fdiv((max)(args),(std::size_t)1);
|
||||
float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins);
|
||||
|
||||
// determine bin positions (their lower bounds)
|
||||
for (std::size_t i = 0; i < this->num_bins + 2; ++i)
|
||||
{
|
||||
this->bin_positions[i] = minimum + (i - 1.) * bin_size;
|
||||
}
|
||||
|
||||
for (typename histogram_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
|
||||
{
|
||||
if (iter->first < this->bin_positions[1])
|
||||
{
|
||||
this->samples_in_bin[0] += iter->second;
|
||||
}
|
||||
else if (iter->first >= this->bin_positions[this->num_bins + 1])
|
||||
{
|
||||
this->samples_in_bin[this->num_bins + 1] += iter->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it = std::upper_bound(
|
||||
this->bin_positions.begin()
|
||||
, this->bin_positions.end()
|
||||
, iter->first
|
||||
);
|
||||
|
||||
std::size_t d = std::distance(this->bin_positions.begin(), it);
|
||||
this->samples_in_bin[d - 1] += iter->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add each subsequent sample to the correct bin
|
||||
else if (cnt > this->cache_size)
|
||||
{
|
||||
if (args[sample] < this->bin_positions[1])
|
||||
{
|
||||
this->samples_in_bin[0] += args[weight];
|
||||
}
|
||||
else if (args[sample] >= this->bin_positions[this->num_bins + 1])
|
||||
{
|
||||
this->samples_in_bin[this->num_bins + 1] += args[weight];
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it = std::upper_bound(
|
||||
this->bin_positions.begin()
|
||||
, this->bin_positions.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
std::size_t d = std::distance(this->bin_positions.begin(), it);
|
||||
this->samples_in_bin[d - 1] += args[weight];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
// creates a vector of std::pair where each pair i holds
|
||||
// the values bin_positions[i] (x-axis of histogram) and
|
||||
// samples_in_bin[i] / cnt (y-axis of histogram).
|
||||
|
||||
for (std::size_t i = 0; i < this->num_bins + 2; ++i)
|
||||
{
|
||||
this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], sum_of_weights(args)));
|
||||
}
|
||||
}
|
||||
|
||||
// returns a range of pairs
|
||||
return make_iterator_range(this->histogram);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO split to save/load and check on parameters provided in ctor
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & cache_size;
|
||||
ar & cache;
|
||||
ar & num_bins;
|
||||
ar & samples_in_bin;
|
||||
ar & bin_positions;
|
||||
ar & histogram;
|
||||
ar & is_dirty;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t cache_size; // number of cached samples
|
||||
histogram_type cache; // cache to store the first cache_size samples with their weights as std::pair
|
||||
std::size_t num_bins; // number of bins
|
||||
array_type samples_in_bin; // number of samples in each bin
|
||||
array_type bin_positions; // lower bounds of bins
|
||||
mutable histogram_type histogram; // histogram
|
||||
mutable bool is_dirty;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_density
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_density
|
||||
: depends_on<count, sum_of_weights, min, max>
|
||||
, density_cache_size
|
||||
, density_num_bins
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_density_impl<mpl::_1, mpl::_2> impl;
|
||||
|
||||
#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
||||
static boost::parameter::keyword<density_cache_size> const cache_size;
|
||||
static boost::parameter::keyword<density_num_bins> const num_bins;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_density
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::density> const weighted_density = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_density)
|
||||
}
|
||||
|
||||
using extract::weighted_density;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,302 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_extended_p_square.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_EXTENDED_P_SQUARE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_EXTENDED_P_SQUARE_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <boost/iterator/permutation_iterator.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/times2_iterator.hpp>
|
||||
#include <boost/accumulators/statistics/extended_p_square.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_extended_p_square_impl
|
||||
// multiple quantile estimation with weighted samples
|
||||
/**
|
||||
@brief Multiple quantile estimation with the extended \f$P^2\f$ algorithm for weighted samples
|
||||
|
||||
This version of the extended \f$P^2\f$ algorithm extends the extended \f$P^2\f$ algorithm to
|
||||
support weighted samples. The extended \f$P^2\f$ algorithm dynamically estimates several
|
||||
quantiles without storing samples. Assume that \f$m\f$ quantiles
|
||||
\f$\xi_{p_1}, \ldots, \xi_{p_m}\f$ are to be estimated. Instead of storing the whole sample
|
||||
cumulative distribution, the algorithm maintains only \f$m+2\f$ principal markers and
|
||||
\f$m+1\f$ middle markers, whose positions are updated with each sample and whose heights
|
||||
are adjusted (if necessary) using a piecewise-parablic formula. The heights of the principal
|
||||
markers are the current estimates of the quantiles and are returned as an iterator range.
|
||||
|
||||
For further details, see
|
||||
|
||||
K. E. E. Raatikainen, Simultaneous estimation of several quantiles, Simulation, Volume 49,
|
||||
Number 4 (October), 1986, p. 159-164.
|
||||
|
||||
The extended \f$ P^2 \f$ algorithm generalizes the \f$ P^2 \f$ algorithm of
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param extended_p_square_probabilities A vector of quantile probabilities.
|
||||
*/
|
||||
template<typename Sample, typename Weight>
|
||||
struct weighted_extended_p_square_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<
|
||||
detail::lvalue_index_iterator<
|
||||
permutation_iterator<
|
||||
typename array_type::const_iterator
|
||||
, detail::times2_iterator
|
||||
>
|
||||
>
|
||||
> result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_extended_p_square_impl(Args const &args)
|
||||
: probabilities(
|
||||
boost::begin(args[extended_p_square_probabilities])
|
||||
, boost::end(args[extended_p_square_probabilities])
|
||||
)
|
||||
, heights(2 * probabilities.size() + 3)
|
||||
, actual_positions(heights.size())
|
||||
, desired_positions(heights.size())
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
std::size_t sample_cell = 1; // k
|
||||
std::size_t num_quantiles = this->probabilities.size();
|
||||
|
||||
// m+2 principal markers and m+1 middle markers
|
||||
std::size_t num_markers = 2 * num_quantiles + 3;
|
||||
|
||||
// first accumulate num_markers samples
|
||||
if(cnt <= num_markers)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
this->actual_positions[cnt - 1] = args[weight];
|
||||
|
||||
// complete the initialization of heights (and actual_positions) by sorting
|
||||
if(cnt == num_markers)
|
||||
{
|
||||
// TODO: we need to sort the initial samples (in heights) in ascending order and
|
||||
// sort their weights (in actual_positions) the same way. The following lines do
|
||||
// it, but there must be a better and more efficient way of doing this.
|
||||
typename array_type::iterator it_begin, it_end, it_min;
|
||||
|
||||
it_begin = this->heights.begin();
|
||||
it_end = this->heights.end();
|
||||
|
||||
std::size_t pos = 0;
|
||||
|
||||
while (it_begin != it_end)
|
||||
{
|
||||
it_min = std::min_element(it_begin, it_end);
|
||||
std::size_t d = std::distance(it_begin, it_min);
|
||||
std::swap(*it_begin, *it_min);
|
||||
std::swap(this->actual_positions[pos], this->actual_positions[pos + d]);
|
||||
++it_begin;
|
||||
++pos;
|
||||
}
|
||||
|
||||
// calculate correct initial actual positions
|
||||
for (std::size_t i = 1; i < num_markers; ++i)
|
||||
{
|
||||
actual_positions[i] += actual_positions[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
this->actual_positions[0] = args[weight];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if(args[sample] >= this->heights[num_markers - 1])
|
||||
{
|
||||
this->heights[num_markers - 1] = args[sample];
|
||||
sample_cell = num_markers - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// find cell k = sample_cell such that heights[k-1] <= sample < heights[k]
|
||||
|
||||
typedef typename array_type::iterator iterator;
|
||||
iterator it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// update actual position of all markers above sample_cell
|
||||
for(std::size_t i = sample_cell; i < num_markers; ++i)
|
||||
{
|
||||
this->actual_positions[i] += args[weight];
|
||||
}
|
||||
|
||||
// compute desired positions
|
||||
{
|
||||
this->desired_positions[0] = this->actual_positions[0];
|
||||
this->desired_positions[num_markers - 1] = sum_of_weights(args);
|
||||
this->desired_positions[1] = (sum_of_weights(args) - this->actual_positions[0]) * probabilities[0]
|
||||
/ 2. + this->actual_positions[0];
|
||||
this->desired_positions[num_markers - 2] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* (probabilities[num_quantiles - 1] + 1.)
|
||||
/ 2. + this->actual_positions[0];
|
||||
|
||||
for (std::size_t i = 0; i < num_quantiles; ++i)
|
||||
{
|
||||
this->desired_positions[2 * i + 2] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* probabilities[i] + this->actual_positions[0];
|
||||
}
|
||||
|
||||
for (std::size_t i = 1; i < num_quantiles; ++i)
|
||||
{
|
||||
this->desired_positions[2 * i + 1] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* (probabilities[i - 1] + probabilities[i])
|
||||
/ 2. + this->actual_positions[0];
|
||||
}
|
||||
}
|
||||
|
||||
// adjust heights and actual_positions of markers 1 to num_markers - 2 if necessary
|
||||
for (std::size_t i = 1; i <= num_markers - 2; ++i)
|
||||
{
|
||||
// offset to desired position
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
||||
|
||||
if((d >= 1 && dp > 1) || (d <= -1 && dm < -1))
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm)*hp + (dp - sign_d) * hm);
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
if(this->heights[i - 1] < h && h < this->heights[i + 1])
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if(d > 0)
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if(d < 0)
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
// for i in [1,probabilities.size()], return heights[i * 2]
|
||||
detail::times2_iterator idx_begin = detail::make_times2_iterator(1);
|
||||
detail::times2_iterator idx_end = detail::make_times2_iterator(this->probabilities.size() + 1);
|
||||
|
||||
return result_type(
|
||||
make_permutation_iterator(this->heights.begin(), idx_begin)
|
||||
, make_permutation_iterator(this->heights.begin(), idx_end)
|
||||
);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO: do we need to split to load/save and verify that the parameters did not change?
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & probabilities;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
}
|
||||
|
||||
private:
|
||||
array_type probabilities; // the quantile probabilities
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // d_i
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_extended_p_square
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_extended_p_square
|
||||
: depends_on<count, sum_of_weights>
|
||||
, extended_p_square_probabilities
|
||||
{
|
||||
typedef accumulators::impl::weighted_extended_p_square_impl<mpl::_1, mpl::_2> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_extended_p_square
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::weighted_extended_p_square> const weighted_extended_p_square = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square)
|
||||
}
|
||||
|
||||
using extract::weighted_extended_p_square;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_kurtosis.hpp
|
||||
//
|
||||
// Copyright 2006 Olivier Gygi, Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_KURTOSIS_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_KURTOSIS_HPP_EAN_28_10_2005
|
||||
|
||||
#include <limits>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_moment.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_mean.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_kurtosis_impl
|
||||
/**
|
||||
@brief Kurtosis estimation for weighted samples
|
||||
|
||||
The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central
|
||||
moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution
|
||||
has zero kurtosis. The kurtosis can also be expressed by the simple moments:
|
||||
|
||||
\f[
|
||||
\hat{g}_2 =
|
||||
\frac
|
||||
{\widehat{m}_n^{(4)}-4\widehat{m}_n^{(3)}\hat{\mu}_n+6\widehat{m}_n^{(2)}\hat{\mu}_n^2-3\hat{\mu}_n^4}
|
||||
{\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3,
|
||||
\f]
|
||||
|
||||
where \f$ \widehat{m}_n^{(i)} \f$ are the \f$ i \f$-th moment and \f$ \hat{\mu}_n \f$ the mean (first moment) of the
|
||||
\f$ n \f$ samples.
|
||||
|
||||
The kurtosis estimator for weighted samples is formally identical to the estimator for unweighted samples, except that
|
||||
the weighted counterparts of all measures it depends on are to be taken.
|
||||
*/
|
||||
template<typename Sample, typename Weight>
|
||||
struct weighted_kurtosis_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type;
|
||||
|
||||
weighted_kurtosis_impl(dont_care)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(
|
||||
accumulators::weighted_moment<4>(args)
|
||||
- 4. * accumulators::weighted_moment<3>(args) * weighted_mean(args)
|
||||
+ 6. * accumulators::weighted_moment<2>(args) * weighted_mean(args) * weighted_mean(args)
|
||||
- 3. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args) * weighted_mean(args)
|
||||
, ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
|
||||
* ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
|
||||
) - 3.;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_kurtosis
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_kurtosis
|
||||
: depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3>, weighted_moment<4> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_kurtosis_impl<mpl::_1, mpl::_2> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_kurtosis
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::weighted_kurtosis> const weighted_kurtosis = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_kurtosis)
|
||||
}
|
||||
|
||||
using extract::weighted_kurtosis;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,196 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_mean.hpp
|
||||
//
|
||||
// Copyright 2006 Eric Niebler, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEAN_HPP_EAN_03_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEAN_HPP_EAN_03_11_2005
|
||||
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/eval_if.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/weights.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/mean.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_sum.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_mean_impl
|
||||
// lazy, by default
|
||||
template<typename Sample, typename Weight, typename Tag>
|
||||
struct weighted_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
|
||||
|
||||
weighted_mean_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
typedef
|
||||
typename mpl::if_<
|
||||
is_same<Tag, tag::sample>
|
||||
, tag::weighted_sum
|
||||
, tag::weighted_sum_of_variates<Sample, Tag>
|
||||
>::type
|
||||
weighted_sum_tag;
|
||||
|
||||
extractor<weighted_sum_tag> const some_weighted_sum = {};
|
||||
|
||||
return numeric::fdiv(some_weighted_sum(args), sum_of_weights(args));
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// immediate_weighted_mean_impl
|
||||
// immediate
|
||||
template<typename Sample, typename Weight, typename Tag>
|
||||
struct immediate_weighted_mean_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
immediate_weighted_mean_impl(Args const &args)
|
||||
: mean(
|
||||
numeric::fdiv(
|
||||
args[parameter::keyword<Tag>::get() | Sample()]
|
||||
* numeric::one<Weight>::value
|
||||
, numeric::one<Weight>::value
|
||||
)
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
// Matthias:
|
||||
// need to pass the argument pack since the weight might be an external
|
||||
// accumulator set passed as a named parameter
|
||||
Weight w_sum = sum_of_weights(args);
|
||||
Weight w = args[weight];
|
||||
weighted_sample const &s = args[parameter::keyword<Tag>::get()] * w;
|
||||
this->mean = numeric::fdiv(this->mean * (w_sum - w) + s, w_sum);
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->mean;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & mean;
|
||||
}
|
||||
|
||||
private:
|
||||
result_type mean;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_mean
|
||||
// tag::immediate_weighted_mean
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_mean
|
||||
: depends_on<sum_of_weights, weighted_sum>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
|
||||
};
|
||||
struct immediate_weighted_mean
|
||||
: depends_on<sum_of_weights>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_weighted_mean_impl<mpl::_1, mpl::_2, tag::sample> impl;
|
||||
};
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct weighted_mean_of_variates
|
||||
: depends_on<sum_of_weights, weighted_sum_of_variates<VariateType, VariateTag> >
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
|
||||
};
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct immediate_weighted_mean_of_variates
|
||||
: depends_on<sum_of_weights>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::immediate_weighted_mean_impl<VariateType, mpl::_2, VariateTag> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_mean
|
||||
// extract::weighted_mean_of_variates
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::mean> const weighted_mean = {};
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_mean_of_variates, (typename)(typename))
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_mean)
|
||||
}
|
||||
|
||||
using extract::weighted_mean;
|
||||
using extract::weighted_mean_of_variates;
|
||||
|
||||
// weighted_mean(lazy) -> weighted_mean
|
||||
template<>
|
||||
struct as_feature<tag::weighted_mean(lazy)>
|
||||
{
|
||||
typedef tag::weighted_mean type;
|
||||
};
|
||||
|
||||
// weighted_mean(immediate) -> immediate_weighted_mean
|
||||
template<>
|
||||
struct as_feature<tag::weighted_mean(immediate)>
|
||||
{
|
||||
typedef tag::immediate_weighted_mean type;
|
||||
};
|
||||
|
||||
// weighted_mean_of_variates<VariateType, VariateTag>(lazy) -> weighted_mean_of_variates<VariateType, VariateTag>
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::weighted_mean_of_variates<VariateType, VariateTag>(lazy)>
|
||||
{
|
||||
typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
// weighted_mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_weighted_mean_of_variates<VariateType, VariateTag>
|
||||
template<typename VariateType, typename VariateTag>
|
||||
struct as_feature<tag::weighted_mean_of_variates<VariateType, VariateTag>(immediate)>
|
||||
{
|
||||
typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,255 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_median.hpp
|
||||
//
|
||||
// Copyright 2006 Eric Niebler, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEDIAN_HPP_EAN_28_10_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MEDIAN_HPP_EAN_28_10_2005
|
||||
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/median.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_p_square_quantile.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_density.hpp>
|
||||
#include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation for weighted samples based on the \f$P^2\f$ quantile estimator
|
||||
|
||||
The \f$P^2\f$ algorithm for weighted samples is invoked with a quantile probability of 0.5.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct weighted_median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
|
||||
|
||||
weighted_median_impl(dont_care) {}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return weighted_p_square_quantile_for_median(args);
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// with_density_weighted_median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation for weighted samples based on the density estimator
|
||||
|
||||
The algorithm determines the bin in which the \f$0.5*cnt\f$-th sample lies, \f$cnt\f$ being
|
||||
the total number of samples. It returns the approximate horizontal position of this sample,
|
||||
based on a linear interpolation inside the bin.
|
||||
*/
|
||||
template<typename Sample>
|
||||
struct with_density_weighted_median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef iterator_range<typename histogram_type::iterator> range_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
with_density_weighted_median_impl(Args const &args)
|
||||
: sum(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
|
||||
, is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
range_type histogram = weighted_density(args);
|
||||
typename range_type::iterator it = histogram.begin();
|
||||
while (this->sum < 0.5 * cnt)
|
||||
{
|
||||
this->sum += it->second * cnt;
|
||||
++it;
|
||||
}
|
||||
--it;
|
||||
float_type over = numeric::fdiv(this->sum - 0.5 * cnt, it->second * cnt);
|
||||
this->median = it->first * over + (it + 1)->first * ( 1. - over );
|
||||
}
|
||||
|
||||
return this->median;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
ar & is_dirty;
|
||||
ar & median;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable float_type sum;
|
||||
mutable bool is_dirty;
|
||||
mutable float_type median;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// with_p_square_cumulative_distribution_weighted_median_impl
|
||||
//
|
||||
/**
|
||||
@brief Median estimation for weighted samples based on the \f$P^2\f$ cumulative distribution estimator
|
||||
|
||||
The algorithm determines the first (leftmost) bin with a height exceeding 0.5. It
|
||||
returns the approximate horizontal position of where the cumulative distribution
|
||||
equals 0.5, based on a linear interpolation inside the bin.
|
||||
*/
|
||||
template<typename Sample, typename Weight>
|
||||
struct with_p_square_cumulative_distribution_weighted_median_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef iterator_range<typename histogram_type::iterator> range_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
with_p_square_cumulative_distribution_weighted_median_impl(dont_care)
|
||||
: is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
void operator ()(dont_care)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
range_type histogram = weighted_p_square_cumulative_distribution(args);
|
||||
typename range_type::iterator it = histogram.begin();
|
||||
while (it->second < 0.5)
|
||||
{
|
||||
++it;
|
||||
}
|
||||
float_type over = numeric::fdiv(it->second - 0.5, it->second - (it - 1)->second);
|
||||
this->median = it->first * over + (it + 1)->first * ( 1. - over );
|
||||
}
|
||||
|
||||
return this->median;
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & is_dirty;
|
||||
ar & median;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable bool is_dirty;
|
||||
mutable float_type median;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_median
|
||||
// tag::with_density_weighted_median
|
||||
// tag::with_p_square_cumulative_distribution_weighted_median
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_median
|
||||
: depends_on<weighted_p_square_quantile_for_median>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_median_impl<mpl::_1> impl;
|
||||
};
|
||||
struct with_density_weighted_median
|
||||
: depends_on<count, weighted_density>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::with_density_weighted_median_impl<mpl::_1> impl;
|
||||
};
|
||||
struct with_p_square_cumulative_distribution_weighted_median
|
||||
: depends_on<weighted_p_square_cumulative_distribution>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::with_p_square_cumulative_distribution_weighted_median_impl<mpl::_1, mpl::_2> impl;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_median
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::median> const weighted_median = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_median)
|
||||
}
|
||||
|
||||
using extract::weighted_median;
|
||||
// weighted_median(with_p_square_quantile) -> weighted_median
|
||||
template<>
|
||||
struct as_feature<tag::weighted_median(with_p_square_quantile)>
|
||||
{
|
||||
typedef tag::weighted_median type;
|
||||
};
|
||||
|
||||
// weighted_median(with_density) -> with_density_weighted_median
|
||||
template<>
|
||||
struct as_feature<tag::weighted_median(with_density)>
|
||||
{
|
||||
typedef tag::with_density_weighted_median type;
|
||||
};
|
||||
|
||||
// weighted_median(with_p_square_cumulative_distribution) -> with_p_square_cumulative_distribution_weighted_median
|
||||
template<>
|
||||
struct as_feature<tag::weighted_median(with_p_square_cumulative_distribution)>
|
||||
{
|
||||
typedef tag::with_p_square_cumulative_distribution_weighted_median type;
|
||||
};
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_moment.hpp
|
||||
//
|
||||
// Copyright 2006, Eric Niebler, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
|
||||
|
||||
#include <boost/config/no_tr1/cmath.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/preprocessor/arithmetic/inc.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/framework/depends_on.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/moment.hpp> // for pow()
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_moment_impl
|
||||
template<typename N, typename Sample, typename Weight>
|
||||
struct weighted_moment_impl
|
||||
: accumulator_base // TODO: also depends_on sum of powers
|
||||
{
|
||||
BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
// for boost::result_of
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_moment_impl(Args const &args)
|
||||
: sum(args[sample | Sample()] * numeric::one<Weight>::value)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->sum += args[weight] * numeric::pow(args[sample], N());
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
return numeric::fdiv(this->sum, sum_of_weights(args));
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & sum;
|
||||
}
|
||||
|
||||
private:
|
||||
weighted_sample sum;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_moment
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
template<int N>
|
||||
struct weighted_moment
|
||||
: depends_on<count, sum_of_weights>
|
||||
{
|
||||
/// INTERNAL ONLY
|
||||
///
|
||||
typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_moment
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int))
|
||||
}
|
||||
|
||||
using extract::weighted_moment;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,275 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_p_square_cumul_dist.hpp
|
||||
//
|
||||
// Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/p_square_cumul_dist.hpp> // for named parameter p_square_cumulative_distribution_num_cells
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_p_square_cumulative_distribution_impl
|
||||
// cumulative distribution calculation (as histogram)
|
||||
/**
|
||||
@brief Histogram calculation of the cumulative distribution with the \f$P^2\f$ algorithm for weighted samples
|
||||
|
||||
A histogram of the sample cumulative distribution is computed dynamically without storing samples
|
||||
based on the \f$ P^2 \f$ algorithm for weighted samples. The returned histogram has a specifiable
|
||||
amount (num_cells) equiprobable (and not equal-sized) cells.
|
||||
|
||||
Note that applying importance sampling results in regions to be more and other regions to be less
|
||||
accurately estimated than without importance sampling, i.e., with unweighted samples.
|
||||
|
||||
For further details, see
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param p_square_cumulative_distribution_num_cells
|
||||
*/
|
||||
template<typename Sample, typename Weight>
|
||||
struct weighted_p_square_cumulative_distribution_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type;
|
||||
typedef std::vector<std::pair<float_type, float_type> > histogram_type;
|
||||
typedef std::vector<float_type> array_type;
|
||||
// for boost::result_of
|
||||
typedef iterator_range<typename histogram_type::iterator> result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_p_square_cumulative_distribution_impl(Args const &args)
|
||||
: num_cells(args[p_square_cumulative_distribution_num_cells])
|
||||
, heights(num_cells + 1)
|
||||
, actual_positions(num_cells + 1)
|
||||
, desired_positions(num_cells + 1)
|
||||
, histogram(num_cells + 1)
|
||||
, is_dirty(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
this->is_dirty = true;
|
||||
|
||||
std::size_t cnt = count(args);
|
||||
std::size_t sample_cell = 1; // k
|
||||
std::size_t b = this->num_cells;
|
||||
|
||||
// accumulate num_cells + 1 first samples
|
||||
if (cnt <= b + 1)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
this->actual_positions[cnt - 1] = args[weight];
|
||||
|
||||
// complete the initialization of heights by sorting
|
||||
if (cnt == b + 1)
|
||||
{
|
||||
//std::sort(this->heights.begin(), this->heights.end());
|
||||
|
||||
// TODO: we need to sort the initial samples (in heights) in ascending order and
|
||||
// sort their weights (in actual_positions) the same way. The following lines do
|
||||
// it, but there must be a better and more efficient way of doing this.
|
||||
typename array_type::iterator it_begin, it_end, it_min;
|
||||
|
||||
it_begin = this->heights.begin();
|
||||
it_end = this->heights.end();
|
||||
|
||||
std::size_t pos = 0;
|
||||
|
||||
while (it_begin != it_end)
|
||||
{
|
||||
it_min = std::min_element(it_begin, it_end);
|
||||
std::size_t d = std::distance(it_begin, it_min);
|
||||
std::swap(*it_begin, *it_min);
|
||||
std::swap(this->actual_positions[pos], this->actual_positions[pos + d]);
|
||||
++it_begin;
|
||||
++pos;
|
||||
}
|
||||
|
||||
// calculate correct initial actual positions
|
||||
for (std::size_t i = 1; i < b; ++i)
|
||||
{
|
||||
this->actual_positions[i] += this->actual_positions[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
|
||||
if (args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
this->actual_positions[0] = args[weight];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if (this->heights[b] <= args[sample])
|
||||
{
|
||||
this->heights[b] = args[sample];
|
||||
sample_cell = b;
|
||||
}
|
||||
else
|
||||
{
|
||||
typename array_type::iterator it;
|
||||
it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// increment positions of markers above sample_cell
|
||||
for (std::size_t i = sample_cell; i < b + 1; ++i)
|
||||
{
|
||||
this->actual_positions[i] += args[weight];
|
||||
}
|
||||
|
||||
// determine desired marker positions
|
||||
for (std::size_t i = 1; i < b + 1; ++i)
|
||||
{
|
||||
this->desired_positions[i] = this->actual_positions[0]
|
||||
+ numeric::fdiv((i-1) * (sum_of_weights(args) - this->actual_positions[0]), b);
|
||||
}
|
||||
|
||||
// adjust heights of markers 2 to num_cells if necessary
|
||||
for (std::size_t i = 1; i < b; ++i)
|
||||
{
|
||||
// offset to desire position
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
||||
|
||||
if ( ( d >= 1. && dp > 1. ) || ( d <= -1. && dm < -1. ) )
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ( (sign_d - dm) * hp + (dp - sign_d) * hm );
|
||||
|
||||
if ( this->heights[i - 1] < h && h < this->heights[i + 1] )
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if (d>0)
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if (d<0)
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
result_type result(Args const &args) const
|
||||
{
|
||||
if (this->is_dirty)
|
||||
{
|
||||
this->is_dirty = false;
|
||||
|
||||
// creates a vector of std::pair where each pair i holds
|
||||
// the values heights[i] (x-axis of histogram) and
|
||||
// actual_positions[i] / sum_of_weights (y-axis of histogram)
|
||||
|
||||
for (std::size_t i = 0; i < this->histogram.size(); ++i)
|
||||
{
|
||||
this->histogram[i] = std::make_pair(this->heights[i], numeric::fdiv(this->actual_positions[i], sum_of_weights(args)));
|
||||
}
|
||||
}
|
||||
|
||||
return make_iterator_range(this->histogram);
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO split to save/load and check on parameters provided in ctor
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & num_cells;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
ar & histogram;
|
||||
ar & is_dirty;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t num_cells; // number of cells b
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // n'_i
|
||||
mutable histogram_type histogram; // histogram
|
||||
mutable bool is_dirty;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_p_square_cumulative_distribution
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_p_square_cumulative_distribution
|
||||
: depends_on<count, sum_of_weights>
|
||||
, p_square_cumulative_distribution_num_cells
|
||||
{
|
||||
typedef accumulators::impl::weighted_p_square_cumulative_distribution_impl<mpl::_1, mpl::_2> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_p_square_cumulative_distribution
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::weighted_p_square_cumulative_distribution> const weighted_p_square_cumulative_distribution = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_p_square_cumulative_distribution)
|
||||
}
|
||||
|
||||
using extract::weighted_p_square_cumulative_distribution;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_p_square_cumulative_distribution.hpp
|
||||
//
|
||||
// Copyright 2012 Eric Niebler. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_CUMULATIVE_DISTRIBUTION_HPP_03_19_2012
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_CUMULATIVE_DISTRIBUTION_HPP_03_19_2012
|
||||
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__)
|
||||
# pragma message ("Warning: This header is deprecated. Please use: boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp")
|
||||
#elif defined(__GNUC__) || defined(__HP_aCC) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
|
||||
# warning "This header is deprecated. Please use: boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp"
|
||||
#endif
|
||||
|
||||
#include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,266 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_p_square_quantile.hpp
|
||||
//
|
||||
// Copyright 2005 Daniel Egloff. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
||||
#define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
||||
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
#include <boost/array.hpp>
|
||||
#include <boost/parameter/keyword.hpp>
|
||||
#include <boost/mpl/placeholders.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/accumulators/framework/accumulator_base.hpp>
|
||||
#include <boost/accumulators/framework/extractor.hpp>
|
||||
#include <boost/accumulators/numeric/functional.hpp>
|
||||
#include <boost/accumulators/framework/parameters/sample.hpp>
|
||||
#include <boost/accumulators/statistics_fwd.hpp>
|
||||
#include <boost/accumulators/statistics/count.hpp>
|
||||
#include <boost/accumulators/statistics/sum.hpp>
|
||||
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
||||
|
||||
namespace boost { namespace accumulators
|
||||
{
|
||||
|
||||
namespace impl {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// weighted_p_square_quantile_impl
|
||||
// single quantile estimation with weighted samples
|
||||
/**
|
||||
@brief Single quantile estimation with the \f$P^2\f$ algorithm for weighted samples
|
||||
|
||||
This version of the \f$P^2\f$ algorithm extends the \f$P^2\f$ algorithm to support weighted samples.
|
||||
The \f$P^2\f$ algorithm estimates a quantile dynamically without storing samples. Instead of
|
||||
storing the whole sample cumulative distribution, only five points (markers) are stored. The heights
|
||||
of these markers are the minimum and the maximum of the samples and the current estimates of the
|
||||
\f$(p/2)\f$-, \f$p\f$ - and \f$(1+p)/2\f$ -quantiles. Their positions are equal to the number
|
||||
of samples that are smaller or equal to the markers. Each time a new sample is added, the
|
||||
positions of the markers are updated and if necessary their heights are adjusted using a piecewise-
|
||||
parabolic formula.
|
||||
|
||||
For further details, see
|
||||
|
||||
R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
||||
histograms without storing observations, Communications of the ACM,
|
||||
Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
||||
|
||||
@param quantile_probability
|
||||
*/
|
||||
template<typename Sample, typename Weight, typename Impl>
|
||||
struct weighted_p_square_quantile_impl
|
||||
: accumulator_base
|
||||
{
|
||||
typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
|
||||
typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type;
|
||||
typedef array<float_type, 5> array_type;
|
||||
// for boost::result_of
|
||||
typedef float_type result_type;
|
||||
|
||||
template<typename Args>
|
||||
weighted_p_square_quantile_impl(Args const &args)
|
||||
: p(is_same<Impl, for_median>::value ? 0.5 : args[quantile_probability | 0.5])
|
||||
, heights()
|
||||
, actual_positions()
|
||||
, desired_positions()
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Args>
|
||||
void operator ()(Args const &args)
|
||||
{
|
||||
std::size_t cnt = count(args);
|
||||
|
||||
// accumulate 5 first samples
|
||||
if (cnt <= 5)
|
||||
{
|
||||
this->heights[cnt - 1] = args[sample];
|
||||
|
||||
// In this initialization phase, actual_positions stores the weights of the
|
||||
// initial samples that are needed at the end of the initialization phase to
|
||||
// compute the correct initial positions of the markers.
|
||||
this->actual_positions[cnt - 1] = args[weight];
|
||||
|
||||
// complete the initialization of heights and actual_positions by sorting
|
||||
if (cnt == 5)
|
||||
{
|
||||
// TODO: we need to sort the initial samples (in heights) in ascending order and
|
||||
// sort their weights (in actual_positions) the same way. The following lines do
|
||||
// it, but there must be a better and more efficient way of doing this.
|
||||
typename array_type::iterator it_begin, it_end, it_min;
|
||||
|
||||
it_begin = this->heights.begin();
|
||||
it_end = this->heights.end();
|
||||
|
||||
std::size_t pos = 0;
|
||||
|
||||
while (it_begin != it_end)
|
||||
{
|
||||
it_min = std::min_element(it_begin, it_end);
|
||||
std::size_t d = std::distance(it_begin, it_min);
|
||||
std::swap(*it_begin, *it_min);
|
||||
std::swap(this->actual_positions[pos], this->actual_positions[pos + d]);
|
||||
++it_begin;
|
||||
++pos;
|
||||
}
|
||||
|
||||
// calculate correct initial actual positions
|
||||
for (std::size_t i = 1; i < 5; ++i)
|
||||
{
|
||||
this->actual_positions[i] += this->actual_positions[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::size_t sample_cell = 1; // k
|
||||
|
||||
// find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
|
||||
if (args[sample] < this->heights[0])
|
||||
{
|
||||
this->heights[0] = args[sample];
|
||||
this->actual_positions[0] = args[weight];
|
||||
sample_cell = 1;
|
||||
}
|
||||
else if (this->heights[4] <= args[sample])
|
||||
{
|
||||
this->heights[4] = args[sample];
|
||||
sample_cell = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef typename array_type::iterator iterator;
|
||||
iterator it = std::upper_bound(
|
||||
this->heights.begin()
|
||||
, this->heights.end()
|
||||
, args[sample]
|
||||
);
|
||||
|
||||
sample_cell = std::distance(this->heights.begin(), it);
|
||||
}
|
||||
|
||||
// increment positions of markers above sample_cell
|
||||
for (std::size_t i = sample_cell; i < 5; ++i)
|
||||
{
|
||||
this->actual_positions[i] += args[weight];
|
||||
}
|
||||
|
||||
// update desired positions for all markers
|
||||
this->desired_positions[0] = this->actual_positions[0];
|
||||
this->desired_positions[1] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* this->p/2. + this->actual_positions[0];
|
||||
this->desired_positions[2] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* this->p + this->actual_positions[0];
|
||||
this->desired_positions[3] = (sum_of_weights(args) - this->actual_positions[0])
|
||||
* (1. + this->p)/2. + this->actual_positions[0];
|
||||
this->desired_positions[4] = sum_of_weights(args);
|
||||
|
||||
// adjust height and actual positions of markers 1 to 3 if necessary
|
||||
for (std::size_t i = 1; i <= 3; ++i)
|
||||
{
|
||||
// offset to desired positions
|
||||
float_type d = this->desired_positions[i] - this->actual_positions[i];
|
||||
|
||||
// offset to next position
|
||||
float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
||||
|
||||
// offset to previous position
|
||||
float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
||||
|
||||
// height ds
|
||||
float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
||||
float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
||||
|
||||
if ( ( d >= 1. && dp > 1. ) || ( d <= -1. && dm < -1. ) )
|
||||
{
|
||||
short sign_d = static_cast<short>(d / std::abs(d));
|
||||
|
||||
// try adjusting heights[i] using p-squared formula
|
||||
float_type h = this->heights[i] + sign_d / (dp - dm) * ( (sign_d - dm) * hp + (dp - sign_d) * hm );
|
||||
|
||||
if ( this->heights[i - 1] < h && h < this->heights[i + 1] )
|
||||
{
|
||||
this->heights[i] = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use linear formula
|
||||
if (d>0)
|
||||
{
|
||||
this->heights[i] += hp;
|
||||
}
|
||||
if (d<0)
|
||||
{
|
||||
this->heights[i] -= hm;
|
||||
}
|
||||
}
|
||||
this->actual_positions[i] += sign_d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result_type result(dont_care) const
|
||||
{
|
||||
return this->heights[2];
|
||||
}
|
||||
|
||||
// make this accumulator serializeable
|
||||
// TODO split to save/load and check on parameters provided in ctor
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int file_version)
|
||||
{
|
||||
ar & p;
|
||||
ar & heights;
|
||||
ar & actual_positions;
|
||||
ar & desired_positions;
|
||||
}
|
||||
|
||||
private:
|
||||
float_type p; // the quantile probability p
|
||||
array_type heights; // q_i
|
||||
array_type actual_positions; // n_i
|
||||
array_type desired_positions; // n'_i
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tag::weighted_p_square_quantile
|
||||
//
|
||||
namespace tag
|
||||
{
|
||||
struct weighted_p_square_quantile
|
||||
: depends_on<count, sum_of_weights>
|
||||
{
|
||||
typedef accumulators::impl::weighted_p_square_quantile_impl<mpl::_1, mpl::_2, regular> impl;
|
||||
};
|
||||
struct weighted_p_square_quantile_for_median
|
||||
: depends_on<count, sum_of_weights>
|
||||
{
|
||||
typedef accumulators::impl::weighted_p_square_quantile_impl<mpl::_1, mpl::_2, for_median> impl;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// extract::weighted_p_square_quantile
|
||||
// extract::weighted_p_square_quantile_for_median
|
||||
//
|
||||
namespace extract
|
||||
{
|
||||
extractor<tag::weighted_p_square_quantile> const weighted_p_square_quantile = {};
|
||||
extractor<tag::weighted_p_square_quantile_for_median> const weighted_p_square_quantile_for_median = {};
|
||||
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_p_square_quantile)
|
||||
BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_p_square_quantile_for_median)
|
||||
}
|
||||
|
||||
using extract::weighted_p_square_quantile;
|
||||
using extract::weighted_p_square_quantile_for_median;
|
||||
|
||||
}} // namespace boost::accumulators
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user