導航:首頁 > 股市分析 > 神經網路股價代碼

神經網路股價代碼

發布時間:2021-06-07 00:12:57

⑴ matlab優化神經網路預測股票程序,求大神幫忙,有重謝。

我這里有遺傳演算法優化的神經網路,但是粒子群的沒有啊!

⑵ BP神經網路matlab源程序代碼講解

newff 創建前向BP網路格式:
net = newff(PR,[S1 S2...SNl],{TF1 TF2...TFNl},BTF,BLF,PF)

其中:PR —— R維輸入元素的R×2階最大最小值矩陣; Si —— 第i層神經元的個數,共N1層; TFi——第i層的轉移函數,默認『tansig』; BTF—— BP網路的訓練函數,默認『trainlm』; BLF—— BP權值/偏差學習函數,默認』learngdm』 PF ——性能函數,默認『mse』;(誤差)

e.g.
P = [0 1 2 3 4 5 6 7 8 9 10];T = [0 1 2 3 4 3 2 1 2 3 4];
net = newff([0 10],[5 1],{'tansig' 'purelin'});net.trainparam.show=50; %每次循環50次net.trainParam.epochs = 500; %最大循環500次
net.trainparam.goal=0.01; %期望目標誤差最小值
net = train(net,P,T); %對網路進行反復訓練
Y = sim(net,P)Figure % 打開另外一個圖形窗口
plot(P,T,P,Y,'o')

⑶ 求BP神經網路演算法的C++源代碼

// AnnBP.cpp: implementation of the CAnnBP class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "AnnBP.h"
#include "math.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAnnBP::CAnnBP()
{
eta1=0.3;
momentum1=0.3;

}

CAnnBP::~CAnnBP()
{

}

double CAnnBP::drnd()
{
return ((double) rand() / (double) BIGRND);
}

/*** 返回-1.0到1.0之間的雙精度隨機數 ***/
double CAnnBP::dpn1()
{
return (double) (rand())/(32767/2)-1;
}

/*** 作用函數,目前是S型函數 ***/
double CAnnBP::squash(double x)
{
return (1.0 / (1.0 + exp(-x)));
}

/*** 申請1維雙精度實數數組 ***/
double* CAnnBP::alloc_1d_dbl(int n)
{
double *new1;

new1 = (double *) malloc ((unsigned) (n * sizeof (double)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_1D_DBL: Couldn't allocate array of doubles\n");
return (NULL);
}
return (new1);
}

/*** 申請2維雙精度實數數組 ***/
double** CAnnBP::alloc_2d_dbl(int m, int n)
{
int i;
double **new1;

new1 = (double **) malloc ((unsigned) (m * sizeof (double *)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
return (NULL);
}

for (i = 0; i < m; i++) {
new1[i] = alloc_1d_dbl(n);
}

return (new1);
}

/*** 隨機初始化權值 ***/
void CAnnBP::bpnn_randomize_weights(double **w, int m, int n)
{
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = dpn1();
}
}

}

/*** 0初始化權值 ***/
void CAnnBP::bpnn_zero_weights(double **w, int m, int n)
{
int i, j;

for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = 0.0;
}
}

}

/*** 設置隨機數種子 ***/
void CAnnBP::bpnn_initialize(int seed)
{
CString msg,s;
msg="Random number generator seed:";
s.Format("%d",seed);
AfxMessageBox(msg+s);
srand(seed);
}

/*** 創建BP網路 ***/
BPNN* CAnnBP::bpnn_internal_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = (BPNN *) malloc (sizeof (BPNN));
if (newnet == NULL) {
printf("BPNN_CREATE: Couldn't allocate neural network\n");
return (NULL);
}

newnet->input_n = n_in;
newnet->hidden_n = n_hidden;
newnet->output_n = n_out;
newnet->input_units = alloc_1d_dbl(n_in + 1);
newnet->hidden_units = alloc_1d_dbl(n_hidden + 1);
newnet->output_units = alloc_1d_dbl(n_out + 1);

newnet->hidden_delta = alloc_1d_dbl(n_hidden + 1);
newnet->output_delta = alloc_1d_dbl(n_out + 1);
newnet->target = alloc_1d_dbl(n_out + 1);

newnet->input_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

newnet->input_prev_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_prev_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

return (newnet);

}

/* 釋放BP網路所佔地內存空間 */
void CAnnBP::bpnn_free(BPNN *net)
{
int n1, n2, i;

n1 = net->input_n;
n2 = net->hidden_n;

free((char *) net->input_units);
free((char *) net->hidden_units);
free((char *) net->output_units);

free((char *) net->hidden_delta);
free((char *) net->output_delta);
free((char *) net->target);

for (i = 0; i <= n1; i++) {
free((char *) net->input_weights[i]);
free((char *) net->input_prev_weights[i]);
}
free((char *) net->input_weights);
free((char *) net->input_prev_weights);

for (i = 0; i <= n2; i++) {
free((char *) net->hidden_weights[i]);
free((char *) net->hidden_prev_weights[i]);
}
free((char *) net->hidden_weights);
free((char *) net->hidden_prev_weights);

free((char *) net);
}

/*** 創建一個BP網路,並初始化權值***/
BPNN* CAnnBP::bpnn_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = bpnn_internal_create(n_in, n_hidden, n_out);

#ifdef INITZERO
bpnn_zero_weights(newnet->input_weights, n_in, n_hidden);
#else
bpnn_randomize_weights(newnet->input_weights, n_in, n_hidden);
#endif
bpnn_randomize_weights(newnet->hidden_weights, n_hidden, n_out);
bpnn_zero_weights(newnet->input_prev_weights, n_in, n_hidden);
bpnn_zero_weights(newnet->hidden_prev_weights, n_hidden, n_out);

return (newnet);

}

void CAnnBP::bpnn_layerforward(double *l1, double *l2, double **conn, int n1, int n2)
{
double sum;
int j, k;

/*** 設置閾值 ***/
l1[0] = 1.0;

/*** 對於第二層的每個神經元 ***/
for (j = 1; j <= n2; j++) {

/*** 計算輸入的加權總和 ***/
sum = 0.0;
for (k = 0; k <= n1; k++) {
sum += conn[k][j] * l1[k];
}
l2[j] = squash(sum);
}
}

/* 輸出誤差 */
void CAnnBP::bpnn_output_error(double *delta, double *target, double *output, int nj, double *err)
{
int j;
double o, t, errsum;

errsum = 0.0;
for (j = 1; j <= nj; j++) {
o = output[j];
t = target[j];
delta[j] = o * (1.0 - o) * (t - o);
errsum += ABS(delta[j]);
}
*err = errsum;

}

/* 隱含層誤差 */
void CAnnBP::bpnn_hidden_error(double *delta_h, int nh, double *delta_o, int no, double **who, double *hidden, double *err)
{
int j, k;
double h, sum, errsum;

errsum = 0.0;
for (j = 1; j <= nh; j++) {
h = hidden[j];
sum = 0.0;
for (k = 1; k <= no; k++) {
sum += delta_o[k] * who[j][k];
}
delta_h[j] = h * (1.0 - h) * sum;
errsum += ABS(delta_h[j]);
}
*err = errsum;
}

/* 調整權值 */
void CAnnBP::bpnn_adjust_weights(double *delta, int ndelta, double *ly, int nly, double **w, double **oldw, double eta, double momentum)
{
double new_dw;
int k, j;

ly[0] = 1.0;
for (j = 1; j <= ndelta; j++) {
for (k = 0; k <= nly; k++) {
new_dw = ((eta * delta[j] * ly[k]) + (momentum * oldw[k][j]));
w[k][j] += new_dw;
oldw[k][j] = new_dw;
}
}

}

/* 進行前向運算 */
void CAnnBP::bpnn_feedforward(BPNN *net)
{
int in, hid, out;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** Feed forward input activations. ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

}

/* 訓練BP網路 */
void CAnnBP::bpnn_train(BPNN *net, double eta, double momentum, double *eo, double *eh)
{
int in, hid, out;
double out_err, hid_err;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** 前向輸入激活 ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

/*** 計算隱含層和輸出層誤差 ***/
bpnn_output_error(net->output_delta, net->target, net->output_units,
out, &out_err);
bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,
net->hidden_weights, net->hidden_units, &hid_err);
*eo = out_err;
*eh = hid_err;

/*** 調整輸入層和隱含層權值 ***/
bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid,
net->hidden_weights, net->hidden_prev_weights, eta, momentum);
bpnn_adjust_weights(net->hidden_delta, hid, net->input_units, in,
net->input_weights, net->input_prev_weights, eta, momentum);
}

/* 保存BP網路 */
void CAnnBP::bpnn_save(BPNN *net, char *filename)
{
CFile file;
char *mem;
int n1, n2, n3, i, j, memcnt;
double dvalue, **w;
n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n;
printf("Saving %dx%dx%d network to '%s'\n", n1, n2, n3, filename);
try
{
file.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

file.Write(&n1,sizeof(int));
file.Write(&n2,sizeof(int));
file.Write(&n3,sizeof(int));

memcnt = 0;
w = net->input_weights;
mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double)));
// mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
dvalue = w[i][j];
//fast(&mem[memcnt], &dvalue, sizeof(double));
fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);

}
}

file.Write(mem,sizeof(double)*(n1+1)*(n2+1));
free(mem);

memcnt = 0;
w = net->hidden_weights;
mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double)));
// mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));
for (i = 0; i <= n2; i++) {
for (j = 0; j <= n3; j++) {
dvalue = w[i][j];
fast(&mem[memcnt], &dvalue, sizeof(double));
// fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);
}
}

file.Write(mem, (n2+1) * (n3+1) * sizeof(double));
// free(mem);

file.Close();
return;
}

/* 從文件中讀取BP網路 */
BPNN* CAnnBP::bpnn_read(char *filename)
{
char *mem;
BPNN *new1;
int n1, n2, n3, i, j, memcnt;
CFile file;

try
{
file.Open(filename,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

// printf("Reading '%s'\n", filename);// fflush(stdout);

file.Read(&n1, sizeof(int));
file.Read(&n2, sizeof(int));
file.Read(&n3, sizeof(int));

new1 = bpnn_internal_create(n1, n2, n3);

// printf("'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3);
// printf("Reading input weights..."); // fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));

file.Read(mem, ((n1+1)*(n2+1))*sizeof(double));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
//fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);
}
}
free(mem);

// printf("Done\nReading hidden weights..."); //fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));

file.Read(mem, (n2+1) * (n3+1) * sizeof(double));
for (i = 0; i <= n2; i++) {

for (j = 0; j <= n3; j++) {
//fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);

}
}
free(mem);
file.Close();

printf("Done\n"); //fflush(stdout);

bpnn_zero_weights(new1->input_prev_weights, n1, n2);
bpnn_zero_weights(new1->hidden_prev_weights, n2, n3);

return (new1);
}

void CAnnBP::CreateBP(int n_in, int n_hidden, int n_out)
{
net=bpnn_create(n_in,n_hidden,n_out);
}

void CAnnBP::FreeBP()
{
bpnn_free(net);

}

void CAnnBP::Train(double *input_unit,int input_num, double *target,int target_num, double *eo, double *eh)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}

for(int j=1;j<=target_num;j++)
{
net->target[j]=target[j-1];
}
bpnn_train(net,eta1,momentum1,eo,eh);

}

void CAnnBP::Identify(double *input_unit,int input_num,double *target,int target_num)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}
bpnn_feedforward(net);
for(int j=1;j<=target_num;j++)
{
target[j-1]=net->output_units[j];
}
}

void CAnnBP::Save(char *filename)
{
bpnn_save(net,filename);

}

void CAnnBP::Read(char *filename)
{
net=bpnn_read(filename);
}

void CAnnBP::SetBParm(double eta, double momentum)
{
eta1=eta;
momentum1=momentum;

}

void CAnnBP::Initialize(int seed)
{
bpnn_initialize(seed);

}

⑷ 誰能教我寫一個MATLAB實現BP神經網路預測股票價格的編碼,我要寫畢業論文,不懂,多謝啊!

網路的訓練過程與使用過程了兩碼事。
比如BP應用在分類,網路的版訓練是指的給你一些樣本權,同時告訴你這些樣本屬於哪一類,然後代入網路訓練,使得這個網路具備一定的分類能力,訓練完成以後再拿一個未知類別的數據通過網路進行分類。這里的訓練過程就是先偽隨機生成權值,然後把樣本輸入進去算出每一層的輸出,並最終算出來預測輸出(輸出層的輸出),這是正向學習過程;最後通過某種訓練演算法(最基本的是感知器演算法)使得代價(預測輸出與實際輸出的某范數)函數關於權重最小,這個就是反向傳播過程。
您所說的那種不需要預先知道樣本類別的網路屬於無監督類型的網路,比如自組織競爭神經網路。

⑸ 利用BP神經網路預測股票價格走勢

參考 matlab神經網路30例 中有一個股票預測的案例
我覺得svm做這個更好

⑹ 基於神經網路的股票預測

還要含代碼?
你的 t 讓門夾了吧?

⑺ 用人工神經網路進行股票預測,數據樣本為開盤,收盤,最高,最低,成交量,成交額。用weka或matlab實現

把樣本數據分為訓練樣本和測試樣本,然後用訓練樣本訓練網路,用測試樣本進行模型驗證

⑻ BP神經網路預測股票

感知器你知道么,如果不知道,建議你買《人工神經網路原理》馬銳著,看完70頁你就會了。里邊也有你這個問題的設計思路。用c語言matlab都能編,如果有問題,請留言,想問下你是什麼專業?

⑼ bp神經網路股票價格預測的MATLAB編程

P=[];『輸入,開盤價,最高價,最低價,收盤價成交量依次5天的數據』
T=[];』專輸出,即第二屬日的收盤』
net=newff(minmax(P),[7,1],{'tansig','logsig'},'traingdx');
net.trainParam.epochs=1000; 『最大訓練次數,根據需要可自行調節』
net.trainParam.goal=0.01; 『誤差』
net.trainParam.lr=0.01; 『學習率』
net=train(net,P,T); 『訓練網路』
test=[];『待預測數據輸入』
out=sim(net,test); 『模擬預測』
我的這個程序沒有進行初始化,你還需要先將數據進行初始化後才能算。

閱讀全文

與神經網路股價代碼相關的資料

熱點內容
貴金屬無風險套利 瀏覽:72
漢博投資發展有限公司 瀏覽:461
貴金屬投資開場白 瀏覽:885
只做歐洲盤外匯 瀏覽:648
信託公司理財產品質押貸款 瀏覽:127
科創板基金還有沒有第三批 瀏覽:782
融資易怎樣 瀏覽:762
嘉實基金年度報告 瀏覽:422
股票投資者起訴 瀏覽:55
組合貸款公積金沒交了 瀏覽:716
復塑科技股票 瀏覽:226
福建泉南投資開發有限公司簡介 瀏覽:461
80美金相當於多少人民幣 瀏覽:235
中石油股票行情查詢 瀏覽:969
江蘇蘇豪創業投資公司 瀏覽:81
合眾思壯股票吧 瀏覽:918
股票漲停還能買賣嗎 瀏覽:283
70000韓元摺合人民幣多少人民幣 瀏覽:289
2017年11月美金匯率對人民幣 瀏覽:799
海寧私募基金出問題 瀏覽:237