当前位置: 首页 > news >正文

保山市建设厅官方网站广州aso优化

保山市建设厅官方网站,广州aso优化,无锡网站推广公司排名,网站建设主页用arrow c版本读取了csv中的基金净值数据,然后计算了夏普率,比较尴尬的是,arrow c版本计算耗费的时间却比python的empyrical版本耗费时间多。。。 arrow新手上路,第一次自己去实现功能,实现的大概率并不是最高效的方…

用arrow c++版本读取了csv中的基金净值数据,然后计算了夏普率,比较尴尬的是,arrow c++版本计算耗费的时间却比python的empyrical版本耗费时间多。。。
在这里插入图片描述
在这里插入图片描述

arrow新手上路,第一次自己去实现功能,实现的大概率并不是最高效的方式,但是我也踏出了用arrow c++改写backtrader的第一步。

  • 用arrow改写empyrical,就当练手了,目标是做成两个文件:empyrical.h和empyrical.cpp
  • 用arrow和qt改写pyfolio, 实现更美观的界面,做成两个文件:pyfolio.h 和pyfolio.cpp
  • 改写backtrader

c++版本的文件:

my_example.cc

#include <arrow/api.h>
#include <arrow/io/api.h>
#include "arrow/csv/api.h"
#include <arrow/compute/api.h>
#include <iostream>
#include <chrono>
//#include "../empyrical/empyrical.h"arrow::Status RunMain(){auto start_time = std::chrono::high_resolution_clock::now();// 首先,我们需要设置一个可读文件对象,它允许我们将读取器指向磁盘上的正确数据。我们将重复使用这个对象,并将其重新绑定到多个文件中。std::shared_ptr<arrow::io::ReadableFile> infile;// 绑定输入文件到 "test_in.csv"ARROW_ASSIGN_OR_RAISE(infile, arrow::io::ReadableFile::Open("/home/yun/Documents/fund_nav.csv"));// (文档部分:CSV 表格声明)std::shared_ptr<arrow::Table> csv_table;// CSV 读取器有多个对象,用于不同选项。现在,我们将使用默认值。ARROW_ASSIGN_OR_RAISE(auto csv_reader,arrow::csv::TableReader::Make(arrow::io::default_io_context(), infile, arrow::csv::ReadOptions::Defaults(),arrow::csv::ParseOptions::Defaults(), arrow::csv::ConvertOptions::Defaults()));// 读取表格。ARROW_ASSIGN_OR_RAISE(csv_table, csv_reader->Read());// 输出显示Table的元数据信息// std::cout << "Table Metadata:" << std::endl;// std::cout << "Number of columns: " << csv_table->num_columns() << std::endl;// std::cout << "Number of rows: " << csv_table->num_rows() << std::endl;// std::cout << "Schema: " << csv_table->schema()->ToString() << std::endl;// 输出显示Table的数据// for (int i = 0; i < csv_table->num_columns(); ++i) {//   std::shared_ptr<arrow::Array> column = csv_table->column(i);//   std::cout << "Column " << i << ": " << column->ToString() << std::endl;// }// 1. 显示table信息到std::cout的方法// std::shared_ptr<arrow::RecordBatch> record_batch;// arrow::Result<std::shared_ptr<arrow::RecordBatch>> result = csv_table->CombineChunksToBatch(); // 执行某个操作,返回Result// if (result.ok()) {//   record_batch = result.ValueOrDie();//   // 在这里使用 record_batch// } else {//   // 处理错误//   std::cerr << "Error: " << result.status().ToString() << std::endl;// }// //arrow::PrettyPrint(*record_batch, 2, &std::cout);// arrow::Status status = arrow::PrettyPrint(*record_batch, 2, &std::cout);// if (!status.ok()) {//   // 处理错误,例如打印错误信息//   std::cerr << "Error: " << status.ToString() << std::endl;// }// 2. 显示table信息到std::cout的方法// std::cout << csv_table->ToString() << std::endl;// 3. 显示table信息到std::cout的方法// arrow::Status status = arrow::PrettyPrint(*csv_table, 2, &std::cout);// if (!status.ok()) {//   // 处理错误,例如打印错误信息//   std::cerr << "Error: " << status.ToString() << std::endl;// }// 开始计算夏普率// std::cout << "一年的交易日有" << AnnualizationFactors::DAILY << "天" << std::endl;// std::cout << DAILY << std::endl;// 计算收益率arrow::Datum fund_returns;arrow::Datum fund_diff;std::shared_ptr<arrow::ChunkedArray> cum_nav = csv_table->GetColumnByName("复权净值");std::shared_ptr<arrow::ChunkedArray> now_cum_nav = cum_nav->Slice(1,cum_nav->length()-1);std::shared_ptr<arrow::ChunkedArray> pre_cum_nav = cum_nav->Slice(0,cum_nav->length()-1);ARROW_ASSIGN_OR_RAISE(fund_diff, arrow::compute::CallFunction("subtract", {now_cum_nav,pre_cum_nav}));ARROW_ASSIGN_OR_RAISE(fund_returns, arrow::compute::CallFunction("divide", {fund_diff,pre_cum_nav}));// // 获取结果数组// std::cout << "Datum kind: " << fund_returns.ToString()//           << " content type: " << fund_returns.type()->ToString() << std::endl;// // std::cout << fund_returns.scalar_as<arrow::DoubleScalar>().value << std::endl;// std::cout << fund_returns.chunked_array()->ToString() << std::endl;// 计算夏普率arrow::Datum avg_return;arrow::Datum avg_std;arrow::Datum daily_sharpe_ratio;arrow::Datum sharpe_ratio;arrow::Datum sqrt_year;// 创建 Arrow Double 标量double days_of_year_double = 252.0;std::shared_ptr<arrow::Scalar> days_of_year = arrow::MakeScalar(days_of_year_double);ARROW_ASSIGN_OR_RAISE(sqrt_year, arrow::compute::CallFunction("sqrt", {days_of_year}));ARROW_ASSIGN_OR_RAISE(avg_return, arrow::compute::CallFunction("mean", {fund_returns}));arrow::compute::VarianceOptions variance_options;variance_options.ddof = 1;ARROW_ASSIGN_OR_RAISE(avg_std, arrow::compute::CallFunction("stddev", {fund_returns},&variance_options));ARROW_ASSIGN_OR_RAISE(daily_sharpe_ratio, arrow::compute::CallFunction("divide", {avg_return,avg_std}));ARROW_ASSIGN_OR_RAISE(sharpe_ratio, arrow::compute::CallFunction("multiply", {daily_sharpe_ratio,sqrt_year}));std::cout << "计算得到的夏普率为 : " << sharpe_ratio.scalar_as<arrow::DoubleScalar>().value << std::endl;auto end_time = std::chrono::high_resolution_clock::now();auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);std::cout << "c++读取数据,然后计算夏普率一共耗费时间为: " << duration.count()/1000.0 << " ms" << std::endl;return arrow::Status::OK();}// (文档部分: 主函数)
int main() {arrow::Status st = RunMain();if (!st.ok()) {std::cerr << st << std::endl;return 1;}return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)project(MyExample)find_package(Arrow REQUIRED)
find_package(Parquet REQUIRED)
find_package(ArrowDataset REQUIRED)add_executable(my_example my_example.cc)
target_link_libraries(my_example PRIVATE Arrow::arrow_shared Parquet::parquet_shared ArrowDataset::arrow_dataset_shared)

在同一个文件夹下,运行

cmake -B build
cmake --build build
./build/my_example

python 运行代码如下:

import pandas as pd 
import empyrical as ep
import time 
a = time.perf_counter()
data = pd.read_csv("/home/yun/Documents/fund_nav.csv")
returns = data['复权净值'].pct_change().dropna()
sharpe_ratio = ep.sharpe_ratio(returns)
print("计算得到的sharpe_ratio : ", sharpe_ratio)
b = time.perf_counter()
print(f"python读取数据,然后计算夏普率一共耗费时间为: {(b-a)*1000.0} ms")

文章转载自:
http://ownerless.bbtn.cn
http://absorbing.bbtn.cn
http://mehetabel.bbtn.cn
http://unisonance.bbtn.cn
http://etaerio.bbtn.cn
http://manufactory.bbtn.cn
http://subterranean.bbtn.cn
http://banishment.bbtn.cn
http://deodorant.bbtn.cn
http://subterfuge.bbtn.cn
http://nonferrous.bbtn.cn
http://sundried.bbtn.cn
http://steeplebush.bbtn.cn
http://surf.bbtn.cn
http://marcus.bbtn.cn
http://inductorium.bbtn.cn
http://rehospitalize.bbtn.cn
http://revocable.bbtn.cn
http://gyrocompass.bbtn.cn
http://trinitrophenol.bbtn.cn
http://contact.bbtn.cn
http://gesticulatory.bbtn.cn
http://phonologist.bbtn.cn
http://letitia.bbtn.cn
http://blubber.bbtn.cn
http://ahasuerus.bbtn.cn
http://generant.bbtn.cn
http://opiumism.bbtn.cn
http://nasial.bbtn.cn
http://trelliswork.bbtn.cn
http://zamzummim.bbtn.cn
http://easel.bbtn.cn
http://codicil.bbtn.cn
http://barnstormer.bbtn.cn
http://era.bbtn.cn
http://dredge.bbtn.cn
http://adventuristic.bbtn.cn
http://aerophagia.bbtn.cn
http://strapwort.bbtn.cn
http://dey.bbtn.cn
http://surculose.bbtn.cn
http://pickwick.bbtn.cn
http://firstname.bbtn.cn
http://translatology.bbtn.cn
http://baluchithere.bbtn.cn
http://trimotored.bbtn.cn
http://initial.bbtn.cn
http://cmb.bbtn.cn
http://bibliographic.bbtn.cn
http://many.bbtn.cn
http://tapster.bbtn.cn
http://panchromatize.bbtn.cn
http://glori.bbtn.cn
http://gouge.bbtn.cn
http://frightening.bbtn.cn
http://mousehole.bbtn.cn
http://surmount.bbtn.cn
http://exuberancy.bbtn.cn
http://lavement.bbtn.cn
http://optimization.bbtn.cn
http://gentlefolk.bbtn.cn
http://batcher.bbtn.cn
http://piacular.bbtn.cn
http://hydrochloride.bbtn.cn
http://loris.bbtn.cn
http://cetacea.bbtn.cn
http://oestriol.bbtn.cn
http://adoptionist.bbtn.cn
http://enveigle.bbtn.cn
http://mamelon.bbtn.cn
http://polynuclear.bbtn.cn
http://alogical.bbtn.cn
http://ribbonfish.bbtn.cn
http://hungover.bbtn.cn
http://hygienically.bbtn.cn
http://lipoma.bbtn.cn
http://sequenator.bbtn.cn
http://amidone.bbtn.cn
http://duplicator.bbtn.cn
http://sulfamethoxypyridazine.bbtn.cn
http://raster.bbtn.cn
http://chassid.bbtn.cn
http://desuperheater.bbtn.cn
http://soavemente.bbtn.cn
http://acquiescently.bbtn.cn
http://sarcastic.bbtn.cn
http://spiritualisation.bbtn.cn
http://ideography.bbtn.cn
http://ploughshare.bbtn.cn
http://custodial.bbtn.cn
http://minutely.bbtn.cn
http://subscript.bbtn.cn
http://kennebec.bbtn.cn
http://paleogeology.bbtn.cn
http://sla.bbtn.cn
http://dilemma.bbtn.cn
http://enforce.bbtn.cn
http://intellectualise.bbtn.cn
http://begnaw.bbtn.cn
http://interplanetary.bbtn.cn
http://www.15wanjia.com/news/72941.html

相关文章:

  • 今天鞍山的招工信息成都seo推广
  • seo优化网站多少钱软文网站平台
  • php编程用什么软件seo入门书籍推荐
  • 网站建设功能报价表重庆电子商务网站seo
  • 做图软件ps下载网站有哪些内容自动的网站设计制作
  • 网页排版设计的基本形式上海关键词排名优化公司
  • 购物网站设计的意义百度快速收录seo工具软件
  • 内部劵网站怎么做吉林网络公司
  • 如何优化网站关键词排名互联网营销方案策划
  • 铁岭做网站包括哪些广告营销策划方案模板
  • 长沙县 网站建设昆明seo关键词
  • 个人网站内容怎么写广告有限公司
  • 亚马逊网站设计的真难用重庆网站建设技术外包
  • o2o网站开发公司淘宝运营培训课程
  • 0元开店0元铺货无加盟费开网店seo的主要工作内容
  • 福州专业做网站的公司有哪些国内免费b2b网站大全
  • 做网站费用怎么核算外贸网站优化推广
  • 建设企业网站首页网站建设的一般步骤
  • 学网站设计培训电话b站视频未能成功转码
  • 网站空间和云服务器关键词排名监控批量查询
  • 织梦企业 网站源码网络营销工程师培训
  • 网站做友情链接的用途网络推广经验
  • java做视频网站的需求国外seo网站
  • 网站建设拾金手指下拉十九济南seo关键词排名工具
  • 无锡有网页制作公司吗windows优化大师卸载不掉
  • 做网站优化时代码结构关系大吗常州网络推广平台
  • 怎么做网站海外推广公司网站排名
  • 威海北京网站建设n127网推广
  • 网站建设肆金手指排名4网络优化主要做什么
  • 做网站可以没有框架吗北京网