Qi

Cogito ergo sum

安装

默认安装位置

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA

nvcc –version 查看安装版本(环境变量配置)

cuda下载地址

最新版本

https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10

历史版本

https://developer.nvidia.com/cuda-toolkit-archive

cudnn下载地址

https://developer.nvidia.com/rdp/cudnn-download

版本关系和查看

cuda: Compute Unified Device Architecture,是一种有NVIDIA推出的通用并行计算架构, 该架构使GPU能够解决复杂的计算问题。

cudnn: 是NVIDIA 推出的用于深度神经网络的GPU加速库,他强调性能,易用性和低内存开销。

cuda和cudnn的关系: cudnn是基于cuda架构开发的专门用于深度神经网络的GPU加速库。cuda可以理解为一个大的商圈,但这个商圈是空的,还未装修。cudnn可以理解为装修后的房间,例如负一楼专门针对游乐(深度神经网络)装修成大型游乐厂。

Volatile是Java虚拟机提供的**轻量级**的同步机制

三大特性

  • 保证可见性
  • 不保证原子性
  • 禁止指令重排

内存可见性

volatile修饰的变量,不拷贝副本到工作内存,直接操作主内存,任何修改都可以其他线程读取到。
volatile的可以保证写入和读取 64 位值(long类型和double类型 )的原子性。

单例中 volatile + synchronized 的作用

volatile 可见性、禁止指令重排
synchronized保证操作的原子性、一致性和可见性。

example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import redis
import matplotlib.pyplotasplt
import pickle

r_basicdx=redis.Redis(host='172.20.3.52',port=6373,db=1)
l_basicdx=redis.Redis(host='127.0.0.1',port=6379,db=1)
keys=r_basicdx.keys()
forkeyinkeys:
value=r_basicdx.get(key)
l_basicdx.set(key,value)
image=pickle.loads(value)
f=plt.figure()
ax=f.add_subplot(111)
ax.text(0.1,0.9,key,ha='center',va='center',transform=ax.transAxes)
plt.imshow(image)
plt.show()

下载与安装

你可以使用我们提供的 Pip, Docker, Virtualenv, Anaconda 或 源码编译的方法安装 TensorFlow.
Pip 安装
Pip 是一个 Python 的软件包安装与管理工具.
在安装 TensorFlow 过程中要涉及安装或升级的包详见 列表
首先安装 pip (或 Python3 的 pip3 ):

Ubuntu/Linux 64-bit

$ sudo apt-get install python-pip python-dev

Mac OS X

$ sudo easy_install pip
安装 TensorFlow :

Ubuntu/Linux 64-bit, CPU only, Python 2.7:

$ sudo pip install –upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.

For other versions, see “Install from sources” below.

$ sudo pip install –upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

Mac OS X, CPU only:

$ sudo easy_install –upgrade six
$ sudo pip install –upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
如果是 Python3 :

Ubuntu/Linux 64-bit, CPU only, Python 3.4:

$ sudo pip3 install –upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl

Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.

For other versions, see “Install from sources” below.

$ sudo pip3 install –upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl

Mac OS X, CPU only:

$ sudo easy_install –upgrade six
$ sudo pip3 install –upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py3-none-any.whl
备注:如果之前安装过 TensorFlow < 0.7.1 的版本,应该先使用 pip uninstall 卸载 TensorFlow 和 protobuf ,保证获取的是一个最新 protobuf 依赖下的安装包.
之后可以测试一下.
基于 Docker 的安装
我们也支持通过 Docker 运行 TensorFlow. 该方式的优点是不用操心软件依赖问题.
首先, 安装 Docker. 一旦 Docker 已经启动运行, 可以通过命令启动一个容器:
$ docker run -it b.gcr.io/tensorflow/tensorflow
该命令将启动一个已经安装好 TensorFlow 及相关依赖的容器.
其它镜像
默认的 Docker 镜像只包含启动和运行 TensorFlow 所需依赖库的一个最小集. 我们额外提供了 下面的容器, 该容器同样可以通过上述 docker run 命令安装:
b.gcr.io/tensorflow/tensorflow-full: 镜像中的 TensorFlow 是从源代码完整安装的, 包含了编译和运行 TensorFlow 所需的全部工具. 在该镜像上, 可以直接使用源代码进行实验, 而不需要再安装上述的任何依赖.
基于 VirtualEnv 的安装
我们推荐使用 virtualenv 创建一个隔离的容器, 来安装 TensorFlow. 这是可选的, 但是这样做能使排查安装问题变得更容易.
首先, 安装所有必备工具:

在 Linux 上:

$ sudo apt-get install python-pip python-dev python-virtualenv

在 Mac 上:

$ sudo easy_install pip # 如果还没有安装 pip
$ sudo pip install –upgrade virtualenv
接下来, 建立一个全新的 virtualenv 环境. 为了将环境建在 ~/tensorflow 目录下, 执行:
$ virtualenv –system-site-packages ~/tensorflow
$ cd ~/tensorflow
然后, 激活 virtualenv:
$ source bin/activate # 如果使用 bash
$ source bin/activate.csh # 如果使用 csh
(tensorflow)$ # 终端提示符应该发生变化
在 virtualenv 内, 安装 TensorFlow:
(tensorflow)$ pip install –upgrade <$url_to_binary.whl>
接下来, 使用类似命令运行 TensorFlow 程序:
(tensorflow)$ cd tensorflow/models/image/mnist
(tensorflow)$ python convolutional.py

当使用完 TensorFlow

(tensorflow)$ deactivate # 停用 virtualenv

$ # 你的命令提示符会恢复原样
基于 Anaconda 的安装
Anaconda 是一个集成许多第三方科学计算库的 Python 科学计算环境,Anaconda 使用 conda 作为自己的包管理工具,同时具有自己的计算环境,类似 Virtualenv.
和 Virtualenv 一样,不同 Python 工程需要的依赖包,conda 将他们存储在不同的地方。 TensorFlow 上安装的 Anaconda 不会对之前安装的 Python 包进行覆盖.
安装 Anaconda
建立一个 conda 计算环境
激活环境,使用 conda 安装 TensorFlow
安装成功后,每次使用 TensorFlow 的时候需要激活 conda 环境
安装 Anaconda :
参考 Anaconda 的下载页面的指导
建立一个 conda 计算环境名字叫tensorflow:

Python 2.7

$ conda create -n tensorflow python=2.7

Python 3.4

$ conda create -n tensorflow python=3.4
激活tensorflow环境,然后使用其中的 pip 安装 TensorFlow. 当使用easy_install使用–ignore-installed标记防止错误的产生。
$ source activate tensorflow
(tensorflow)$ # Your prompt should change

Ubuntu/Linux 64-bit, CPU only, Python 2.7:

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.

For other versions, see “Install from sources” below.

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

Mac OS X, CPU only:

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl
对于 Python 3.x :
$ source activate tensorflow
(tensorflow)$ # Your prompt should change

Ubuntu/Linux 64-bit, CPU only, Python 3.4:

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl

Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.

For other versions, see “Install from sources” below.

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl

Mac OS X, CPU only:

(tensorflow)$ pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py3-none-any.whl
conda 环境激活后,你可以测试
当你不用 TensorFlow 的时候,关闭环境:
(tensorflow)$ source deactivate

$ # Your prompt should change back
再次使用的时候再激活 :-)
$ source activate tensorflow
(tensorflow)$ # Your prompt should change.

Run Python programs that use TensorFlow.

When you are done using TensorFlow, deactivate the environment.

(tensorflow)$ source deactivate

投资决策模型化,基于历史数据验证,交易自动执行

股市波动,长期、短期、相对

投资指标

风险收益率

夏普率=(收益-无风险利率)/波动率

胜率赔率

胜率是指出手赚钱次数与总出手次数之比

赔率是指平均每次出手赚到的钱除以平均每次出手赔的钱,也叫做盈亏比

策略

阿尔法策略

优点:不管指数是涨还是下跌,都能赚钱的一种方法,具体的操作思路是找出市场里最优秀的品种,做多这些品种,然后做空相应多的指数,这样就锁定了最优秀的品种带来的收益,而把指数带来的波动进行了平抑。
缺点:回撤和收益都比较小的交易策略

程序化CTA

优点:适合大众使用的程序化交易方法,是指将交易策略的思想设计成完整的逻辑运行体系,然后用合适的计算机语言编写成程序,有计算机进行自动交易。程序化交易的优点是,将交易模式系统化,制度化,排除人性的心理障碍,确保交易策略的执行行。挣的是趋势的钱,挣的是纪律的钱,但因为趋势不常有,所以这是一种低胜率,高赔率的方法。

入场条件、出场条件,品种选择、时机选择,资金管理

统计套利

通过计算某些关联品种之间出现了价差的扩大,那就可以在品种之间进行配对交易,从而进行套利

低风险套利

ETF套利也是一种低风险套利,比如某个ETF指数基金现在的价格是2.1,而如果我们用一揽子股票来组成这个ETF指数基金,价格是2块,那么我们就可以在市场上卖出基金,买入股票,来得到这0.1的差价。这些套利比较容易执行,收入也很可观,而且风险很小。

高频交易

利用计算机处理市场微观结构层面的不均衡性,往往交易次数多,持仓时间短,可能会送大量交易指令,又快速撤单,再反向做交易获得收益,每笔交易平均利润小但稳定。

优点是总收益率极高,当日平仓降低隔夜风险,隔夜资金利息收入降低资金成本,绩效评估周期短。

算法交易

降低冲击成本的一种被动的程序化交易,通过科学的成本估算模型和交易实施算法,将一个大额的交易拆分成系列小额交易 在合理的时间点分别执行,以此来尽量减少 对市场价格造成的冲击,降低交易成本,而且还能帮助机构投资者快速增加交易量。适合的对象包括大小非减持者,大宗交易接盘出货,“大宗交易-融券卖出”套利者,Alpha套利者,套期保值者,日以上级别程序化交易者等。

参考:https://www.jianshu.com/p/2c470ef5d083

开发过程中,需要根据不同场景选择不同算法和策略。

将需要经常改变的部分抽象提取为接口,通过引用该接口,就可以调用该接口实现类的行为,即具体策略。实现了策略具体实现和调用者的隔离。

优点

  1. 可以动态改变对象行为

缺点

  1. 使用哪种策略需要调用者自己决定
  2. 产生很多策略类

示例

public interface IStrategy {
    public void doSomething();
}

public class Strategy1 implements IStrategy {
@Override
public void doSomething() {

}

}

public class Strategy2 implements IStrategy {
@Override
public void doSomething() {

}

}

public class Context implements IStrategy {

private IStrategy strategy;

public Context(IStrategy strategy){
this.strategy = strategy;
}

@Override
public void doSomething() {
strategy.doSomething();
}

}

public static void main(String[] args) {
IStrategy strategy = new Strategy1();
Context context = new Context(strategy);
context.doSomething();
}

流程相同,执行过程中有差别

可用于定义算法骨架,将一些实现放到子类中实现,达到不用改变算法结构,重新实现算法的目的

代码实现

package template;

public abstract class Model {
/**
* 类似的行为、逻辑
*/
protected abstract void start();

protected abstract void stop();

/**

  • 固定的流程 模板化
    */
    final public void excet(){
    this.start();
    this.stop();
    }

}

package template;

public class Ocar extends Model {
@Override
protected void start() {

}

@Override
protected void stop() {

}

}

package template;

public class Wcar extends Model {
@Override
protected void start() {

}

@Override
protected void stop() {

}

}

package template;

public class Client {
public static void main(String[] args) {
Model wcar=new Wcar();
wcar.excet();

    Model ocar=new Ocar();
    ocar.excet();
}

}

代理模式+策略模式,典型 Spring 中的 DispatcherServlet

用户将任务全权委派给代理负责,代理负责根据策略任务调度具体的执行单元

执行单元

/**
 * 执行单元实现的接口
 */
public interface ITarget {

void doSomething(String command);

}

/**

  • 执行单元A
    */
    public class TargetA implements ITarget {
    @Override
    public void doSomething(String command) {

    }

}

/**

  • 执行单元B
    */
    public class TargetB implements ITarget {
    @Override
    public void doSomething(String command) {

    }

}

执行单元调度者

public class Manager {
    private Map<String ,ITarget> targets = new HashMap<String ,ITarget>();

public Manager(){
    targets.put("commandA",new TargetA());
    targets.put("commandB",new TargetB());
}

public void dispatch(String command){
    targets.get(command).doSomething(command);
}

}

使用者

public class Boss {
    
public static void main(String[] args) {
    Manager manager = new Manager();
    manager.dispatch("Command1");
}

}

使用原型对象的方法创建对象的实列,创建的实例equals原型

final 类型修饰的成员变量不能进行深度拷贝

场景

  • 类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等
  • 构造对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式
  • 工厂模式中使用
  • 共有信息很多

浅拷贝

对象内引用原先原型的对象

深拷贝

对象内引用也拷贝一份

代码实现

package prototype;

//Cloneable 标记对象可拷贝
public class PersonClone implements Cloneable {

@Override //Override Object方法
public PersonClone clone(){
    try {
        //clone 不会调用构造方法
        return (PersonClone)super.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return null;
}

}

package prototype;

public class MainClass {
public static void main(String[] args) {
PersonClone personClone = new PersonClone();
PersonClone clone = personClone.clone();
System.out.println(personClone.hashCode());
System.out.println(clone.hashCode());
}
}

0%